- Home /
Rigidbody rotation constantly spins upon collision.
Hey! I've recently started learning Unity and am just now learning about physics and how to code with them in mind.
[Context] I have created a sphere that moves with Add Force. The X and Z axis are frozen but I have the Y axis unfrozen for gameplay purposes. Interpolation is turned to interpolate. The camera rotates around the sphere in third person and the force is added to the sphere using both key inputs and the cameras position relative to the sphere. Ther sphere is on a terrain that has hills. The mass is 1.85 but can be changed if need be.
---
The problem I am facing is that whenever the sphere collides with the terrains hills, the sphere starts to rapidly spin out of control, which makes the camera jitter. I have read online that affecting the ridigbody's mass would lessen the spin or completely fix the issue; however, that has not solved the rapid spin. Here is my code.
using UnityEngine;
public class cameraController : MonoBehaviour
{
public GameObject Player;
public Transform cam, player;
public float mouseSens;
public float speed;
private float mouseY, mouseX;
private Rigidbody rb;
private Vector3 rotationVector;
void Start()
{
rb = Player.GetComponent<Rigidbody>();
Cursor.lockState = CursorLockMode.Locked;
Vector3 rotationVector = new Vector3(0, mouseX, 0);
//offset = (cam.transform.position - target.position).normalized * camDistance;
}
void FixedUpdate()
{
Rotate();
}
public void Rotate()
{
mouseX += Input.GetAxis("Mouse X") * speed;
mouseY += Input.GetAxis("Mouse Y") * speed;
mouseY = Mathf.Clamp(mouseY, -35, 89);
transform.LookAt(cam);
cam.rotation = Quaternion.Euler(mouseX, mouseY, 0);
Quaternion deltaRotation = Quaternion.Euler(rotationVector * Time.deltaTime);
rb.MoveRotation(rb.rotation * deltaRotation);
}
}
Help and insight would be greatly appreciated by this newbie!
Answer by kubajs · Mar 30, 2020 at 10:19 PM
Try to set Rigidbody angular drag to some higher value if this is acceptable.
Answer by yosheDev · Mar 30, 2020 at 10:16 PM
Update for anyone with a similar problem: Freezing the Y Rotation of the object can fix this problem. Unfortunately, I need the Y Rotation for my game. I am currently investigating ways I could create a rotation while still freezing the Y rotation on my Rigidbody. Once again, open to input and advice.
Answer by slake_it · Jan 12, 2021 at 04:13 PM
this happened to me with Rigidbody2D, The issue turned out to be my object was rotated 180 degrees around the y-axis but I guess since the physics 2D system knows nothing about rotation around y-axis, that caused weird behavior.
if you're having this in 3D rigidbody, then maybe check the scale of your object, check if it's negative
Your answer
Follow this Question
Related Questions
Rigidbody NavMeshAgent Causing Crazy Collisions with Player. 0 Answers
Knock down moving target ridigbody from collision. 1 Answer
How may I observe expected physical interactions while using Rigidbody.MoveRotation()? 1 Answer
Object Flies into Air Upon Collision, or goes through hill depending on isKinematic settings 1 Answer
Rigidbody doesn't fall when platform below it shrinks to nothing. 1 Answer