- Home /
Bike wheel rotation on x-axis and steering on y-axis
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...
Answer by FortisVenaliter · May 18, 2015 at 06:33 PM
You're probably going to want an empty "Steering Axle" game object between the "wheels" and "frontwheel" objects in the hierarchy. Then rotate that for steering, and let the wheels do their own thing.
Glad that worked for you. Please be sure to mark my answer as the correct one so others searching will see this question as answered and can reference it.
Your answer
Follow this Question
Related Questions
Weird wheel rotation 1 Answer
How make front wheel bike turn left or right? 1 Answer
WheelCollider rotates mesh wrong 1 Answer
Problems with WheelColliders' settings 1 Answer
Wheel picker 0 Answers