I need help moving a cube 1 unit to the right after 1 second until it reached its boundary on the right side, which is 4. So it would take it 4 seconds to reach 4 while only appearing 4 times at 1, 2, 3, and 4, starting at 0 on the x axis. Then move the cube to the other boundary which is -4. So it would take 8 seconds appearing at 4, 3, 2, 1, 0 ,-1, -2, -3, and -4. The back and forth movement will be done for the whole game. ,I have a block and I want to move it 1 unit to the right after 1 second until it reaches 4 then move it -1 unit after 1 second until it goes to -4 and so on. So it will take 4 seconds to reach 4 units to the right and then 8 seconds to -4 from 4, then another 8 seconds from 4 to -4 and so on.
Good day.
I think you need to know about Invoke function, which allow to execute a method placed in the same script in a fixed time.
For example:
float movingTime= 3;
OnTriggerEnter(Collider)
{
if (Collider.tag == MapBorder)
{
Invoke ("MoveCube", movingTime);
}
}
void MoveCube()
{
"Move Cube"
}
When cube reaches the border, will be moved after 3 seconds.
If helped, accept the answer! :D
You could set a timer in the Update function that get +1 every second.
Then if timer = 2 → cube position x = 2 and so on.
After 8 seconds you reset the timer to 0.
I guess there is a way better and shorter solution to this, but this comes to my mind first.