Question by
JayAlmighty1506 · Jan 11, 2021 at 10:12 PM ·
movement3dmovement scripttop-downspaceship
Player keeps spinning after collision
my ship controls work just fine normally but when it collides with something, it starts spinning, wich is normal, but the thing is, if i try to compensate the spin by spinning in the opposite direction, it doesn't work, its like the momentum of the spin just ignores the ships controls. This is my movement script. (note that this is a top-down kind of movement (no y position changes) no gravity is on, the ship is in space)
public class ShipControls : MonoBehaviour
{
private Rigidbody rb;
public float maxVelocity = 3f;
public float rotationSpeed = 0.4f;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void Update()
{
float yAxis = Input.GetAxis("Vertical");
float xAxis = Input.GetAxis("Horizontal");
ThrustForward(yAxis);
Rotate(transform, xAxis * rotationSpeed);
}
private void ClampVelocity()
{
float x = Mathf.Clamp(rb.velocity.x, -maxVelocity, maxVelocity);
float y = Mathf.Clamp(rb.velocity.y, -maxVelocity, maxVelocity);
rb.velocity = new Vector3(x, 0, y);
}
private void ThrustForward(float amount)
{
Vector3 force = transform.forward * amount;
rb.AddForce(force);
}
public void Rotate(Transform t, float amount)
{
t.Rotate(0, amount, 0);
}
}
Comment