Question by
Kastenessen · Jul 04, 2017 at 09:37 AM ·
3drotaterotate objectrotation axisrotate rotation
How come when I rotate my spaceship, it also rotates on the two other axis's? Please help?
Hello, I have a problem that I get a lot with working in 3d Space.
I have a spaceship with a constant forward speed and when I turn a corner I would like it to rotate on the z axis as well, but only to a certain angle. With the below code I get it to rotate but it rotates on all of the axis's instead of just one, and seems to ignore that I don't want it to rotate further than 45f on the z-axis.
// TO KEEP TRANS POS Y AT 0.
transform.position = new Vector3(transform.position.x, 0f, transform.position.z);
// ADD A CONSTANT FORWARD FORCE
rigBod.AddForce(transform.forward * constantThrustSpeed * Time.deltaTime);
// IF INPUT LEFT OR RIGHT THEN ROTATE SHIP.
if (Input.GetAxis(horizAxis) > 0)
{
transform.Rotate(new Vector3(0f, turningRotationSpeed * Input.GetAxis(horizAxis) * Time.deltaTime,0f));
if (transform.rotation.z < 45f)
{
transform.Rotate(new Vector3(0f, 0f, turningRotationSpeed * Input.GetAxis(horizAxis) * Time.deltaTime));
}
}
else
{
// LEVEL OUT
if (transform.rotation.z > 0f)
{
transform.Rotate(new Vector3(0f, 0f, turningRotationSpeed * -1f * Time.deltaTime));
}
}
if (Input.GetAxis(horizAxis) < 0)
{
transform.Rotate(new Vector3(0f, turningRotationSpeed * Input.GetAxis(horizAxis) * Time.deltaTime, 0f));
if (transform.rotation.z > -45f)
{
transform.Rotate(new Vector3(0f, 0f, turningRotationSpeed * Input.GetAxis(horizAxis) * Time.deltaTime));
}
}
else
{
// LEVEL OUT
if (transform.rotation.z < 0f)
{
transform.Rotate(new Vector3(0f, 0f, turningRotationSpeed * 1f * Time.deltaTime));
}
}
I am not sure what to do here. I would like the ship to level back out to z 0f and it does sometimes, but it seems to rotate on all of the axis's when turning instead of just z and ignores my < 45f or > - 45f rules.
Could somebody help please?
Comment