Smoothing a rotation

I need to smooth a “LookAt”
Or I need a script that make ALL the rotation smooth.

 #pragma strict
public var theCamera : Transform;
private var movementSpeed : float = 5.0;
private var myTransform : Transform;
function Start()
{
     myTransform = this.transform;
}
function Update()
{
  
     // get inputs
     var inputX : float = Input.GetAxis( "Horizontal" );
     var inputY : float = Input.GetAxis( "Vertical" );
    
     // get current position, then do calculations
     var moveVectorX : Vector3 = theCamera.forward * inputY;
     var moveVectorY : Vector3 = theCamera.right * inputX;
     var moveVector : Vector3 = ( moveVectorX + moveVectorY ).normalized * movementSpeed * Time.deltaTime;
    
     // update Character position
     myTransform.position = myTransform.position + Vector3( moveVector.x, 0.0, moveVector.z );
    
     // and rotation I NEED THE SMOOTH HERE
     myTransform.LookAt( myTransform.position + Vector3( moveVector.x, 0.0, moveVector.z ) );
    
    
}

This is a kind of controller for third person, but the rotation is instant and I want to make it smooth, but I don’t know how. Any ideas?
Please help!

Quaternion.RotateTowards(transform.rotation, put the result of lookat here, speed at which to rotate);

Hope this is helpful.

2 Likes

Hi thanks, what do you mean with “result of lookat”, what should I write here?

Quaternion.LookAt(position to look at - current position) this will give you the quaternion to pass through to rotate towards. Ill write a quick example when im home. On a mobile atm.

1 Like

Well, Thank you SO much for your time
I’ll wait for your example, please be specific, I’m not an expert.

Sorry for the late reply, completely forgot when i got home.

I was picturing something like this although I get strange behaviour due to my scene not being setup up for this sort of thing.

If it seems strange to you aswell let me know & ill throw together something a bit more complex with comments so you know how it works, it would be in c# though as I am much faster in that (can port it to java if needed later).

#pragma strict

public var theCamera : Transform;
public var movementSpeed : float = 5.0f;
public var turnSpeed : float = 10.0f;

function Update () {
    var inputX : float = Input.GetAxis("Horizontal");
    var inputY : float = Input.GetAxis("Vertical");
   
    var moveVectorX : Vector3 = theCamera.forward * inputY;
    var moveVectorY : Vector3 = theCamera.right * inputX;
    var moveVector : Vector3 = (moveVectorX + moveVectorY).normalized * movementSpeed * Time.deltaTime;
   
    var lastPos : Vector3 = transform.position;
    transform.position = transform.position + Vector3(moveVector.x, 0.0f, moveVector.z);
    transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(lastPos - transform.position), turnSpeed);
}
1 Like

Thank you again for your time, but I have 2 problems with your script:

  • The rotation is smooth but If I move my player for example in the left , he go in the left but he is watching the opposite side (the right).
  • If I don’t press a key, the rotation returns: x = 0, y = 0, z = 0.
    Have you tested it? It worked? Please let me know
    Thanks a lot for the smoothing problem any way.
    If you don’t want to help me more, there is no problem.
    I’ll try to fix it anyway by my self.
    Thanks very much again for the help!

I solved it writing the “-” in moveVector.x, and in moveVector.z
transform.position = transform.position + Vector3(-moveVector.x, 0.0f, -moveVector.z);

Now remains the problem of the rotation that return x = 0 y = 0 z = 0 if I don’t press a key.

The reason why its 0 without input is because you multiply the forward vector by inputX & inputY. These values are 0 without input so you are multiplying a value by 0 which cause the result to be 0. Something along the lines of the below should solve the issue:

//put this outside of any functions
private var lookRot : Quaternion;

// put this after the decleration of moveVector
If(moveVector.sqrMagnitude != 0.0f){
var Lastpos : Vector3 = transform.position;
transform.position= transform.position+ Vector3(moveVector.x, 0.0f, moveVector.z);
lookRot =
=Quaternion.LookRotation(lastPos - transform.position);
}
transform.rotation = Quaternion.RotateTowards(transform.rotation, lookRot, turnSpeed;

You will probably need a start function to calculate the initial rotation, but otherwise this should take care of the 0 issue.

1 Like

Now the script works perfectly!
Thanks very much for all the help lordconstant

Np, happy to help

1 Like