Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kgmpers · Feb 08, 2015 at 02:32 AM · movementtransformvector3jitter

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

shakey-ships.gif (271.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by keburanuil · Feb 08, 2015 at 12:49 PM

Try using LateUpdate() instead of Update()

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges