I am making a bike racing game.
So far I have a 3D model of a bike and I have got it to move using wheel colliders.
The problem is that I want both, front and rear wheels, to rotate according to the velocity of the bike. But, only the rear wheel is rotating according to the velocity of the bike (rigidbody).
The front wheel is rotating (only steering) correctly but its not rotating on x-axis.
I want front wheel to rotate on x-axis as well like the rear wheel and also rotate on y-axis to give the effect of steering.
My project Hierarchy looks like this for the bike:
Bike
- Bike_Body (bike body except front and rear wheels)
- centerOfMass (empty object with position (0,-8,0) to stabilize bike)
- wheels
- frontWheel (empty object with wheel collider)
- frontwheelTyre (having model of bike front wheel)
- rearWheel (empty object with wheel collider)
- rearwheelTyre (having model of bike rear wheel)
The script I am using is:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BikeController : MonoBehaviour
{
public List<BikeAxleInfo> axleInfos;
public float maxMotorTorque;
public float maxSteeringAngle;
public Transform centerOfMass;
public Vector3 inertiaTensorVector;
private float motor;
private float steering;
void Start() {
rigidbody.centerOfMass = centerOfMass.position;
rigidbody.inertiaTensorRotation = Quaternion.identity;
rigidbody.inertiaTensor = inertiaTensorVector * rigidbody.mass;
}
void FixedUpdate()
{
motor = maxMotorTorque * Input.GetAxis("Vertical");
steering = maxSteeringAngle * Input.GetAxis("Horizontal");
foreach (BikeAxleInfo axleInfo in axleInfos)
{
if (axleInfo.steering)
{
axleInfo.wheelCol.steerAngle = steering;
axleInfo.wheel.localEulerAngles = new Vector3(0,steering,0);
}
if (axleInfo.motor)
{
axleInfo.wheelCol.motorTorque = motor;
}
axleInfo.wheel.Rotate(new Vector3(rigidbody.velocity.z, 0, 0));
}
}
}
[System.Serializable]
public class BikeAxleInfo
{
public WheelCollider wheelCol;
public Transform wheel;
public bool motor;
public bool steering;
}
What am I doing wrong? Kindly help please…