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 Nathan1258 · Jan 02, 2019 at 07:55 PM · transformcontrolleraxismouse positionwheel collider

Rotate car wheels with mouse position and not arrow keys

Hello, I've made a simple car controller where it just changes the translations of 4 wheels on a model car, (the front wheels rotate for steering and the rear wheels rotate back/forwards for movement), however I'd like to control the front wheels rotation and steer the car with the mouses horizontal position.

When I try to just change the input settings on the project from 'Key or mouse button' to 'Mouse movement', it will need the player to be constantly moving the mouse right to turn right. I just want the player to move the mouse to the right once to turn right and then when they want to straighten up the car's steering, they return the mouse back towards the middle?

I was thinking in the update function I just reset the mouse's position to go back to the center (0,0) or make it like stick to the center sorta thing.

Here's my code for the controller that just uses arrow keys at the moment:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CarController : MonoBehaviour
 {
     public WheelCollider WheelFL;
     public WheelCollider WheelFR;
     public WheelCollider WheelRL;
     public WheelCollider WheelRR;
     public Transform WheelFLtrans;
     public Transform WheelFRtrans;
     public Transform WheelRLtrans;
     public Transform WheelRRtrans;
     public Vector3 eulertest;
     float maxFwdSpeed = -3000;
     float maxBwdSpeed = 1000f;
     float gravity = 9.8f;
     private bool braked = false;
     private float maxBrakeTorque = 500;
     private Rigidbody rb;
     public Transform centreofmass;
     private float maxTorque = 1000;
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         rb.centerOfMass = centreofmass.transform.localPosition;
     }
 
     void FixedUpdate()
     {
         if (!braked)
         {
             WheelFL.brakeTorque = 0;
             WheelFR.brakeTorque = 0;
             WheelRL.brakeTorque = 0;
             WheelRR.brakeTorque = 0;
         }
         //speed of car, Car will move as you will provide the input to it.
 
         WheelRR.motorTorque = maxTorque * Input.GetAxis("Vertical");
         WheelRL.motorTorque = maxTorque * Input.GetAxis("Vertical");
         WheelFL.motorTorque = maxTorque * Input.GetAxis("Vertical");
         WheelFRa.motorTorque = maxTorque * Input.GetAxis("Vertical");
 
         //changing car direction
         //Here we are changing the steer angle of the front tyres of the car so that we can change the car direction.
         WheelFL.steerAngle = 30 * (Input.GetAxis("Horizontal"));
         WheelFR.steerAngle = 30 * Input.GetAxis("Horizontal");
     }
     void Update()
     {
         HandBrake();
 
         //for tyre rotate
         WheelFLtrans.Rotate(WheelFL.rpm / 60 * 360 * Time.deltaTime, 0, 0);
         WheelFRtrans.Rotate(WheelFR.rpm / 60 * 360 * Time.deltaTime, 0, 0);
         WheelRLtrans.Rotate(WheelRL.rpm / 60 * 360 * Time.deltaTime, 0, 0);
         WheelRRtrans.Rotate(WheelRL.rpm / 60 * 360 * Time.deltaTime, 0, 0);
         //changing tyre direction
         Vector3 temp = WheelFLtrans.localEulerAngles;
         Vector3 temp1 = WheelFRtrans.localEulerAngles;
         temp.y = WheelFL.steerAngle - (WheelFLtrans.localEulerAngles.z);
         WheelFLtrans.localEulerAngles = temp;
         temp1.y = WheelFR.steerAngle - WheelFRtrans.localEulerAngles.z;
         WheelFRtrans.localEulerAngles = temp1;
         eulertest = WheelFLtrans.localEulerAngles;
     }
     void HandBrake()
     {
         //Debug.Log("brakes " + braked);
         if (Input.GetButton("Jump"))
         {
             braked = true;
         }
         else
         {
             braked = false;
         }
         if (braked)
         {
 
             WheelRL.brakeTorque = maxBrakeTorque * 20;//0000;
             WheelRR.brakeTorque = maxBrakeTorque * 20;//0000;
             WheelRL.motorTorque = 0;
             WheelRR.motorTorque = 0;
         }
     }
 }

Thanks, Nathan.

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

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

Related Questions

DualShock 4 Horizontal axis stuck on -1 0 Answers

Joystick 3rd Axis problem on Linux 0 Answers

Steam VR move object relative to controller origin 0 Answers

What is the lable for the yaw axis 1 Answer

Relative rotation on a custom axis? 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