Hi
What am I doing wrong here?
My transform rotates on all 3 axis but the X axis (pulling the ship’s nose up or pushing it down) stops at 90 degrees in either direction. When it reaches 90 it stops and just spins on the other axis. I thought this was fairly simple and I’m not sure where it’s going wrong. Any ideas?
Transform player;
public float movementSpeed = 5f;
public int invert = -1;
float tiltSpeed = 100f;
float rollSpeed = 300f;
public float tiltAngle = 30f;
// Start is called before the first frame update
void Start()
{
player = GetComponent<Transform>();
}
// Update is called once per frame
void Update()
{
// Get left/right/up/down
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
// Update ship position
//transform.Translate(transform.forward * Time.deltaTime, Space.Self);
// Update ship rotation
Quaternion lookTowards;
if (horizontal != 0 || vertical != 0)
{
float rotationX = transform.rotation.eulerAngles.x;
float rotationY = transform.rotation.eulerAngles.y;
float rotationZ = transform.rotation.eulerAngles.z;
float hTurn = horizontal * tiltSpeed * Time.deltaTime;
float vTilt = vertical * tiltSpeed * Time.deltaTime;
float roll = horizontal * rollSpeed * Time.deltaTime;
rotationX += vTilt;
rotationY += hTurn;
rotationZ -= roll;
rotationZ = ClampAngle(rotationZ, -75, 75);
lookTowards = Quaternion.Euler(rotationX, rotationY, rotationZ);
transform.rotation = Quaternion.RotateTowards(transform.rotation, lookTowards, Mathf.Deg2Rad * 45f);
} else
{
lookTowards = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
transform.rotation = Quaternion.RotateTowards(transform.rotation, lookTowards, Mathf.Deg2Rad * 45f);
}
}
Cheers
Dan
