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 karabinak · Nov 10, 2020 at 09:03 PM · 3dcar game

How do I set the maximum wheel rotation on my car?

When I click the to button the wheel spins clockwise to 25 degrees which is correct. But when I click "a" nothing happens. When I change to a negative value the wheels keep spinning forever, what can I do?

 void Update()
 {
     Debug.Log(transform.eulerAngles.y);

     if ((Input.GetKey("a")) && (transform.eulerAngles.y > 335))
      {
         transform.Rotate(-Vector3.up * speed * Time.deltaTime);
      }    
     
     
     if ((Input.GetKey("d")) && (transform.eulerAngles.y < 25))
     {
         transform.Rotate(Vector3.up * speed * 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

1 Reply

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

Answer by jackmw94 · Nov 10, 2020 at 09:44 PM

The problem here is that 360 is also 0 degrees, 270 degrees is also -90 degrees, etc. This means that whenever we want to find the differences between angles in degrees we have to account for these possible offsets. Luckily, unity provides us with functions to help!

 if (Input.GetKey(KeyCode.A) 
         && Vector3.SignedAngle(Vector3.forward, transform.forward, Vector3.up) > -25f)
 {
     transform.Rotate(-Vector3.up * (speed * Time.deltaTime));
 }
 
 if (Input.GetKey(KeyCode.D)
         && Vector3.SignedAngle(Vector3.forward, transform.forward, Vector3.up) < 25)
 {
     transform.Rotate(Vector3.up * (speed * Time.deltaTime));
 }


This will mean that A will work while its forward direction is less than 25 degrees anti-clockwise from forward and D will work while forward is less than 25 degrees clockwise of it.

This is a good starting point to demonstrate the angle issue but there will be a problem here given the fact we are comparing to the global forward direction - thus breaking this if you ever want to turn your car, which I think you might.
To solve this we should compare to the car's forward direction.

 Vector3 carForwardDirection = carTransform.forward;
 Vector3 carUpDirection = carTransform.up;
 
 if (Input.GetKey(KeyCode.A)
         && Vector3.SignedAngle(carForwardDirection , transform.forward, carUpDirection ) > -25f)
 {
     transform.Rotate(-carUpDirection * (speed * Time.deltaTime));
 }
 
 if (Input.GetKey(KeyCode.D)
         && Vector3.SignedAngle(carForwardDirection , transform.forward, carUpDirection ) < 25)
 {
     transform.Rotate(carUpDirection * (speed * Time.deltaTime));
 }


It's hard to test code snippets so I haven't but I hope this either works or helps!

A few side notes, I've added the brackets in side the rotate call to ensure that we multiply floats before the vector, multiplying my a vector is marginally more expensive than floats, so this way we only do it once. Won't make a big difference but it's a good habit. Also I changed the "a" to KeyCode.A, it's a little nicer to use the enum as it prevents typos - probably hard to do for a single letter but you never know!

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

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

Related Questions

Procedurally generate a car track 1 Answer

Accelerate and brake a car without wheel collider 1 Answer

Help with AI car physics 1 Answer

How can I change the height of Hinge Joint for my car game?,How to change the height of the hinge joint for my car game? 0 Answers

I tried to make collectible Coins for my Car Game that Increase the Player's Max Velocity every Time one gets collected, but it failed. 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