I need the lock the rotation of the player object being controlled, and other answers I have found have not been working. I made a new script just for locking the rotation. My other script only controls the X and Y axes, and doesn’t modify the rotation at all, and I would like a C# script line that will lock the object’s rotation to one value, and will not change, even if there is another object bumping into it.
2 Likes
This is one way to do it:
using UnityEngine;
public class helpscript : MonoBehaviour
{
public float fixedRotation = 5;
void Update ()
{
Vector3 eulerAngles = transform.eulerAngles;
transform.eulerAngles = new Vector3( eulerAngles.x , fixedRotation , eulerAngles.z );
}
}
It sounds to me like “Freeze Rotation” on the rigidbody would fix this.
For an alternate solution to the answer, you can lock rotation every frame by using. Freeze Rotation probably works better for performance on rigid bodies.
void Update ()
{
transform.rotation = Quaternion.identity;
}