- Home /
How to freeze rotation, but stay relative to an object
I have object1(marble) and object2(player) this script is on the player object
void Update ()
{
player.transform.position = new Vector3(marble.transform.position.x, marble.transform.position.y + 0.45f, marble.transform.position.z);
transform.LookAt(transform.position + marble.rigidbody.velocity);
}
All works fine, except all I need is that the player object to not rotate along its x and z axis. How can I stop the player object to stay relative to the marble object, but to lock its x and z axis? Freezing the x and z rotation on the player Rigidbody has no affect. Can someone help?
Answer by robertbu · Jul 07, 2013 at 02:43 PM
The typical way to solve this kind of problem is to bring the LookAt() point down to the level of the game object:
void Update () {
player.transform.position = new Vector3(marble.transform.position.x, marble.transform.position.y + 0.45f, marble.transform.position.z);
Vector3 v3LookPos = transform.position + marble.rigidbody.velocity;
v3LookPos.y = transform.position.y;
transform.LookAt(v3LookPos);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613105628im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
rotation of child object inside another simultaneously rotated object 1 Answer
Global movement 0 Answers
Position from rotation problem. 1 Answer
Imported model is rotated 90 degrees off of straight, can't use any transforms 1 Answer
Freeze rigidbody position without affecting rotation - please help! 1 Answer