Search Unity

Smooth Rotation on NPCs

Discussion in 'Scripting' started by hmihalis, Jun 15, 2018.

  1. hmihalis

    hmihalis

    Joined:
    Sep 26, 2017
    Posts:
    4
    Hi there,

    I'm new to scripting and trying to adapt some scripts I purchased on the asset store for AI agents to have animations. My question is whether there is a way to adapt this line of code so that it performs the rotation smoothly over a 1 second, currently it just snaps them into the rotation:
    Code (CSharp):
    1. //Rotate agent towards LookAt
    2.                     gameObject.transform.LookAt(needsimNode.Blackboard.activeSlot.LookAt);
    Thank you for any help.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  3. hmihalis

    hmihalis

    Joined:
    Sep 26, 2017
    Posts:
    4
    Hi,

    Thanks for your response. It doesn't need to be exactly 1 second, just blend instead of snapping before entering the next state. Could you possibly show me what the line of code would be?
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    When you're new to coding, everything can seem tough.

    The RotateTowards takes parameters: current rotation, target rotation, and max degrees delta.

    Code (csharp):
    1. Vector3 direction = other.transform.position - transform.position;
    2. Quaternion rot = Quaternion.LookRotation(direction);
    3.  
    4. transform.rotation = Quaternion.RotateTowards(transform.rotation, rot, 10 * Time.deltaTime); // 10 could be a variable for rotation speed.
    Something like that.

    I would suggest that you follow some tutorials to help get started: https://unity3d.com/learn/beginner-tutorials
    https://unity3d.com/learn/tutorials/lets-try
    https://unity3d.com/learn/tutorials/s/scripting
     
  5. hmihalis

    hmihalis

    Joined:
    Sep 26, 2017
    Posts:
    4
    Thanks so much for you help and the links i'll check them out.