- Home /
Making mesh rotate with WheelCollider
So this may be a simple question, but I really can't seem to figure it out. I searched all over google for this, but alot of them were just linking to youtube tutorials, which showed some script from another tutorial, and messed the whole thing up, by using another script..
So I have 4 wheel colliders, which I can turn. Now I want the meshes, to turn with the wheel colliders.
This is my hierarchy:
Vehicle:
WheelMeshRightFront
WheelMeshLeftFront
WheelMeshLeftBack
WheelMeshRightBack
WheelColliderRightFront
WheelColliderLeftFront
WheelColliderLeftBack
WheelColliderRightBack
So this should be all set up. I'm simply asking for some hint to, how to turn the mesh with the wheels?
I've read something about using eularAngles, and then set it on the mesh, but I just can't figure out how you do that..
Any answers to this?
Feel free to ask about anything related to my scripts or setup. Thanks!
EDIT: I've accepted evil's answer, cause it should be working, although I found another script that did the trick.
LeftBackWheelCollider.steerAngle = 45 * Input.GetAxis("Horizontal");
RightBackWheelCollider.steerAngle = 45 * Input.GetAxis("Horizontal");
LeftBackWheelMesh.transform.localEulerAngles = new Vector3(
LeftBackWheelMesh.transform.localEulerAngles.x,
LeftBackWheelMesh.transform.localEulerAngles.y,
45 * Input.GetAxis("Horizontal")
);
RightBackWheelMesh.transform.localEulerAngles = new Vector3(
RightBackWheelMesh.transform.localEulerAngles.x,
RightBackWheelMesh.transform.localEulerAngles.y,
45 * Input.GetAxis("Horizontal")
);
Not sure what I am doing wrong. Have tried 2 different scripts. In both cases my wheel mesh is rotating around an axis ins$$anonymous$$d of the wheel mesh rotating in place. Any ideas?
Sounds like you should readjust your mesh's center.
Answer by EvilTak · Dec 15, 2014 at 06:54 AM
Use this code:
flWheel.localEulerAngles = new Vector3(flWheel.localEulerAngles.x, flWheelCollider.steerAngle - flWheel.localEulerAngles.z, flWheel.localEulerAngles.z);
frWheel.localEulerAngles = new Vector3(frWheel.localEulerAngles.x, frWheelCollider.steerAngle - frWheel.localEulerAngles.z, frWheel.localEulerAngles.z);
flWheel.Rotate(flWheelCollider.rpm / 60 * 360 * Time.deltaTime, 0, 0);
frWheel.Rotate(frWheelCollider.rpm / 60 * 360 * Time.deltaTime, 0, 0);
rlWheel.Rotate(rlWheelCollider.rpm / 60 * 360 * Time.deltaTime, 0, 0);
rrWheel.Rotate(rrWheelCollider.rpm / 60 * 360 * Time.deltaTime, 0, 0);
I have used this code myself and it is working properly. If the code doesn't work, then there is a problem in your setup, and I think it may be because you haven't assigned the proper variables (because the transform was rotating but your mesh wasn't).
Okay thanks :) I'll try it when i get home from work. But isn't this script rotating the wheels horizontally, and not vertically (steering) ? I'll do some debugging on the wheel variables, and see what I can come up with. Thanks for now
The first two lines rotate the wheels according to the steering.
It should be rotating on the Y axis, but it is rotating on the X axis. It might be some exporting error from Blender. I'll accept your answer and write the solution I found in the question. I'll do it when i turn on the pc (approx 20 $$anonymous$$). :) thanks for your trouble and help.
Thx ... Wheel$$anonymous$$esh.Rotate(WheelCollider.rpm / 60 360 Time.deltaTime, 0, 0); ... Saved my Project with a bicycle, front wheel at the angle of the fork, because GetWorldPose does not apply well on a 2-wheel vehicle with fork, again Thank you very much !!!
Answer by ShawnFeatherly · Mar 17, 2015 at 11:28 PM
EvilTak's code worked well for me. I had some trouble figuring out how to incorporate it. So I made a script that I could attach to the vehicles body. The vehicles body is the game object with a rigidbody. It should have child objects for each wheel, these child wheels are nothing but the wheel mesh (No Wheel Colliders). Those wheels can then individually be added to this MoveWheels component depending if they can steer or can't steer.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[RequireComponent(typeof(Rigidbody))]
public class MoveWheels : MonoBehaviour
{
public List<Transform> WheelsThatSteer;
public List<Transform> WheelsWithoutSteer;
private Dictionary<Transform, WheelCollider> _wheelsThatSteer = new Dictionary<Transform, WheelCollider>();
private Dictionary<Transform, WheelCollider> _wheelsWithoutSteer = new Dictionary<Transform, WheelCollider>();
[Tooltip("Motor torque on the wheel axle expressed in Newton meters.")]
private float maxTorque = 60.0f;
private float maxSteeringAngle = 30;
void Start()
{
foreach (var wheel in WheelsThatSteer)
{
_wheelsThatSteer.Add(wheel, addWheelCollider(wheel));
}
foreach (var wheel in WheelsWithoutSteer)
{
_wheelsWithoutSteer.Add(wheel, addWheelCollider(wheel));
}
}
void Update()
{
foreach (var wheel in _wheelsThatSteer)
{
// steer mesh
wheel.Key.localEulerAngles = new Vector3(
wheel.Key.localEulerAngles.x,
wheel.Value.steerAngle - wheel.Key.localEulerAngles.z,
wheel.Key.localEulerAngles.z);
// rotate mesh
wheel.Key.Rotate(wheel.Value.rpm / 60 * 360 * Time.deltaTime, 0, 0);
}
foreach (var wheel in _wheelsWithoutSteer)
{
// rotate mesh
wheel.Key.Rotate(wheel.Value.rpm / 60 * 360 * Time.deltaTime, 0, 0);
}
}
void FixedUpdate()
{
var torque = Input.GetAxis("Vertical") * maxTorque;
var steerAngle = Input.GetAxis("Horizontal") * maxSteeringAngle;
// update wheel collider physics
foreach (var wheel in _wheelsThatSteer.Values)
{
wheel.motorTorque = torque;
wheel.steerAngle = steerAngle;
}
foreach (var wheel in _wheelsWithoutSteer.Values)
{
wheel.motorTorque = torque;
}
}
private WheelCollider addWheelCollider(Transform wheelMesh)
{
var go = new GameObject(wheelMesh.name + "Collider");
go.transform.position = wheelMesh.position;
go.transform.parent = wheelMesh.parent;
go.transform.rotation = wheelMesh.rotation;
var wheelRadius = wheelMesh.renderer.bounds.size.y / 2;
var wc = go.AddComponent<WheelCollider>();
wc.radius = wheelRadius;
return wc;
}
}
wow. Can you explain the code? high level comments would be appreciate in the code snippet. Works wonders. Thanks.
Answer by maketor · Jul 02, 2017 at 02:13 AM
algo asi
public void Localpos(WheelCollider wheelXD){ if (wheelXD.transform.childCount ==0) { return; }
Transform rueda = wheelXD.transform.GetChild(0);
Vector3 position;
Quaternion rotation;
wheelXD![alt text][1].GetWorldPose(out position, out rotation);
wheelXD.transform.position = position;
wheelXD.transform.localRotation = rotation;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613173930im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Simplified vehicle physics for ambient traffic vehicles 1 Answer
Wheelcollider Settings to Match Default Unity 4 0 Answers
When I use WheelColliders, my car drifts to the left. Why? 3 Answers
Why does my car re-align after drift into the original direction? 0 Answers
Cloud recognition in Vuforia 0 Answers