#I need help with enemy targeting (i used the 2d brackeys astar pathfinding tutorial)

1 messages · Page 1 of 1 (latest)

stone spade
#

just realised i should put the code to make it easier to help
{
public GameObject Enemy;
public GameObject FoodItem;

public Slider HealthSlider;
public float maxHealth = 100;
public float currentHealth;

public Slider HungerSlider;
public float Hunger;
public float MaxHunger = 100f;

public Transform target;
public Transform Den;

public float speed = 200f;
public float NextWaypointDistance = 3f;

Path path;
int currentWaypoint = 0;
bool reachedEndOfPath = false;

Seeker seeker;
Rigidbody2D rb;

public float chaseRadius;
public float attackRadius;

void Start()
{
    Hunger = MaxHunger;
    currentHealth = maxHealth;

    seeker = GetComponent<Seeker>();
    rb = GetComponent<Rigidbody2D>();

    InvokeRepeating("UpdatePath", 0f, .5f);

    target = Den;
}
#

void Update()
{
HungerSlider.value = Hunger;
HealthSlider.value = currentHealth;

    if (Hunger >= 0)
    {
        Hunger -= 1f * Time.deltaTime;
    }

    if (Hunger > 100)
    {
        Hunger = 100;
    }
#

if (currentHealth >= 0)
{
if (Hunger <= 0)
{
TakeDamage(0.8f * Time.deltaTime);
}
}

    if (currentHealth <= 0)
    {
        GameObject go = Instantiate(FoodItem);
        go.transform.position = Enemy.transform.position;
        Destroy(Enemy);
    }

    if (Hunger <= 60)
    {
        if (!GameObject.FindWithTag("Scavanger"))
        {
            target = Den;
        }
        else
        {
            target = GameObject.FindWithTag("Scavanger").transform;
        }
    }

    if (Hunger <= 40)
    {
        if (!GameObject.FindWithTag("Player"))
        {
            target = Den;
        }
        else
        {
            target = GameObject.FindWithTag("Player").transform;
        }
    }

    if (Hunger > 60)
    {
        target = Den;
    }

    if (currentHealth > maxHealth)
    {
        currentHealth = maxHealth;
    }
}

void UpdatePath()
{
    if (seeker.IsDone())
    {
        seeker.StartPath(rb.position, target.position, OnPathComplete);
    }
}

void OnPathComplete(Path p)
{
    if (!p.error)
    {
        path = p;
        currentWaypoint = 0;
    }
}

void FixedUpdate()
{
    if (path == null)
        return;

    if (currentWaypoint >= path.vectorPath.Count)
    {
        reachedEndOfPath = true;
        return;
    }
    else
    {
        reachedEndOfPath = false;
    }
#

Vector2 direction = ((Vector2)path.vectorPath[currentWaypoint] - rb.position).normalized;
Vector2 force = direction * speed * Time.deltaTime;

    rb.AddForce(force);

    float distance = Vector2.Distance(rb.position, path.vectorPath[currentWaypoint]);

    if (distance < NextWaypointDistance)
    {
        currentWaypoint++;
    }

}

void TakeDamage(float damage)
{
    currentHealth -= damage;
}

private void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.gameObject.CompareTag("Food"))
    {
        Hunger += 40;
        currentHealth += 1;
    }
    if (collision.gameObject.CompareTag("Small Food"))
    {
        Hunger += 15;
        currentHealth += 1;
    }
    if (collision.gameObject.CompareTag("Player"))
    {
        currentHealth -= 1;
    }
}

}

still prairieBOT
#

Use codeblocks to send code in a message!

To make a codeblock, surround your code with ```
To use C# syntax highlighting add cs after the three back ticks.

For example:
```cs
Console.WriteLine("Hello World");
```

Produces:

Console.WriteLine("Hello World");

To send lengthy code, paste it into https://paste.myst.rs/ and send the link of the paste into chat.

obsidian dew
#

And why on earth did you delete your original post? Now no one knows what your problem is blobGlare

stone spade
obsidian dew
#

You don't need to delete the thread

#

If you solve the issue, we prefer if you mark it Solved (you can also close it)

#

If you mark it Solved, and share how you solved it, someone else who faces a similar issue might be able to come in, and see your solution

#

You would be helping others

stone spade