Hi
I have a steering wheel script that allows me to move the steering wheel left and right in the inspector. plus I also have a car moment script that allow me to move forward and back and turn left and right.
I want to me able to attach the steering wheel script i have to the car moment script so I can steer steering wheel moves when I turn the car
is there a way I can do this
Steering Wheel Script
using System.Collections;
using UnityEngine;
public class SteeringWheelRotation : MonoBehaviour {
[Range(-45f,45f)]
public float steeringWheelRotationAmount = 0f;
public float wheelRotationDampening = 0.5f;
public Transform frontLeftWheel, frontRightWheel;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.eulerAngles = new Vector3 (transform.eulerAngles.x, transform.eulerAngles.y, -steeringWheelRotationAmount);
}
}
Car Moment Script
using System.Collections;
using UnityEngine;
public class Car : MonoBehaviour
{
public float maxTorque = 5000f;
public float speed;
public Transform centerofMass;
public WheelCollider[] wheelColliders = new WheelCollider[4];
public Transform[] tireMeshes = new Transform[4];
private Rigidbody m_rigidBody;
void Start()
{
m_rigidBody = GetComponent<Rigidbody>();
m_rigidBody.centerOfMass = centerofMass.localPosition;
}
void Update()
{
UpdateMeshesPositions();
}
void FixedUpdate()
{
float steer = Input.GetAxis ("Horizontal");
float accelrate = Input.GetAxis ("Vertical");
float finalAngle = steer * 45f;
wheelColliders [0].steerAngle = finalAngle;
wheelColliders [1].steerAngle = finalAngle;
for (int i = 0; i < 4; i++)
{
wheelColliders*.motorTorque = accelrate * maxTorque;
}
}
void UpdateMeshesPositions()
{
for(int i = 0; i < 4; i++)
{
Quaternion quat;
Vector3 pos;
wheelColliders*.GetWorldPose(out pos, out quat);
tireMeshes*.position = pos;
tireMeshes*.rotation = quat;
}
}