Harry
Harry

Reputation: 1091

How to add avoidance and chase behavior in Unity3D

I would like to implement the type of behavior where a group of prey is evading from a predator like this Game

I tried to write the script this way but I don't get the desired motion the prey just moves forward.

public Transform target;

    public float damping;
    public float drivespeed;

    void Update () {
        transform.Translate(Vector3.forward * Time.deltaTime * -drivespeed);

        Quaternion rotation = Quaternion
            .LookRotation(target.position - transform.position);
        transform.rotation = Quaternion
            .Slerp(transform.rotation, rotation, Time.deltaTime * damping);
    }

Upvotes: 0

Views: 44

Answers (2)

AKX
AKX

Reputation: 169184

Looks like you want

transform.Translate(transform.forward * Time.deltaTime * -drivespeed);

instead of

transform.Translate(Vector3.forward * Time.deltaTime * -drivespeed);

(Vector3.forward being a world forward vector, not the object's forward vector.)

Upvotes: 2

Will Anderson
Will Anderson

Reputation: 113

It could be that the transform.translate is being called before the rotation logic but it's hard to tell what's going wrong here.

  • Is the object rotating correctly?
  • If so is the transform moving forward ignoring the rotation?
  • Are drivespeed and damping variables != 0?

Upvotes: 0

Related Questions