This question already has an answer here:

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

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ what about Transfrom.LookAt() ? This rotates the transfrom so that the forward vector points towards the given world space coordinates ? \$\endgroup\$ – Uri Popov May 18 '16 at 10:05
  • \$\begingroup\$ no it is not suitable i have tried this already. \$\endgroup\$ – Muhammad Faizan Khan May 18 '16 at 10:07
  • \$\begingroup\$ Please don't ask the same question over and over. Update your other question if the answers don't answer your question. \$\endgroup\$ – MichaelHouse May 18 '16 at 15:42

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 () {

    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.

  • \$\begingroup\$ what is Tweeng? \$\endgroup\$ – Muhammad Faizan Khan May 18 '16 at 11:25
  • \$\begingroup\$ 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 \$\endgroup\$ – Uri Popov May 18 '16 at 11:28
  • \$\begingroup\$ you mention a code snippet it using it so i have to add it in my code \$\endgroup\$ – Muhammad Faizan Khan May 18 '16 at 11:31
  • \$\begingroup\$ 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. \$\endgroup\$ – Uri Popov May 18 '16 at 11:33
  • \$\begingroup\$ so why did you add in the answer remove it \$\endgroup\$ – Muhammad Faizan Khan May 18 '16 at 11:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.