Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved A simple code to turn something towards another thing with lerp

Discussion in 'Editor & General Support' started by mfatihbarut, Apr 16, 2021.

  1. mfatihbarut

    mfatihbarut

    Joined:
    Apr 11, 2018
    Posts:
    1,059
    Hi all,
    I spent realy (unnecessary) long time to find this code.
    Therefore wanted to share it here.
    This simple line of code turn your object to another object with learp with defining speed

    Code (CSharp):
    1.     void TurnTowardsWithLerp()
    2.     {
    3.         float speed = 1;
    4.         GameObject enemy = null;
    5.         // Determine which direction to rotate towards
    6.         Vector3 targetDirection = enemy.transform.position - transform.position;
    7.         transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(targetDirection), Time.deltaTime * speed);
    8.     }