SHAI
SHAI

Reputation: 859

Why is Object moving by its own?

I'm new to Unity3d, I want my platform to move on the x-axis only, but when I run the game I can see changes of both x and z directions instead of moving left, the object moves left and up.

This is my code:

    protected virtual void Update () {
    transform.Translate(Vector3.left * (objectSpeed * Time.deltaTime));}

If I apply this code on a simple cube it works fine (left only), but when I apply it on my object- it goes left and up. Here is my object:

enter image description here

Upvotes: 0

Views: 889

Answers (1)

Dávid Florek
Dávid Florek

Reputation: 571

You are using transform.Trnaslate to move your object. It uses the default value for relativeTo, which is Space.Self. This means, that it will transform the object's position relative to it's rotation. Your object has slight rotation on y axis, that causes the problem. Either set relativeTo to Space.World or set the object's rotaiton to 0.

Here is more information in Unity Documentation.

Upvotes: 3

Related Questions