Hi all,
I’m fairly new to using Unity, but have been in game development for some time now, mostly on the creative side.
Anyhow, I’m fiddling around trying to make a 3d object move and tilt rotate, like a spaceship would in a shoot em up game.
I’ve got the movement with inertia and tilt forward fine (using x euler axis), however if I try to tilt sideways, it doesn’t seem to work. Using either Y or Z euler axis will yield the same result and it will just rotate it to the left or right, instead of tilt left or right.
Thanks in advance for your help - Unity is pretty boss so far!
Code (to note, I’ve tried both .z and .y and they show different values in the debugger, just visually it appears the same):
if(Input.GetKey(KeyCode.D)) // Moving Right
{
gameObject.rigidbody.AddForce(Vector3.right * 50, ForceMode.Acceleration);
curAngles.z += rotationSpeed * Time.deltaTime;
}
if(Input.GetKey(KeyCode.A)) // Moving Left
{
gameObject.rigidbody.AddForce(Vector3.left * 50, ForceMode.Acceleration);
curAngles.z -= rotationSpeed * Time.deltaTime;
}
if(Input.GetKey(KeyCode.W)) // Moving Forward
{
gameObject.rigidbody.AddForce(Vector3.forward * 50, ForceMode.Acceleration);
curAngles.x -= rotationSpeed * Time.deltaTime;
}
if(Input.GetKey(KeyCode.S)) // Moving Back
{
gameObject.rigidbody.AddForce(Vector3.back * 50, ForceMode.Acceleration);
curAngles.x += rotationSpeed * Time.deltaTime;
}
// Ship Tilt Rotation
curAngles.x = Mathf.Clamp(curAngles.x, -5, 5);
curAngles.z = Mathf.Clamp(curAngles.z, -95, 95);
Debug.Log(curAngles);
transform.eulerAngles = initialAngles + curAngles;