Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading