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 /
  • Help Room /
avatar image
0
Question by trezero · Feb 02, 2018 at 09:42 AM · movementrigidbody

How can I move a RigidBody object smoothly between 3 possible positions?

I'm new to Unity. I'm building a game where my primary object controlled by the player is a car that can move between 3 lanes (Left, Center, and Right). I followed a couple tutorials and found that the resulting positions were never consistent in the movement. Because of the scale of my environment I need left, center and right lanes to be located at -.18, 0 and .18. With my current code I'm struggling to control where the player ends up when the user hits the key to move left or right.

The idea I've been pursuing is that when the user hits moveL button it would start moving left by changing the horizVel. Then this movement should be stopped in the coroutine after .2 seconds and adjusted accordingly to the specific position it should be in.

 rigidBody = GetComponent<Rigidbody> ();
 
 if ((Input.GetKeyDown (moveL)) && (horizLaneNum>1)&& (controlLocked == "n"))
             {
                 horizVel = -1;
                 StartCoroutine (stopSlideSideHoriz ());
                 horizLaneNum -= 1;
                 controlLocked = "y";
             }
 
 IEnumerator stopSlideSideHoriz()
     {
         yield return new WaitForSeconds (.2f);
         horizVel = 0;
         if (rigidBody.position.x > -0.08 && rigidBody.position.x < 0.09) {
             rigidBody.MovePosition(new Vector3(0.0f, rigidBody.position.y, rigidBody.position.z));
         };
         if (rigidBody.position.x > 0.09) {
             rigidBody.MovePosition(new Vector3(0.18f, rigidBody.position.y, rigidBody.position.z));
         };
         if (rigidBody.position.x < -0.08) {
             rigidBody.MovePosition(new Vector3(-.18f, rigidBody.position.y, rigidBody.position.z));
         };
 
         controlLocked = "n";
         Debug.Log(string.Format("x position: {0}", rigidBody.position.x));
 
     }

I expected from the code above that as long as the user position was within range of the correct position the rigidBody.movePosition command would move it to the exact position before the coroutine ends.

With my debug log, everything numbers-wise looks correct, but in Unity I can see that my player position is never ending up where it should be. Its always off and sometimes by a little bit and other times by a lot.

What am I missing? Is there a better way to do this?

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

0 Replies

· Add your reply
  • Sort: 

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

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

Everything that moves need an Rigid Body? 1 Answer

Realistic Soccer Ball Dribbling? 1 Answer

[Help] Player stops moving when hitting wall diagonally 0 Answers

Movement is inconsistent 0 Answers

Objects slowly move by their own? 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