-1
\$\begingroup\$

Following this tutorial: Understanding Steering Behaviors: Seek, I tried to add seek and steering behavior to my Unity prototype but it's not working this is my script. The cube does not steer it just follows the target.

public GameObject target;

    float maxForce = 15;
    float maxSpeed = 30;
    float maxVelocity = 200;
    float mass = 15;

    Vector3 velocity;

    void Start() {
        velocity = Vector3.zero;
    }

    void Update() {
        transform.LookAt(target.transform);

        var desiredVelocity = target.transform.position - transform.position;
        desiredVelocity = desiredVelocity.normalized * maxVelocity * Time.deltaTime;

        var steering = desiredVelocity - velocity;
        steering = Vector3.ClampMagnitude(steering, maxForce);
        steering = steering / mass;

        velocity = Vector3.ClampMagnitude(velocity + steering, maxSpeed);
        transform.position += velocity * Time.deltaTime;

    }

What could be the problem??

\$\endgroup\$
  • \$\begingroup\$ If it's correctly following the target, then it sounds like the steering behaviour is performing as advertised. Can you describe what you mean by "does not steer" in more detail? \$\endgroup\$ – DMGregory Aug 2 '18 at 11:23
  • \$\begingroup\$ The object does not steer towards the target but instead, it just goes straight to the target without any steering behavior. \$\endgroup\$ – Harry Aug 2 '18 at 11:41
  • \$\begingroup\$ You mean that if the object was moving with a particular velocity, then the target moved, the object heads directly to the new target without its path visibly arcing? It sounds like you've tuned the maxForce parameter too high for your mass & typical speeds. If it can, this script will complete its turn in a single frame and head directly in the new direction. It's the maxForce that limits how much it can change its velocity in a single frame. Have you tried tuning the parameters to achieve your desired behaviour? \$\endgroup\$ – DMGregory Aug 2 '18 at 11:46
  • \$\begingroup\$ Thanks i made some changes to the values it works perfectly now \$\endgroup\$ – Harry Aug 2 '18 at 14:55
1
\$\begingroup\$

I had to make some adjustments to the values this works perfectly.

   public float Mass = 15;
    public float MaxVelocity = 3;
    public float MaxForce = 15;

    private Vector3 velocity;
    public Transform target;

    private void Start() {
        velocity = Vector3.zero;
    }

    private void Update() {
        var desiredVelocity = target.transform.position - transform.position;
        desiredVelocity = desiredVelocity.normalized * MaxVelocity;

        var steering = desiredVelocity - velocity;
        steering = Vector3.ClampMagnitude(steering, MaxForce);
        steering /= Mass;

        velocity = Vector3.ClampMagnitude(velocity + steering, MaxVelocity);
        transform.position += velocity * Time.deltaTime;
        transform.forward = velocity.normalized;

        Debug.DrawRay(transform.position, velocity.normalized * 2, Color.green);
        Debug.DrawRay(transform.position, desiredVelocity.normalized * 2, Color.magenta);

    }
\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.