I have this animated gif of my problem.

I have a sprite object with a box collider, and I'm trying to rotate it around its center.

My rotation script consists of this :

this.transform.Rotate (0, 0, Time.deltaTime * rotateSpeed);

and in the translation script this is the code :

this.transform.Translate (0, -tranSpeed * Time.deltaTime, 0);

but as you see in the gif it does not give me the effect I desire. Can anyone give me what am I supposed to do to be able to rotate it around its center? Thank you.

  • \$\begingroup\$ this.transform.Rotate(x,y,z,Space.Self) \$\endgroup\$ – dimitris93 Apr 14 '15 at 19:55
  • \$\begingroup\$ @Shiro Already had that code, did not help \$\endgroup\$ – Zee Apr 14 '15 at 19:57
  • \$\begingroup\$ that rotates an object around itsself. what do you mean by did not help ? what did you see and what did you expect to see ? \$\endgroup\$ – dimitris93 Apr 14 '15 at 19:58
  • \$\begingroup\$ It gives me the exact same problem as in the animated image, what I want is the object keeping its rotation around itself, while translating. \$\endgroup\$ – Zee Apr 14 '15 at 20:07
  • 3
    \$\begingroup\$ Have you tried renaming the sprite you are using for the long square black rectangle thing? \$\endgroup\$ – jgallant Apr 14 '15 at 20:31

From the docs on transform.Translate:

public void Translate(float x, float y, float z, Space relativeTo = Space.Self);

If relativeTo is left out or set to Space.Self the movement is applied relative to the transform's local axes. (the x, y and z axes shown when selecting the object inside the Scene View.)

So because you have not specified a space in which to apply the translation, it is applied relative to the object's current facing direction. As the object turns, so does its direction of travel.

You can fix this by setting transform.position directly, or telling transform.Translate to move relative to Space.World:

this.transform.Translate (0, -tranSpeed * Time.deltaTime, 0, Space.World);
  • \$\begingroup\$ Personally I recommend setting transform.position directly, but yes his explanation is completely correct \$\endgroup\$ – jhocking Apr 15 '15 at 17:00
  • \$\begingroup\$ A third method is to nest your GameObjects' transforms, so you have an inner part that's spinning, and an outer part that's translating, so that the rotation doesn't affect the coordinate system used for any other behaviours. \$\endgroup\$ – DMGregory Apr 15 '15 at 20:56

It seems like your transformations might be accumulating over time. As repeatedly "stepping forward" and "rotating around your axis" will cause the black bar to walk in circles. I'm unfamiliar with Unity, but there are some general approaches that might be applicable:

  1. You could try recalculating the object's position from its starting position, rather than from its current position.
  2. You could try to make the translation transformation independant from the object's orientation. Right now, it seems the translation is using the black bar's orientation to calculate the local X-axis. Instead of a local coordinate system, you'll want to use the world coordinate system.
  3. As a general remark, it is important to know that transformations are not commutative! This means that the order in which you apply them, can cause different results. As a general rule, transformations are mostly applied in this order: 1. scaling -> 2. rotation -> 3. translation. This makes sure your scaling and rotation don't cause a change in position. It also makes sure your scaling aligns with the local axes of the object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.