Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 knuckles209cp · May 26, 2017 at 03:19 AM · positiontranslate

Checking position doesn't work?

When i press D on my keyboard, I would like it to move to the right smoothly and stop when the position of textobj reaches -12. Unfortunately this doesn't want to co-operate with me, Here is the script. There is no errors , The text does not stop scrolling when textobj reaches -12.

 var Speed : float = 5;
 var textobj : GameObject;
 var canMove : boolean = true;
 var OriginalSpeed : float = 5;
 var moveText : boolean = false;
 function Update (){
 
     if( Input.GetKeyDown(KeyCode.D)){
         if( canMove == true ){
             moveText = true;
         }
     }
     
     if( moveText == true ){
         textobj.transform.position += Vector3.left * Time.deltaTime * Speed;
     }
     
     if( textobj.transform.position  == Vector3(-12,0,0)){
         Debug.LogWarning("test");
         speed = 0;
     }
 }
 
 function stopMovement (){
     canMove = false;
     Speed = 0;
     yield WaitForSeconds(0.1);
     Speed = OriginalSpeed;
     canMove = true;
 }
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

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by bobisgod234 · May 26, 2017 at 04:00 AM

Add Debug.Log (textobj.transform.position). You will see that the number wont become precisely -12, but instead has some decimal on the end.

Since Time.deltaTime is usually some fraction (e.g. 0.0132323), your transform.position will never end up being precisely -12. Instead as its updates, it probably goes something like -11.58, -11.79, -11.97, -12.23 etc.

If you want the object to stop when it hits -12, you need to check if its exceeded -12, and clamp it to -12.

      //check if past -12
      if( textobj.transform.position.x  <= -12f){
          Debug.LogWarning("test");
          speed = 0;
          // clamp to -12
          textobj.transform.position = new Vector3(-12f, textobj.transform.position.y, textobj.transform.position.z);
      }
Comment
Add comment · Show 4 · 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 knuckles209cp · May 26, 2017 at 04:34 AM 0
Share

Works perfectly! thank you :)

avatar image knuckles209cp · May 26, 2017 at 04:37 AM 0
Share

would i be able to ask you another question here? [ related to this question ]

avatar image bobisgod234 knuckles209cp · May 26, 2017 at 06:16 AM 0
Share

Sure, as long as it is related to this question.

avatar image knuckles209cp bobisgod234 · May 26, 2017 at 09:27 AM 0
Share

At the moment it just moves in one speed, is it possible for it to smooth in then smooth out ? I have this so far :

 if( curr_count == 0 ){
     if( textobj.transform.position.x  <= -12f){
           speed = 0;
           // clamp to -12
           textobj.transform.position = new Vector3(-12f, textobj.transform.position.y, textobj.transform.position.z);
           GetComponent(AudioSource).PlayOneShot(okSound);
           moveText = false;
           curr_count = 1;
           openPlane();
     }
     }
avatar image
0

Answer by methos5k · May 26, 2017 at 04:18 PM

This is because of floating point (im)precision. A simple solution would be '<=' the x portion of the position (as that seems to be the only part you're testing. If it was more than that, you could use 'Distance' to measure and compare it to a small value (but not a precise one).

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

71 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing GUITexture's position through code (relative to screen) 1 Answer

Translate snapping to integers in the scene window? 0 Answers

How to stop an object from translating to a desire position? 0 Answers

Move object with Translate on X axis (with mouse) 0 Answers

How to move an object to anothers object position? 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