- Home /
Question by
sdocy503 · Jan 13, 2018 at 08:10 AM ·
collisionmovement script2d-game
How to stop a group of gameobjects from shaking when they arrive at their destination?
I'm working on a RTS. My units move as a group when you right click on the background. I'm trying to get rid of all the shaking they do when they arrive near their destination and try to crowd into the same spot. I've tried adding code to tell if they haven't moved significantly for a number of frames and if not tell them to stop moving but i can't quite get it to work. I'm wondering if anyone else has run into the same issue and has found a simple solution. Here's the code we're using to move the units:
void Update () {
Vector3 currentPos = transform.position;
int xdir = 0;
int ydir = 0;
float x = currentPos.x;
float y = currentPos.y;
float moveToX = globals.moveTo.x;
float moveToY = globals.moveTo.y;
if (x < moveToX)
{
xdir = 1;
}
else if(x > moveToX)
{
xdir = -1;
}
else if(x == moveToX)
{
xdir = 0;
}
if(y < moveToY)
{
ydir = 1;
}
else if(y > moveToY)
{
ydir = -1;
}
else if(y == moveToY)
{
ydir = 0;
}
if (xdir != 0 || ydir != 0)
{
transform.Translate(Time.deltaTime * speed * xdir, Time.deltaTime * speed * ydir, 0);
}
}
globals.moveTo is set to the position the user click on screen.
Comment