Hello, i need help at cube position!


I took position “x” from the cube, I want to know when “x” is increasing or decreasing. if “x” decreasing variable “direction” will be -1, if “x” increase will be 1;

i’m still new to unity but i think that what you are looking for is to check the x position of the cube in the update and store it in variable and in the next frame compare it to that variable .

           void update{}  
            cubexPos = cube.transform.position.x;           
            if (cubexPos < personagem.transform.position) 
                        direction += 1 `    }
            else if (cubexPos >  personagem.transform.position)
            { ``                       
`` direction -=`1    
                {print("cube not moving on x axis")}

i did not test this code by the way

Hello @Al1aska !

I’m not entirely sure what you are asking in your question, but it sounds like you’re wanting to know how to go about tracking whether or not a specific GameObject’s X position is increasing or decreasing overtime.
The easiest way to do this that I can think of is to create a script which would keep track of the GameObject’s current position, and previous position on the last frame. From there, every update, you can simply take the difference of the two positions. If the difference of the current X position and previous X position is a negative number, then it means the GameObject is decreasing in it’s X position - and vice versa.

Vector3 previousPosition, currentPosition;

public void Update() {
     previousPosition = currentPosition;
     currentPosition = transform.position;

public int GetDirection() {
     return currentPosition.x - previousPosition.x;

The above is just an example, tweak it to your needs. Hopefully this helps to answer your question!