Funy things ( or not ) happens.

the question will be straight forward , I use a script to move a Cube Down of 1 unit to go back at its original position at X speed.

AND IT WORK.

the thing is , when i whant it to go left, right or up first. the cube go in the said destination but just keep going and never come back.

Ive tried to change direction values in the script and also the rotation of the cube itself. Both way gave me the same result.

Anyone have any idea what could it be ?

There’s a bug in your script.

If you post it, we can help you with that.

1 Like

Yup you’Re right !

using UnityEngine;
using System.Collections;

public class movement : MonoBehaviour
{
    float speed;
    Vector3 direction;
    float min;
    float max;
    float units = 1.0f;

    void Start()
    {
        //Set max to CURRENT position on Y axis, and set min to current minus units (2.0 in this example).
        max = transform.position.y;
        min = transform.position.y - units;

        //Set first direction to be down
        direction = Vector3.down;
    }

    void Update()
    {
        //Change speed depending on direction of object translation.
        if(direction == Vector3.down)
        {
            speed = 1.0f;
        }

        else if(direction == Vector3.up)
        {
            speed = 1.0f;
        }




        //Use transform.Translate to move the current direction and current speed.
        transform.Translate(direction * speed * Time.deltaTime);




        //Change direction if object has reached min or max position on the Y axis (up and down).
        if(transform.position.y >= max)
        {
            direction = Vector3.down;
        }

        if(transform.position.y <= min)
        {
            direction = Vector3.up;  
        }
    }
}

Your script works. If you want to go right/left, you’ll want to check position.x instead of position.y.

Same thing happen… Thanks for the hint tho. I thought for a sec that it might just be it !

I assume you want to change your speed here, instead of having it the same in both ifs :slight_smile:

You think that the fact both speed are the same could have an effect on the behaviour ? Im not sure to understand what you mean ?