# Moving object orientation toward its movement in unity3d [duplicate]

I have a gameObject (camera) which is moving on the spline. I want to set it orientation towards it movement. Means, object should need to see where it is moving. I some thing tried this but this is not looking rightly! any advice?

 transform.rotation = Quaternion.LookRotation(transform.position);


I have also tried this

 transform.rotation = Quaternion.LookRotation(transform.forward);


## marked as duplicate by MichaelHouse♦May 18 '16 at 15:41

• what about Transfrom.LookAt() ? This rotates the transfrom so that the forward vector points towards the given world space coordinates ? – Uri Popov May 18 '16 at 10:05
• no it is not suitable i have tried this already. – Muhammad Faizan Khan May 18 '16 at 10:07

I wiped up a script that does what you want I think. Check it out.

public class OrientTowardsDirection : MonoBehaviour {

Vector3 prevLoc = Vector3.zero;
Vector3 moveDirection = Vector3.zero;
public Transform cube;
private float MaxTurnSpeed = 100;
void Start()
{
//code for simply moving the camera was here but was removed as per requested from Op.
}
void Update () {

Orient();
}
void Orient()
{
moveDirection = transform.TransformDirection(moveDirection);// Turns the direction from local to world space
moveDirection = (transform.position - prevLoc);// calculate direction vector;
Quaternion wanted_rotation = Quaternion.LookRotation(moveDirection);//create the rotation
transform.rotation = Quaternion.RotateTowards(transform.rotation, wanted_rotation,MaxTurnSpeed* Time.deltaTime);// apply the rotation with a max turn speed so its smooth
prevLoc = transform.position;// change our prev pos.
}
}


Explanations are comments in the code.

• what is Tweeng? – Muhammad Faizan Khan May 18 '16 at 11:25
• it just moves the camera towards a given transform. Its not important in your case but if you want you can read more about Tweeng – Uri Popov May 18 '16 at 11:28
• you mention a code snippet it using it so i have to add it in my code – Muhammad Faizan Khan May 18 '16 at 11:31
• what!?No you dont have to add it. What you need is the Orient() method. Tweeng just moves the camera for my own test. You can move the camera how ever you want. – Uri Popov May 18 '16 at 11:33
• so why did you add in the answer remove it – Muhammad Faizan Khan May 18 '16 at 11:37