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 HakJak · Jan 15, 2015 at 05:11 AM · rotationrigidbodyquaternionlimitbike

Limit Rotation of Rigidbody to 45 Degrees (Handlebars on a Bicycle)

I desperately need some help here! I have a long-term commitment to build this game so any advice you can give will be GREATLY appreciated! I've been struggling with this for hours and I feel really stupid because this seems like it should be an easy problem to solve.

I have a bicycle that is controlled with a script I bought on the asset store, however I have to add a script to make the handlebars turn in sync with the front wheel. Unfortunately the main script is written in Javascript and has no comments, so I can't really reference anything in that script. Instead, I've written a separate script in C# that turns the handlebars according to user input. I'm trying to limit the turning to 45 degrees in either direction.

The bike has a ragdoll rider, so it is important that the handlebar is a rigidbody so I can attach joints.

Here's what I have so far. It works, as long as I don't drive the bike anywhere! Once I move the bike anywhere else, the handlebar rotation resets back to the very first position, and not back to the center I'm probably going about this all wrong. REALLY need some help.

        private Quaternion startRot = Quaternion.Euler(0, 0, 0);
         private Quaternion targetLeft = Quaternion.Euler(0, 0, 0);
         private Quaternion targetRight = Quaternion.Euler(0, 0, 0);
         private float speedRot = 5.0f; //speed rotation
         private float handleTurnInput;
 
         handleTurnInput = player.GetAxis ("Steer Horizontal");
 
     void Start() 
     {
         startRot = this.transform.localRotation; //initialization start rotation
         targetRot = startRot;
         targetRot *= Quaternion.Euler(0, 0, 45);
         targetRot2 = startRot;
         targetRot2 *= Quaternion.Euler(0, 0, -45);
     }
     
         void FixedUpdate() 
         {
     
             handleTurnInput = player.GetAxis ("Steer Horizontal");
     
             if(handleTurnInput > 0) 
             {
                 rigidbody.rotation=Quaternion.Slerp (transform.rotation, targetRot, speedLeft*Time.deltaTime);
             }
             else if(handleTurnInput < 0) 
             {
                 rigidbody.rotation=Quaternion.Slerp (transform.rotation, targetRot2, speedRight*Time.deltaTime);
             }
             else
             {
                 rigidbody.rotation=Quaternion.Slerp (transform.rotation, startRot, speedRot*Time.deltaTime);
             }
         }


alt text

screen shot 2015-01-14 at 9.52.51 pm.png (148.3 kB)
Comment
Add comment · Show 1
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 HakJak · Jan 15, 2015 at 10:27 AM 0
Share

Here's a screenshot showing the problem I'm having. When the user stops giving input, the handlebars should re-center with the bike. Ins$$anonymous$$d, they recenter to the very first position they were in at the start of the game. I am so stuck... really hoping for some help here.

alt text

screen shot 2015-01-14 at 9.55.57 pm.png (42.8 kB)

2 Replies

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

Answer by Baste · Jan 15, 2015 at 05:12 PM

Instead of setting the rotation of your handlebar's rigidbody, set their transform's localrotation. I'm assuming that the handlebars have the main bike as their parent object.

You're already using the local rotation as the start rotation, so this should be enough:

 if(handleTurnInput > 0) 
 {
     transform.localRotation = Quaternion.Slerp (transform.localRotation, targetRot, speedLeft*Time.deltaTime);
 }
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
avatar image
0

Answer by Moonasooper · Aug 24, 2016 at 03:01 PM

transfrom.localEularAngle solved this for me

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Preventing rotation of rigidbody at a specific rotation 0 Answers

Keeping transform.forward facing forwards 1 Answer

How to get rigidbody local z axis rotation in Euler angles? 0 Answers

how do i compare the angles of a rigid bodies velocity against its transform in one local axis 0 Answers

Calculating Torque but cant add Quaternion 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