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
1
Question by TheRichardGamer · Dec 01, 2015 at 07:55 PM · c#inputcarwheels

How to make a functional steering wheel for a car game?

I want my steering wheel to be like a real one and so I've made it so that you can rotate it left or right but I want to be able to reset the rotation of the wheel once the user has stopped turning the wheel, and I assume I'd need some kind of variable to store the amount of rotation of the wheel, but I don't know how.

Anyways, here's my code so far:

 using UnityEngine;
 using System.Collections;
 
 public class Wheel : MonoBehaviour {
 
     public int turnSpeed;
     public GameObject wheel;
     
 
     void Update ()
     {
 
         if(Input.GetKey (KeyCode.A))
         {
 
             wheel.transform.Rotate(-Vector3.up * Time.deltaTime * turnSpeed);
 
         }
 
         if(Input.GetKey (KeyCode.D))
         {
             
             wheel.transform.Rotate(Vector3.up * Time.deltaTime * turnSpeed);
             
         }
 
     }
 
 }
 
Comment
Add comment · Show 2
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 alebasco · Dec 01, 2015 at 10:01 PM 0
Share

You're going to need to store the amount of times it has rotated completely, but eventually Quaternion.RotateTowards will be what you want to get back to the normal position

avatar image meat5000 ♦ · Dec 02, 2015 at 05:56 PM 0
Share

Resetting the rotation will look pretty unrealistic. Especially if the wheels dont follow or if they snap to place instantly.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by JedBeryll · Dec 01, 2015 at 11:27 PM

Just store the default rotation of the wheel at start to something like:

     float rotations = 0;
     Quaternion defaultRotation;
 
     void Start()
     {
         defaultRotation = wheel.transform.localRotation;
     }

then you can rotate it back like this:

 void Update()
 {
      if(Input.GetKey (KeyCode.A))
      {
          
         wheel.transform.Rotate(-Vector3.up * Time.deltaTime * turnSpeed);
         rotations -= turnSpeed;
      }
  
      else if(Input.GetKey (KeyCode.D))
      {
          
         wheel.transform.Rotate(Vector3.up * Time.deltaTime * turnSpeed);
         rotations += turnSpeed;
      }
     ...
     else    //rotate to default
     {
         if (rotations < -1)
         {
             wheel.transform.Rotate(Vector3.up * Time.deltaTime * turnSpeed);
             rotations += turnSpeed;
         }
         else if (rotations > 1)
         {
              wheel.transform.Rotate(-Vector3.up * Time.deltaTime * turnSpeed);
              rotations -= turnSpeed;
         }
         else if (rotations != 0)
         {
              wheel.transform.localRotation = defaultRotation;
              rotations = 0;
         }
     }
 }

I think this should do it. There may be some inaccuracy with the floats so you can compare the rotations to 1 and -1 and you can just set the wheel to a default rotation. If you care about that 1 degree you need to do different checks though .

Note: if you don't really need to use the wheel's gameobject anywhere, store it's Transform instead. (Or if you need it in just a few cases you can still access it as wheel.gameobject). This would be easier on the processor as it doesn't need to access the transform every frame.

Comment
Add comment · Show 6 · 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 alebasco · Dec 02, 2015 at 12:07 AM 1
Share

What if the user rotated it around more than once?

avatar image JedBeryll alebasco · Dec 02, 2015 at 07:22 AM 0
Share

True.... didn't think of that. Edited.

avatar image TheRichardGamer · Dec 02, 2015 at 05:52 PM 0
Share

I tried it and it works but there's an issue where the position that the wheel resets itself to is slightly less than 0 degrees.

alt text

img.jpg (237.0 kB)
avatar image JedBeryll TheRichardGamer · Dec 02, 2015 at 09:39 PM 0
Share

Are you sure this is not the initial rotation that is being set to defaultRotation? Do you somehow prohibit turning the wheel further than a certain rotation?

avatar image Akhil-Raja · Dec 03, 2015 at 06:58 AM 0
Share

I see your y rotation is not set to 0. Are you sure the image isnt rotated or slightly tilted ?

avatar image no00ob · May 10, 2018 at 03:39 PM 0
Share

I know this is a old thread but why isn't this working? everything functions but when I turn the car the position the steering wheel returns changes, any idea why?

avatar image
0

Answer by saadali211 · Oct 03, 2018 at 08:29 AM

How to make a functional car steering wheel for a parking game. #Unity 3D #car control for parking game with #steering wheel with free script. Realistic car control with steering wheel UI to control car in unity game.

https://youtu.be/0D-WBP18hxY

alt text


carparking02.png (273.4 kB)
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

42 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

Related Questions

Distribute terrain in zones 3 Answers

Wheels that just plain won't spin 0 Answers

WheelCollider rotates mesh wrong 1 Answer

Multiple Cars not working 1 Answer

Function Requires Two Button Presses Instead of One? 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