How to make a delay of a missile's rotation in Unity3D?

Hello there,

I have a missile flying towards the player (2D). It rotates immediately when it ‘sees’ the player and flies towards him. So far so good.

Now my question is: How do I delay the rotation of the missile? Currently it rotates towards the player exactly (i.e. synchronously) when player moves. Any help, please?

This is my code of the missile (just an excerpt; it’s the part that controls its behavior when flying to the player):

protected Transform m_target;//player
    public Vector3 m_targetOffset;//Use this if the center of the object is not what you target
    public AnimationCurve m_distanceInfluence = AnimationCurve.Linear(0, 1, 1, 1);//Increase the intensity depending on the distance
    public float m_searchRange = 10f;//Range within the missile will search a new target
    public float m_guidanceIntensity = 5f;//Intensity the missile will be guided with
    protected bool m_lookDirection = true;
    protected Vector3 m_lookDirectionOffset;
    public float m_searchRange = 10f;//Range within the missile will search a new target


    void FixedUpdate() {
       GoToTarget();
    }

    protected virtual void GoToTarget() {
        m_direction = m_target.position + (Vector3)m_targetOffset - this.transform.position.normalized * m_distanceInfluence.Evaluate(1 - (m_target.position + (Vector3)m_targetOffset - this.transform.position).magnitude / m_searchRange);
        m_rigidbody.velocity = Vector2.ClampMagnitude(m_rigidbody.velocity + (Vector2)m_direction * m_guidanceIntensity, m_rigidbody.velocity.magnitude);
      
        if (m_rigidbody.velocity != Vector2.zero)
        {
            m_forward = m_rigidbody.velocity.normalized;

            if (m_lookDirection)
            {
                this.transform.eulerAngles = new Vector3(0, 0, -Mathf.Atan2(m_rigidbody.velocity.x, m_rigidbody.velocity.y) * Mathf.Rad2Deg);
                this.transform.Rotate(m_lookDirectionOffset);
            }
        }
    }

A couple of approaches.

  1. Instantly rotate toward the player, but by a limited amount. Use transform.RotateTowards() instead of Rotate. This doesn’t “delay the rotation” as you requested, but it’s more physically and realistically accurate, and simpler to code as well.

  2. Create a queue of player positions. On each player Update, Enqueue the player position, and if the queue is longer than N elements, Dequeue elements until it is just N elements long. On each rocket, use Peek on the queue to see the N-th-ago position of the player. This is more or less what you requested, taken literally.

1 Like

Smoothing movement between any two particular values:

You have currentQuantity and desiredQuantity.

  • only set desiredQuantity
  • the code always moves currentQuantity towards desiredQuantity
  • read currentQuantity for the smoothed value

Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

The code: SmoothMovement.cs · GitHub

I have attached an actual missile-turning game package for your reference. This game uses the process above, which I use EVERYWHERE in my games for smoothly-changing quantities.

8718540–1178694–MissileTurnTowards.unitypackage (12.1 KB)

1 Like

Thanks for the package!:slight_smile: I will check this out! Thank you very much!

Creating a queue of player positions is a brilliant idea! I will check this out ASAP! Thanks so much, man!:slight_smile: