Hello, I’m trying to make a game where you snowboard down a hill. It’s a simple game but the snowboard tends to spin around and is very hard to control. I’m trying to make a script that limits the rotation in the x axis (so the board doesn’t flip over) within a certain range, and also limit the rotation in the y axis (left and right). I’ve looked at a few other questions but they don’t seem to work that well. I’m new to Unity so I’m still getting familiar with it all. Here is what I have so far:
rigidbody.rotation.SetLookRotation (transform.forward);
var planarVel = new Vector2 (rigidbody.velocity.x, rigidbody.velocity.z);
var planarForward = new Vector2 (transform.localRotation.x, transform.localRotation.z);
var deviation = Vector2.Angle (planarVel, planarForward);
if (rigidbody.velocity.magnitude > 5 && deviation>45f) {
Quaternion velQuat = new Quaternion();
velQuat.eulerAngles = rigidbody.velocity;
rigidbody.rotation = Quaternion.RotateTowards(transform.localRotation, velQuat, 2f);
}
This code is only for the y axis rotation lock. I don’t want to completely lock the rotation with the check box in the inspector as I do want to allow for some flexibility in movement.