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 Solliden · Nov 15, 2018 at 01:12 PM · transform.position

How can I get the precise transform position in Update?

Hello. I have a problem with my Player constantly moving on the x axis, as I'd like it to move when the user presses A or D, and stop moving when it reaches a whole integer on the x axis.

An example would be moving to transform.position.x = 2 and stopping.

 bool MoveLeft = false;
 bool MoveRight = false;
 bool InMotion = false;

 void Update () {

     if (Input.GetKey(KeyCode.A) && InMotion == false)
     {
         MoveLeft = true;
     }

     if (Input.GetKeyUp(KeyCode.A) && InMotion == false)
     {
         MoveLeft = false;
     }

     if (Input.GetKey(KeyCode.D) && InMotion == false)
     {
         MoveRight = true;
     }

     if (Input.GetKeyUp(KeyCode.D) && InMotion == false)
     {
         MoveRight = false;
     }

     if (MoveLeft == true || MoveRight == true)
     {
         if (MoveLeft == true)
         {
             InMotion = true;
             transform.Translate(new Vector3(-1, 0, 1) * Time.deltaTime * 3);
             if (transform.position.x % 1 == 0)
             {
                 InMotion = false;
                 MoveLeft = false;
             }
         }
         else if (MoveRight == true)
         {
             InMotion = true;
             transform.Translate(new Vector3(1, 0, 1) * Time.deltaTime * 3);
             if (transform.position.x % 1 == 0)
             {
                 InMotion = false;
                 MoveRight = false;
             }
         }
     }
     else
     {
         transform.Translate(new Vector3(0, 0, 1) * Time.deltaTime * 5);
     }


 }

Changing if (transform.position.x % 1 == 0) statement to >= 0.98 works, but it is not as precise as I would like it to be. Is there a way around this, or am I missing something?

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

Answer by Matt1000 · Nov 15, 2018 at 02:17 PM

You just won't be able to because your object never actually IS on x = 2. The reason is that game Physics doesn't work like the real one. Objects don't go through all the intermediate points between two positions. They just teleport to it. Tus, you cant check if it IS in a position but rather if it is near.

In other words, you SHOULD and MUST use the >= 0.98. or even 0.99, depending on your speed. However, that doesn't mean that your object needs to stay there. once you know it's near an integer position you can just round it so it is in the exact spot. Example:

 if (transform.position.x % 1 == 0)
 {
     transform.position = new Vector3(int.Round(transform.position.x), transform.position.y, 
                                           transform.position.z);
     InMotion = false;
     MoveRight = false;
 }


Anyhow, you should be very careful of how you do this because once the player is in this position you may not be able to take him out of it since it will always enter in the if clause. Perhaps using coroutines moving the player is a better approach but that's a totally different script.

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 Solliden · Nov 15, 2018 at 06:56 PM 0
Share

Thanks, this was informative and helpful :)

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

95 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 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

geting GameObject transform position gives Spasmic numbers 1 Answer

Character controller problem 2 Answers

Transform position to a public variable 1 Answer

How do i make object follow me? 0 Answers

Instantiation in a negative position relative to player 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