rotate rigid body based on transform.velocity?

using c#, I’ve been searching for how to change the rotation to direction player is moving. I’ve found several solutions, but this looks the best:


error CS0120: An object reference is required to access non-static member `UnityEngine.Quaternion.SetLookRotation(UnityEngine.Vector3)’

When I try using this code, i get the following errors:

I also try this with the same error:

Quaternion myQ = new Quaternion.SetLookRotation(velocity);
transform.rotation = myQ;

error CS0426: The nested type SetLookRotation' does not exist in the type UnityEngine.Quaternion’

Not sure exactly what you mean but try this.

    void Update () 
		transform.rotation = Quaternion.LookRotation(rigidbody.velocity);
		print (transform.rotation);

Otherwise, if you’re wanting the object’s rotation to only move on Y axis and act like a “turret”…

using UnityEngine;
using System.Collections;

public class EXAMPLE : MonoBehaviour 
	public Transform player;
	Vector3 target;
	void Update () 
		target = new Vector3(player.position.x, this.transform.position.y, player.position.z);

The reason you have these errors, is that SetLookRotation() of class Quaternion is a instance method, meaning you have to call it on an instance of that class. There is also a class method LookRotation() which does the same thing, you just have assign the return value somewhere.

So you can use



transform.rotation = Quaternion.LookRotation(velocity);