- Home /
How to remove jitters from moving game object
I'm working on a scene where I have a few game objects("ships") that have basically two objectives: move towards a point, avoid other ships.
The code I have works in a general sense. I also have some code, following the boids algorithm, to make the ships try to move towards the center of the formation. This gives a nice effect of all the ships following a tracking object, not colliding with each other, and by trying to get to the center of the formation there's a little variability that keeps things from looking too static when the tracking object is still.
What I can't get rid of though are jitters in the ships movement. It looks like the move to point, and avoid nearest ship code are conflicting so they bounce back and forth rather than moving smoothly towards and away from each other. Any suggestions would be appreciated.
public float speed;
public float maxSpeed;
private Vector3 targetV;
public bool moveCenter = true;
public bool avoidOtherShips = true;
public bool followTracker = true;
//Alignment
public float flockToCenterFactor;
//Separation
public float avoidDistance;
public float avoidFactor;
//Follow Tracker
public float trackerFactor;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
GameObject[] ships = GameObject.FindGameObjectsWithTag("Player");
targetV = FollowTracker() + AvoidNearestShip(ships) + MoveToCenter(ships);
targetV = Vector3.ClampMagnitude(targetV * speed, maxSpeed);
transform.position += targetV * Time.deltaTime;
//transform.position = Vector3.Lerp(transform.position, transform.position + targetV, Time.deltaTime);
}
private Vector3 MoveToCenter(GameObject[] ships) {
if (moveCenter) {
Vector3 center = new Vector3();
int numShips = ships.Length - 1;
foreach (GameObject ship in ships) {
if (gameObject != ship) {
center += ship.transform.position;
}
}
return ((center/numShips) - transform.position) * flockToCenterFactor;
}
return new Vector3();
}
private Vector3 AvoidNearestShip(GameObject[] ships) {
if (avoidOtherShips) {
Vector3 avoidV = new Vector3();
int numShips = 0;
float shortestDistance = avoidDistance;
foreach (GameObject ship in ships) {
if (gameObject != ship) {
float shipDistance = Vector3.Distance(transform.position, ship.transform.position);
if (shipDistance < shortestDistance) {
shortestDistance = shipDistance;
avoidV = ship.transform.position;
numShips++;
}
}
}
if (numShips > 0 ) {
return (transform.position - avoidV) * avoidFactor;
}
}
return new Vector3();
}
private Vector3 FollowTracker() {
if (followTracker) {
GameObject tracker = GameObject.FindGameObjectWithTag("Tracker");
float trackerDistance = Vector3.Distance(transform.position, tracker.transform.position);
if (trackerDistance > avoidDistance) {
return (tracker.transform.position - transform.position) * trackerFactor;
}
}
return new Vector3();
}
[1]: /storage/temp/40364-shakey-ships.gif
Your answer
![](https://koobas.hobune.stream/wayback/20220613182745im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Player moves diagonally even when not telling him to 0 Answers
Moving an Object - The right way. 2D 1 Answer
Move Obj A towards Obj B BUT keep going in same direction when Obj A has reached Obj B 1 Answer
Move from one position to another in x seconds 2 Answers
Prevent jittering of object movement 0 Answers