- Home /
Question by
Tariq-mehmood · Sep 02, 2016 at 06:52 AM ·
unity 5wheelwheel collider
Rotation of wheels of a car
what is wanted to do is to rotate wheels of my car but tiers are rotating round my car as you can see in picture. the Script i am using is written below
using UnityEngine;
using System.Collections;
public class CarController : MonoBehaviour {
public WheelCollider TireFR;
public WheelCollider TireFL;
public WheelCollider TireRR;
public WheelCollider TireRL;
public float MaxSpeed = 100;
public float angle = 10;
public Transform CenterOfGravity;
public Transform TireTransformFR;
public Transform TireTransformFL;
public Transform TireTransformRR;
public Transform TireTransformRL;
Rigidbody rb;
public void ApplyLocalPositionToVisuals(WheelCollider collider)
{
if (collider.transform.childCount == 0)
{
return;
}
Transform visualWheel = collider.transform.GetChild(0);
Vector3 position;
Quaternion rotation;
collider.GetWorldPose(out position, out rotation);
visualWheel.transform.position = position;
visualWheel.transform.rotation = rotation;
}
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
rb.centerOfMass = CenterOfGravity.localPosition;
}
void Update()
{
TireTransformFL.Rotate ( TireFL.rpm / 60 * 360 * Time.deltaTime, 0, 0);
TireTransformFR.Rotate ( TireFR.rpm / 60 * 360 * Time.deltaTime,0,0);
TireTransformRL.Rotate ( TireRL.rpm / 60 * 360 * Time.deltaTime, 0, 0);
TireTransformRR.Rotate ( TireRR.rpm / 60 * 360 * Time.deltaTime, 0, 0);
}
// Update is called once per frame
void FixedUpdate () {
TireRR.motorTorque = MaxSpeed * Input.GetAxis("Vertical");
TireRL.motorTorque = MaxSpeed * Input.GetAxis("Vertical");
TireFL.steerAngle = angle * Input.GetAxis("Horizontal");
TireFR.steerAngle = angle * Input.GetAxis("Horizontal");
ApplyLocalPositionToVisuals(TireFL);
ApplyLocalPositionToVisuals(TireFR);
ApplyLocalPositionToVisuals(TireRL);
ApplyLocalPositionToVisuals(TireRR);
}
}
vdbgbggrg.png
(488.4 kB)
Comment
Best Answer
Answer by b1gry4n · Sep 02, 2016 at 09:49 AM
Make sure your art assets position for the wheel is zerod out. I am guessing it is a child and you accidentally moved it instead of whatever its parent is.