Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by mads232 · Dec 13, 2014 at 11:20 AM · wheelcollidervehicle

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")
                     );

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jervbear · Aug 23, 2017 at 06:44 PM 0
Share

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?

avatar image mads232 Jervbear · Aug 23, 2017 at 07:16 PM 0
Share

Sounds like you should readjust your mesh's center.

3 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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).

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mads232 · Dec 15, 2014 at 07:28 AM 0
Share

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

avatar image EvilTak · Dec 15, 2014 at 07:30 AM 0
Share

The first two lines rotate the wheels according to the steering.

avatar image mads232 · Dec 15, 2014 at 07:38 AM 0
Share

Oh, yeah I see :) Thanks

avatar image mads232 · Dec 16, 2014 at 05:47 PM 0
Share

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.

avatar image Ricardo-Martins · Feb 24, 2017 at 02:27 AM 1
Share

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 !!!

avatar image
2

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;
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lfagel84 · Jan 12, 2021 at 01:30 AM 0
Share

wow. Can you explain the code? high level comments would be appreciate in the code snippet. Works wonders. Thanks.

avatar image
0

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;
 }

alt text


unity.jpg (103.8 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image maketor · Jul 02, 2017 at 02:14 AM 0
Share

in update write

void update(){ Localpos(); }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges