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

Resolved Making NavMesh Agent turn to camera after it gets to its destination

Discussion in 'Navigation' started by Hexolenn, Mar 24, 2023.

  1. Hexolenn

    Hexolenn

    Joined:
    Dec 27, 2021
    Posts:
    44
    I have a NavMesh Agent that I move randomly trough a room and I want it to turn to the camera after it stops its movement (I use SetDestination to move my agent) . The camera is static so I can both use cameras position or just set the agents Y rotation to 180. But if I use just those it will snap to that rotation(I have used transform.LookAt but as I said it snaps to it) but I want it to go with the same speed it uses for its normal roatations. What will be the best way to do this ?
     
    Last edited: Mar 24, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,120
    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
     
  3. Hexolenn

    Hexolenn

    Joined:
    Dec 27, 2021
    Posts:
    44
    I have found this video that does excally as I wanted. Maybe it will help you too.
    (There is code in the commends that lets you only turn in the Y axis if you need that too)