Skip to main content
Removing misleading constant — better to use just the speed variable
Source Link
DMGregory
  • 136.3k
  • 22
  • 247
  • 373

When in doubt, be sure to read the documentation for the methods you're using.

Here you'll find out that if you give transform.Translate three float parameters and no Space, it defaults to translating in local coordinates. ie. in this case, it moves along the object's Z axis, not the world's Z axis.

Once you rotate the plane, that direction might point up or down, changing the global y coordinate of the object.

Because this change is going through the Transform, it completely bypasses the constraints you imposed on the Rigidbody.

So, possible fixes:

  • Specify Space.World if you want to move forward in world space, not local space

    transform.Translate(0, 0, 10 * translationSpeed * Time.deltaTime, Space.World);

  • If you want to steer left/right, just not up/down, compute a desired translation in world space, then clamp-out the y:

    var desiredTravel = transform.forward * translationSpeed * Time.deltaTime; desiredTravel.y = 0f; transform.Translate(desiredTravel, Space.World);

  • Or, route all movement via the Rigidbody by setting its velocity instead of teleporting with Transform changes. This lets the physics engine maintain the constraints you've configured:

    rigidbody.velocity = transform.forward * translationSpeed;

When in doubt, be sure to read the documentation for the methods you're using.

Here you'll find out that if you give transform.Translate three float parameters and no Space, it defaults to translating in local coordinates. ie. in this case, it moves along the object's Z axis, not the world's Z axis.

Once you rotate the plane, that direction might point up or down, changing the global y coordinate of the object.

Because this change is going through the Transform, it completely bypasses the constraints you imposed on the Rigidbody.

So, possible fixes:

  • Specify Space.World if you want to move forward in world space, not local space

    transform.Translate(0, 0, 10 * translationSpeed * Time.deltaTime, Space.World);

  • If you want to steer left/right, just not up/down, compute a desired translation in world space, then clamp-out the y:

    var desiredTravel = transform.forward * translationSpeed * Time.deltaTime; desiredTravel.y = 0f; transform.Translate(desiredTravel, Space.World);

  • Or, route all movement via the Rigidbody by setting its velocity instead of teleporting with Transform changes. This lets the physics engine maintain the constraints you've configured:

    rigidbody.velocity = transform.forward * translationSpeed;

When in doubt, be sure to read the documentation for the methods you're using.

Here you'll find out that if you give transform.Translate three float parameters and no Space, it defaults to translating in local coordinates. ie. in this case, it moves along the object's Z axis, not the world's Z axis.

Once you rotate the plane, that direction might point up or down, changing the global y coordinate of the object.

Because this change is going through the Transform, it completely bypasses the constraints you imposed on the Rigidbody.

So, possible fixes:

  • Specify Space.World if you want to move forward in world space, not local space

    transform.Translate(0, 0, translationSpeed * Time.deltaTime, Space.World);

  • If you want to steer left/right, just not up/down, compute a desired translation in world space, then clamp-out the y:

    var desiredTravel = transform.forward * translationSpeed * Time.deltaTime; desiredTravel.y = 0f; transform.Translate(desiredTravel, Space.World);

  • Or, route all movement via the Rigidbody by setting its velocity instead of teleporting with Transform changes. This lets the physics engine maintain the constraints you've configured:

    rigidbody.velocity = transform.forward * translationSpeed;

Source Link
DMGregory
  • 136.3k
  • 22
  • 247
  • 373

When in doubt, be sure to read the documentation for the methods you're using.

Here you'll find out that if you give transform.Translate three float parameters and no Space, it defaults to translating in local coordinates. ie. in this case, it moves along the object's Z axis, not the world's Z axis.

Once you rotate the plane, that direction might point up or down, changing the global y coordinate of the object.

Because this change is going through the Transform, it completely bypasses the constraints you imposed on the Rigidbody.

So, possible fixes:

  • Specify Space.World if you want to move forward in world space, not local space

    transform.Translate(0, 0, 10 * translationSpeed * Time.deltaTime, Space.World);

  • If you want to steer left/right, just not up/down, compute a desired translation in world space, then clamp-out the y:

    var desiredTravel = transform.forward * translationSpeed * Time.deltaTime; desiredTravel.y = 0f; transform.Translate(desiredTravel, Space.World);

  • Or, route all movement via the Rigidbody by setting its velocity instead of teleporting with Transform changes. This lets the physics engine maintain the constraints you've configured:

    rigidbody.velocity = transform.forward * translationSpeed;