- Home /
How to move an object back/forth on a set path?
Hello, I am moving an enemy object with a collider 2D and rigidbody2D back and forth on top of a box collider 2D platform. The enemy moves back and forth without issue sometimes. Other times, the enemy seems to get stuck and just vibrate back and forth. Here is the code I am using. Any suggestions would be awesome!:
public float speed;
public float distance;
private float xStartPosition;
void Start () {
xStartPosition = transform.position.x;
}
void Update () {
if (transform.position.x < xStartPosition || transform.position.x > xStartPosition + distance)
{
speed *= -1;
}
transform.position = new Vector2(transform.position.x + speed * Time.deltaTime, transform.position.y);
}
}
Answer by Bunny83 · Oct 06, 2014 at 01:25 PM
Well, the reason is quite simple. Imagine Time.deltaTime is quite large when you read on end of your path. That means you move a certain amount over your turning point. The next frame if detects that and reverts the direction. Now if the Time.deltaTime is lower this time you might not move far enough to get back into the valid area so you're still outside your turning mark. That's why you change direction every frame.
You have to do the check selective for each side:
public float speed;
public float distance;
private float xStartPosition;
void Start () {
xStartPosition = transform.position.x;
}
void Update () {
if ((speed < 0 && transform.position.x < xStartPosition) || (speed > 0 && transform.position.x > xStartPosition + distance))
{
speed *= -1;
}
transform.position = new Vector2(transform.position.x + speed * Time.deltaTime, transform.position.y);
}
Thist should work as you only switch direction when you actually move in the wrong direction
Thanks Bunny83. This seems to have taken care of the issue! I've marked the answer as correct and I'll give it another play-through to be sure :)
I tried this script and it works fine but it always start moving to the right. i did some $$anonymous$$or changes to the script so the platform starts moving when i step on it and i want it to go to the left first. I tried making the distance float negative or rotating the platform but it doesn't work. any idea how to fix it?
Answer by Seneral · Oct 06, 2014 at 12:46 PM
This is quite simple, Imagine your character outside of this boundaries: Every frame the direction would be swirched, write something like this:
void Update () { int dir = 1; if (transform.position.x < xStartPosition || transform.position.x > xStartPosition + distance) { dir *= -1; } transform.position = new Vector2(transform.position.x + (speed*dir) * Time.deltaTime, transform.position.y); }
Note this is pseudocode and also not very clean. I wouldn't set the transform but move it. I'll let you do it;)
Thanks Seneral, but the direction change isn't really the problem (that works with the code above). The issue is that sometimes the movement just stops and I can't quite pin down why.
That's what I meant to describe, switching the direction every frame results in getting stuck and "vibrating". Bunny83 explained it way better:)