Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in '2D' started by drpelz, Jan 10, 2023.

  1. drpelz

    drpelz

    Joined:
    Dec 7, 2017
    Posts:
    69
    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):

    Code (CSharp):
    1. protected Transform m_target;//player
    2.     public Vector3 m_targetOffset;//Use this if the center of the object is not what you target
    3.     public AnimationCurve m_distanceInfluence = AnimationCurve.Linear(0, 1, 1, 1);//Increase the intensity depending on the distance
    4.     public float m_searchRange = 10f;//Range within the missile will search a new target
    5.     public float m_guidanceIntensity = 5f;//Intensity the missile will be guided with
    6.     protected bool m_lookDirection = true;
    7.     protected Vector3 m_lookDirectionOffset;
    8.     public float m_searchRange = 10f;//Range within the missile will search a new target
    9.  
    10.  
    11.     void FixedUpdate() {
    12.        GoToTarget();
    13.     }
    14.  
    15.     protected virtual void GoToTarget() {
    16.         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);
    17.         m_rigidbody.velocity = Vector2.ClampMagnitude(m_rigidbody.velocity + (Vector2)m_direction * m_guidanceIntensity, m_rigidbody.velocity.magnitude);
    18.      
    19.         if (m_rigidbody.velocity != Vector2.zero)
    20.         {
    21.             m_forward = m_rigidbody.velocity.normalized;
    22.  
    23.             if (m_lookDirection)
    24.             {
    25.                 this.transform.eulerAngles = new Vector3(0, 0, -Mathf.Atan2(m_rigidbody.velocity.x, m_rigidbody.velocity.y) * Mathf.Rad2Deg);
    26.                 this.transform.Rotate(m_lookDirectionOffset);
    27.             }
    28.         }
    29.     }
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,869
    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.
     
    drpelz likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Smoothing movement between any two particular values:

    https://forum.unity.com/threads/beginner-need-help-with-smoothdamp.988959/#post-6430100

    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: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4

    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.
     

    Attached Files:

    drpelz likes this.
  4. drpelz

    drpelz

    Joined:
    Dec 7, 2017
    Posts:
    69
    Thanks for the package!:) 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!:)
     
    Last edited: Jan 10, 2023