'how can i make the object move on that path in unity2D? [closed]
Solution 1:[1]
There are numerous ways to achieve that. A good starting point is a state machine.
- You have a state which decides in which direction you are currently moving.
- After a certain time (or condition is met) you change the state.
sample code:
{
private int state = 1; // 1 means moving down and right, 2 means moving up.
// you could also use an enum for this.
private float time = 0; // how long the enemy is already moving in this direction
void Update() {
if (state == 1) {
// move down right
transform.position = transform.position + new Vector(1,-1,0) * Time.deltaTime;
} else if (state == 2) {
// move up
// TODO
}
// Add additional states here...
time += Time.deltaTime; // track how long you are in this state
// Change the condition here...
if (time > TIME_MOVING_IN_SAME_DIRECTION) {
time = 0; // reset time
// set next state:
if (state == 1) state = 2;
else if (state == 2) state = 1;
// Add additional changes between the states here if necessary
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kenten Fina |

