I have a game in a 3D world space. The gameplay is in 2D. So, the player has position frozen on 1 axis and rotation frozen on 2 axes. Position freeze works fine. However, some of the players experience unwanted object rotation during the gameplay even with frozen constraints (I have never encountered such an issue during BETA-test)
Object unwanted weird rotation
Constraints while I test it for myself:
My code (Player movement)
public class Movement : MonoBehaviour
{
public Rigidbody rb;
public int clickForce = 500;
private Plane plane = new Plane(Vector3.up, Vector3.zero);
public float speed;
public Slider staminaBar;
private void Start()
{
Rigidbody rb = GetComponent<Rigidbody>();
rb.centerOfMass = Vector3.zero;
rb.inertiaTensorRotation = Quaternion.identity;
}
void FixedUpdate()
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Confined;
if (Input.GetMouseButton(0))
{
if (staminaBar.value > 0.25)
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float enter;
if (plane.Raycast(ray, out enter))
{
var hitPoint = ray.GetPoint(enter);
var mouseDir = hitPoint - gameObject.transform.position;
mouseDir = mouseDir.normalized;
rb.AddForce(mouseDir * clickForce);
}
Plane playerPlane = new Plane(Vector3.up, transform.position);
Ray rayy = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast(rayy, out hitdist))
{
Vector3 targetPoint = rayy.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
}
}
}
}