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!