• 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 /
This question was closed Jun 29, 2015 at 12:47 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by ghost20000 · Mar 29, 2015 at 09:51 PM · directionplatformmovingmoving platformmoving-platform

Can't get platform to move to the other direction

This is my code:

 using UnityEngine;
 using System.Collections;
 
 public class MovingPlat : MonoBehaviour {
     
     public static float movespeed = 2f;
     public Vector2 Direction = Vector2.right;
 
     public void Start(){
 
     }
 
     public void Update(){
         if (transform.localPosition.x == 40.5f)
             Direction = -Direction;
 
         if (transform.localPosition.x == 30.2f)
             Direction = Vector2.right;
 
         transform.Translate(Direction * movespeed * Time.deltaTime);
     }
 }
 
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

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by lordlycastle · Mar 29, 2015 at 09:58 PM

Well yeah, I doubt that the x of the position of the transform is exactly 40.5 any time, unless set by you from code. Use a range, like position.x < 41 && position.x > 40 so it never changes direction and the default value of Direction is .right so it only moves in right.

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 ghost20000 · Mar 29, 2015 at 10:17 PM 0
Share

Thanks! although now my character doesn't stay on the platform

avatar image ghost20000 · Jun 29, 2015 at 12:26 AM 0
Share

nvm fixed it

avatar image
1

Answer by supernat · Mar 29, 2015 at 09:59 PM

Your localPosition.x value will never equal 40.5f. Floating point values are not exact, so what you call 40.5, a floating point register may store as 40.49999934. And the other problem is that you are not adding 1 bit of precision to the register every frame, so you'll almost never even hit this number. You'd have to get very lucky for moveSpeed * Time.deltaTime to = 40.49999934 in the first place.

The solution is to check using >= (for the 40.5 case) and <= (for the 30.2 case) if that works for you. Or check localPosition.x is within 2.0 units of 40.5 (in other words >= 39.5 and <= 41.5). But in the latter case, you had better be sure that moveSpeed*Time.deltaTime will never be greater than 2.0 units in any single frame (you'd need to add code and add some hysteresis, say make sure it's never greater than 1.9), or you'll miss that window when they minimize the game or some other hiccup causes a delay making Time.deltaTime bigger than you expected.

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Cant get moving platform to go the other direction 1 Answer

Moving Platform Moves instantly. 1 Answer

Rigidbody on a platform 2 Answers

Moving Platform Troubles 0 Answers

Multiplayer - Other players shaking back and fourth on moving platform 2 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