- Home /
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:
transform.rotation=Quaternion.SetLookRotation(velocity);
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'
Answer by clunk47 · Dec 16, 2012 at 08:51 PM
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);
transform.LookAt(target);
}
}
Sorry if this doesn't help, I'll need a more detailed explanation of what you're going for. Will be happy to help either way :)
this helped a lot, thank you. I'm still new and learning so thank you for clarifying.
Always happy to help, I voted your question up +1 for being a good question :)
Answer by aleksigron · Dec 17, 2012 at 01:11 AM
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.SetLookRotation(velocity);
or
transform.rotation = Quaternion.LookRotation(velocity);
Your answer
![](https://koobas.hobune.stream/wayback/20220613085524im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
i want to get my to cube to rotate 90 degrees on the x-axis as it jumps 0 Answers
Making my GambeObject rotate to face the direction of movement, smoothly. 0 Answers
Why if I Rotate only 1 axis, it rotates the other 2 axis also? 2 Answers
OnDrawGizmos Debug.DrawLine position is pinching when rotated. 0 Answers