My “character” is a camera with a capsule collider. It has a simple movement script that uses translation and rotation to move around and look left/right.
When my character bumps into a wall, it will sometimes start turning on its own. How can I fix this?
You’re using a rigidbody which means your character controller is interacting with the world via the physics system which is why it’s rotating on its own.
Off the top of my head, you can probably control this by constraining the rigidbodies rotation around the Y axis and only changing the Y axis rotation manually via script.
This should prevent the physics system from changing your rotation around the Y axis, while not preventing manual changes to the Y rotation. Example code:
//this contains the objects rotation around each axis (x,y,z) in degrees:
transform.eulerAngles
//we can make a new set of euler angles with a minor adjustment (+1 to Y) by
//using the current euler angles found in transform.eulerAngles:
Vector3 newEulerAngles = new Vector3(
transform.eulerAngles.x,
transform.eulerAngles.y + 1,
transform.eulerAngles.z);
//NOTE: You can change the '+ 1' above to whatever you like. Such as:
//Input.GetAxis("Mouse X") * myMoveSpeed
//then we can create a new rotation Quaternion using the new euler angles
//and assign it to our current rotation
transform.rotation = Quaternion.Euler(newEulerAngles);
Look through the Transform class for more information.