🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Kinematics and rotating to a target

Started by
1 comment, last by dmatter 4 years, 7 months ago

Hi


I have an issue trying to get my object to turn to the target and reach the target. The problem is, some times a target chosen is at such a position that my object just can't turn to reach it and thus keeps rotating endless orbiting around the object and i don't know how to correct for this.

Here is a visual of the issue:

hhttps://i.imgur.com/bFi9QNn.gif

Where i need it to do is some how figure out that it needs to slow down its forward velocity but turn at the same speed, but the turning is based on the change in velocity so i am confused how to fix it.

The rotation of the mesh visual just rotates to face the same direction as the velocity.


How can i improve on this steering behaviour so its more precise?

This is my current code:

    _acceleration = _acceleration.normalized * MaxAcceleration; // constant acceleration
    _velocity += _acceleration * Time.deltaTime;
    
    // limit to max velocity
    float speed = _velocity.magnitude;
    Vector3 dir = _velocity / speed;
    speed = Mathf.Clamp(speed, _minSpeed, _maxSpeed);
    _velocity = dir * speed;

    transform.position += _velocity * Time.deltaTime;
    transform.forward = _velocity.normalized;
Advertisement

Your code snippet doesn't really show where any actual steering takes places or how you calculate a change in direction (it just integrates acceleration and caps the velocity).

From watching the gif I wondered if you could cut the thrust when close to the target and if you introduce a drag term (like air resistance) to dampen the forward speed then it will gradually slow down and hopefully allow it to reach a closer distance to the target.

More generally, it is extremely unlikely to ever exactly match the floating-point representation of the target position and will instead oscillate around that position almost like a spring, as demonstrated by your gif. By adjusting the thrust, turnspeed, drag, deacceleration, thrust-cutoff, etc you can tune how close it reaches before oscillation occurs but you will always need a tolerance value to say "ok that's close enough" to consider the target as reached.

This topic is closed to new replies.

Advertisement