Hello everyone, I am currently in the process of writing a spaceship controller, unfortunately I have a problem as soon as I fly a winekl of 90 degrees up or down, the spaceship starts to rotate uncontrollably
Maybe one of you has an idea what it could be, I actually expect that it flies a looping or just 90 degrees up but without rotating
using UnityEngine;
using Cinemachine;
public class SpaceshipController : MonoBehaviour {
[SerializeField] private InputReader inputReader;
[SerializeField] private CinemachineFreeLook freeLookCamera;
[SerializeField] private SpaceshipDataSO spaceshipData;
[SerializeField] private Transform shipModel;
[SerializeField] private Rigidbody spaceshipRigidbody;
[SerializeField] private float steeringSmoothing;
[SerializeField] private float thrustSmoothing;
private Vector3 rawInputSteering;
private Vector3 smoothInputSteering;
private float rawInputThrust;
private float smoothInputThrust;
private void OnEnable() {
inputReader.SteeringEvent += OnSteering;
inputReader.ThrustEvent += OnThrust;
inputReader.EnableGameplayInput();
}
private void OnDisable() {
inputReader.SteeringEvent += OnSteering;
inputReader.ThrustEvent += OnThrust;
}
private void OnSteering(Vector2 inputSteering) {
rawInputSteering = new Vector3(inputSteering.y, 0, -inputSteering.x);
}
private void OnThrust(float inputThrust) {
rawInputThrust = inputThrust;
}
private void Update() {
smoothInputSteering = Vector3.Lerp(smoothInputSteering, rawInputSteering, Time.deltaTime * steeringSmoothing);
smoothInputThrust = Mathf.Lerp(smoothInputThrust, rawInputThrust, Time.deltaTime * thrustSmoothing);
freeLookCamera.m_XAxis.Value = spaceshipData.CameraTurnAmount * smoothInputSteering.z;
freeLookCamera.m_Lens.FieldOfView = 40 + (smoothInputThrust * 10);
}
private void FixedUpdate() {
spaceshipRigidbody.velocity = transform.forward * spaceshipData.ThrustAmount * (Mathf.Max(smoothInputThrust, 0.2f));
Vector3 newTorque = new Vector3(smoothInputSteering.x * spaceshipData.PitchSpeed, -smoothInputSteering.z * spaceshipData.YawSpeed, 0);
spaceshipRigidbody.AddRelativeTorque(newTorque);
spaceshipRigidbody.rotation = Quaternion.Slerp(spaceshipRigidbody.rotation, Quaternion.Euler(new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, 0)), 0.5f);
shipModel.localEulerAngles = new Vector3(smoothInputSteering.x * spaceshipData.LeanAmountY, shipModel.localEulerAngles.y, smoothInputSteering.z * spaceshipData.LeanAmountX);
}
}