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