Error when make the objects both MoveLeft and Spinning in Unity

I’m trying to make objects fly to the left while spinning themselves.

Here is the MoveLeft script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveLeft : MonoBehaviour
{
    private float moveLeftSpeed = 10;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(Vector3.left * Time.deltaTime * moveLeftSpeed);
    }
    
}

And there is the SpinObjects script:

using System.Collections.Generic;
using UnityEngine;

public class SpinObjectsX : MonoBehaviour
{
    public float spinSpeed = 50;

    // Update is called once per frame
    void Update()
    {
        transform.Rotate(new Vector3(0, Time.deltaTime * spinSpeed, 0));
    }
}

I expect the object's movement will look like this.

I expect the object’s movement will look like this.

But when I use both scripts, the object moves very weird, it is still spinning itself but instead of moving to the left, it spinning around something...

But when I use both scripts, the object moves very weird, it is still spinning itself but instead of moving to the left, it spinning around something…

>Solution :

Both Translate and Rotate per default work in local space of according object unless you explicitly pass in Space.World as additional last parameter.

So, after rotating your object around the Y-axis it’s local left vector is now rotated as well and pointing somewhere else.

=> When you do the local space translation towards left of the object it doesn’t actually move left in the world.

In order to move left in absolute world space you want to use

transform.Translate(Vector3.left * Time.deltaTime * moveLeftSpeed, Space.World);

Leave a Reply