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
1
Question by vptb · Aug 27, 2014 at 11:48 AM · 2dmovementtimelerpsynchronize

Synchronize object positions

I have these saws that move around those trails in pairs and at a constant speed. When I test this on my computer with good FPS it all works great and they move as intended.

But when I test it on iPad (low and oscillating FPS) the saws in each pair start to move differently, sometimes they get closer to each other and other times they get farther away. I'm having an hard time coming up with a solution for this, maybe you guys can help me.

alt text

This is my Update function:

 private void Update(){
         //Saw Rotation
         BladeTransform.rotation *= Quaternion.AngleAxis(SawRotSpeed*360*Time.deltaTime, -transform.forward);
  
  
         //Saw Movement
         transform.position = Vector3.Lerp(_curStartPos, _destinyPos, _sawTimer);
         if (_sawTimer < 1) _sawTimer += (Time.deltaTime*1.2f*TilesPerSec)/_distanceNextWaypoint;
         else{
             _sawTimer = 0;
             _curStartPos = transform.position;
             if (Inverse){
                 if (_destinyWaypoint == 0) _destinyWaypoint = WayPoints.Count - 1;
                 else _destinyWaypoint--;
             }
             else{
                 if (_destinyWaypoint >= (WayPoints.Count - 1)) _destinyWaypoint = 0;
                 else _destinyWaypoint++;
             }
             _destinyPos = WayPoints[_destinyWaypoint].position;
             _distanceNextWaypoint = Vector3.Distance(_destinyPos, _curStartPos);
         }
 }

I use the common method of:

 Timer = 0;
 Lerp(start,end,Timer);
 if(Timer<1) Timer+=Time.deltatime;

I always used this for movement but never on a situation where I needed two objects to be synchronised with each other, am I doing something wrong or is there any other way of doing this on which I can have these saws synchronised with each other?

serras_lup.png (195.6 kB)
Comment
Add comment · Show 1
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
avatar image vptb · Aug 27, 2014 at 02:21 PM 1
Share

I changed the movement to FixedUpdate() and used Time.fixedDeltaTime and it seems to have solved the problem. But I don't think this is a good solution, so if someone has any better ideas please post them.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Tomer-Barkan · Aug 27, 2014 at 02:48 PM

Why are you dividing the timer increase by _distanceNextWaypoint? That would cause them to move in different speeds depending on the length of the vector. is that what you planned? If they start in different locations, and the distance to the waypoint is different, they will move in different speeds.

Also, maybe share some more code, variable declarations, etc. Could _sawTimer be static or modifiedby other parts of code?

If still no solution, try printing it out debug values. Print out _sawTimer and (Time.deltaTime*1.2f*TilesPerSec)/_distanceNextWaypoint; (the increase in _sawTimer).

Check that the increase is the same for all saws and the _sawTimer increases as expected every frame.

Comment
Add comment · Show 2 · 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
avatar image vptb · Aug 27, 2014 at 03:52 PM 0
Share

I'm dividing by _distanceNextWaypoint because I want a constant velocity.

Let's say I have these 4 WP:

WP 1 is at (0,0) WP 2 is at (2,0) WP 3 is at (2,8) WP 4 is at (0,8)

If I don't divide the timer increase by the distance to the next WP, the saw will take the same time to move from WP 1->2 and from WP 2->3, even though the distance from 2->3 is 3x higher than 1->2.

_sawTimer can't be static because each saw travels a different path, and is local to each saw.

I'm only having problems with this when the device has low and oscillating frame rate.

Here's more code:

     public List<Transform> WayPoints;
     public int StartWaypointIndex = 0;
     public bool Inverse;
     public float SawRotSpeed = 1;
     public float TilesPerSec = 2;
     private Vector3 _curStartPos;
     private Vector3 _destinyPos;
     private int _destinyWaypoint;
     private float _distanceNextWaypoint;
     private float _sawTimer;
 
     // Use this for initialization
     private void Start(){
         _sawTimer = 0;
         if (Inverse){
             if (StartWaypointIndex == 0) _destinyWaypoint = WayPoints.Count - 1;
             else _destinyWaypoint = StartWaypointIndex - 1;
         }
         else
         {
             if (StartWaypointIndex >= (WayPoints.Count - 1)) _destinyWaypoint = 0;
             else _destinyWaypoint = StartWaypointIndex + 1;
         }
 
         _curStartPos = WayPoints[StartWaypointIndex].position;
         _destinyPos = WayPoints[_destinyWaypoint].position;
         _distanceNextWaypoint = Vector3.Distance(_destinyPos, _curStartPos);
     }
 
     private void Update(){
         //Saw Rotation
         BladeTransform.rotation *= Quaternion.AngleAxis(SawRotSpeed*360*Time.deltaTime, -transform.forward);
  
  
         //Saw $$anonymous$$ovement
         transform.position = Vector3.Lerp(_curStartPos, _destinyPos, _sawTimer);
         if (_sawTimer < 1) _sawTimer += (Time.deltaTime*1.2f*TilesPerSec)/_distanceNextWaypoint;
         else{
             _sawTimer = 0;
             _curStartPos = transform.position;
             if (Inverse){
                 if (_destinyWaypoint == 0) _destinyWaypoint = WayPoints.Count - 1;
                 else _destinyWaypoint--;
             }
             else{
                 if (_destinyWaypoint >= (WayPoints.Count - 1)) _destinyWaypoint = 0;
                 else _destinyWaypoint++;
             }
             _destinyPos = WayPoints[_destinyWaypoint].position;
             _distanceNextWaypoint = Vector3.Distance(_destinyPos, _curStartPos);
         }
     }
avatar image vptb · Aug 27, 2014 at 05:13 PM 0
Share

I tested with debug logs and what is happening is that when there's oscillation on the fps, if the saw blades in each pair are in different paths (one is going from WP 1->2 with a distance of 2, and the other one is going from WP 2->3 with a distance of 8) the delay on the Update function derived from the frame rate will make it so that they will move with a different speed because of their paths. Which means that this problem only happens when there's different distances between waypoints.

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is wrong with this script ? if duration is 10 why the transform is moving in 11 seconds and not 10 ? 1 Answer

Getting a 2d Sprite to move over time to an Array 1 Answer

Lerping in 2D 1 Answer

2D Sprite leaves a trail when moving diagonally 1 Answer

Moving 2D sprite along a path 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