- Home /
How to Attach Steering wheel script to a car moment script
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
[code=CSharp]
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);
}
}
[/code]
Car Moment Script
[code=CSharp]
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[i].motorTorque = accelrate * maxTorque;
}
}
void UpdateMeshesPositions()
{
for(int i = 0; i < 4; i++)
{
Quaternion quat;
Vector3 pos;
wheelColliders[i].GetWorldPose(out pos, out quat);
tireMeshes[i].position = pos;
tireMeshes[i].rotation = quat;
}
}
[/code]
Answer by saadali211 · Oct 03, 2018 at 08:25 AM
✅ [ SOLUTION ] Here is a detailed video tutorial with free Asset follow this video to implement the Steering wheel script to a car.
Answer by nt314p · Jul 30, 2017 at 10:32 PM
Pardon me if this is not what you wanted, but:
While clicking on your car gameobject, in the inspector, click on the Add Component button. Click on Scripts, and then click on your steering script.
You can also have the script open in the project view and drag the script onto the car gameobject.
No that didnt work cause it rotated the car now and not the steering wheel
Could you post a picture of the inspector of both your steering wheel and your car?
I think I have fixed it now
I found a basic rotation script http://answers.unity3d.com/questions/167526/rotate-to-0.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SteeringWheelRotationNew : $$anonymous$$onoBehaviour {
// This is the maximum angle that your ship will rotate to (assign it in editor)
public float maxAngle = 0;
// This is the stored angle that your ship is at
float currentAngle = 0;
void Update()
{
// This makes your angle somewhere between -30 and 30 degrees
float targetAngle = Input.GetAxis("Horizontal") * maxAngle;
// This makes the interpolation faster when the input is pressed down,
// making sure that the value is always positive.
float interpolationSpeed = 1 + ($$anonymous$$athf.Abs(Input.GetAxis("Vertical") * 0));
// This smoothly sets the current angle based on the input
currentAngle = $$anonymous$$athf.Lerp(currentAngle, targetAngle, interpolationSpeed);
// replace this with however you implement the final value
transform.rotation = Quaternion.Euler(-0, -0, currentAngle);
transform.localEulerAngles = new Vector3(0, 0, transform.localEulerAngles.z);
}
}
I added transform.localEulerAngles = new Vector3(0, 0, transform.localEulerAngles.z); at the end so it only rotated from the z axis
Your answer
Follow this Question
Related Questions
How can i prevent from mouse to collide with thirdpersoncontroller ? 0 Answers
How can i lock the mouse cursor in the middle of the screen ? 1 Answer
Why InputField don't have the property text ? 2 Answers
How can i change the walls height and keep the size for example 100x100 but height 0.1 ? 1 Answer
How can i using a break point if a gameobject have a collider after added to it ? 1 Answer