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.
Your answer
Follow this Question
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