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);
}
}
}