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 Ptath · Jan 10, 2014 at 05:30 AM · distancetranslateplatformmovingaccurate

Platform does not move accurate. Transform translate not accurate.

Hello everyone. I have a moving platform in my 2D game(X axis, moves left and right). Platform moves by the distance set in the inspector. My problem is that I want to move it exactly by 2.0, however it moves by 2.054267 and something like that. As a result, total position of the platform changes a bit, I dont want it.

here is my code

pragma strict

var speed : float = 4; var P : GameObject; var distance : float = 2.0;

private var i : int = 1; private var DistanceTravelled : float = 0; private var lastPosition : Vector3;

function Start() { //cached data. lastPosition = transform.position; }

function Update() {

     var mspeed = speed * Time.deltaTime;
     DistanceTravelled += Vector3.Distance(transform.position, lastPosition);
     lastPosition = transform.position;
     
     if (i == 1)
     {
         gameObject.transform.Translate(Vector3.right * mspeed);
     
         if ( DistanceTravelled >= distance)
             {
                 i = -1;
                 print(DistanceTravelled);
                 DistanceTravelled = 0;
                }
            
     }
     
     if (i == -1)
     {
         gameObject.transform.Translate(Vector3.left * mspeed);    
     
         if ( DistanceTravelled >= distance)
             {
                 i = 1;
                 print(DistanceTravelled);
                 DistanceTravelled = 0;
             }                
     }

}

In console I am getting something like this http://i.imgur.com/LipyBA8.png

I fixed this problem by setting exact left and right edge, but i dont like it. Any ideas?

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
Best Answer

Answer by robertbu · Jan 10, 2014 at 05:47 AM

The problem is that Time.deltaTime is not guaranteed to divide in such a way you will always land exactly on the distance you specify. Sometimes you object will land a bit beyond the specified position. Here is a solution using Mathf.PingPong(). Given a travel distance, it will move the object over the specified distance...half to the right and half to the left of the starting position:

 #pragma strict
 var travelDistance = 2.0;
 var speed = 3.0;
 
 private var startPos : Vector3;
 private var halfDist : float;
 
 function Start() {
     startPos = transform.position;
     halfDist = travelDistance / 2.0;
 }
 
 function Update () {
     var v = startPos;
     v.x += Mathf.PingPong(speed * Time.time, travelDistance) - halfDist;
      transform.position = v;
 }

 
Comment
Add comment · Show 1 · 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 Ptath · Jan 10, 2014 at 05:56 AM 0
Share

Thank you very much!

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

18 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

Related Questions

Rigidbody on a platform 2 Answers

Multiplayer - Other players shaking back and fourth on moving platform 2 Answers

Moving Platform Moves instantly. 1 Answer

Keep object rotating around a collider given a distance to keep 1 Answer

Need help with a relatively simple issue. 1 Answer


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