transform.translate strange problem

So uh I have an empty gameobject with a Cube inside of it.

Empty gameobject has this code inside.

Basically, code is a cube that you control using WASD, where it can go forward and backwards based on rotations.

It works, doesent move in the transform.right…it goes all weird…

So whats wrong, its inconsistent

void Update () {
//do different movements based on controls
//forward and back
if (Input.GetKey(KeyCode.W))
{
this.gameObject.transform.Translate(gameObject.transform.right * CubeSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
this.gameObject.transform.Translate(-gameObject.transform.right * CubeSpeed * Time.deltaTime);
}
//rotate
if (Input.GetKey(KeyCode.A))
{
this.gameObject.transform.Rotate(0, -RotationSpeed, 0);
}
if (Input.GetKey(KeyCode.D))
{
this.gameObject.transform.Rotate(0, RotationSpeed, 0);
}
}

So is it transform.Translate with the problem?

Someone’s gonna come along and say it anyway so I might as well be the first to do it. Please post your scripts in code tags, makes things a whole lot easier to read.

Now on to your problem. You have not explained what you are trying to make and what you mean by “goes all weird” or what is “inconsistent”, so this is very little to go on. You have not established if this is a 3D or 2D game, and if it’s 2D is it a side scroller or top down. Not much can be done without this frame of reference for movement.

The script you have posted here seems pretty straight forward, the axis you have chosen to move in is right-ward which is the positive X axis, but you have used transform.right which means it is the local positive X axis of the object. Probably on purpose since you seem to want it to.

If it is 2D top down project, maybe you wanted it to rotate in the Z axis instead of the Y axis, all i can think of with the info here.

1 Like