Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

wheel rotation

Discussion in 'Physics' started by passer, Dec 11, 2014.

  1. passer

    passer

    Joined:
    Dec 1, 2013
    Posts:
    12
    Hi. I can't find answer to this simple question.
    I have a 3d car on a sloping surface. It have to move down by gravity. And his wheels have to rotate. But I can't reach it.
    WheelCollider do only spring role and car moves without wheel rotation.
    CapsuleCollider on wheel stops car.
    CapsuleCollider and Rigidbody on wheel, detaches wheel.

    Anybody has any idea how to do it?
     
  2. cl9-2

    cl9-2

    Joined:
    May 31, 2013
    Posts:
    417
    Do you have a script that rotates a wheel mesh based on its corresponding wheel collider configurations?
     
  3. passer

    passer

    Joined:
    Dec 1, 2013
    Posts:
    12
    No. I don't have. How can I know how fast I have to rotate the wheels?
    I need that wheels move as other, independent objects with their own rigidbody and Capsule Collider.
     
  4. cl9-2

    cl9-2

    Joined:
    May 31, 2013
    Posts:
    417
    If you're going to using capsule colliders as wheels, you'll need 2 hinge joints for each capsule collider.

    This is a C# script that rotates the wheel mesh along with a corresponding wheel collider.

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using UnityEngine;
    6. public class WheelAlignment : MonoBehaviour
    7. {
    8.     public WheelCollider correspondingCollider;
    9.     private float rotationVel = 0F;
    10.     public void Update()
    11.     {
    12.         // Hit point for the raycast collision
    13.         RaycastHit hit;
    14.        
    15.         // Find the collider's center point. The center of the collider might not actually be
    16.         //   the real position if the transform's off.
    17.         Vector3 colliderCenter = correspondingCollider.transform.TransformPoint(correspondingCollider.center);
    18.         // Cast a ray out from the wheel collider's center the distance of the suspension,
    19.         // if it hit something, then use the "hit" variable's data to find where the wheel hit,
    20.         // if it didn't, then se tthe wheel to be fully extended along the suspension.
    21.         bool collided = Physics.Raycast(colliderCenter,
    22.                                         -correspondingCollider.transform.up,
    23.                                         out hit,
    24.                                         correspondingCollider.suspensionDistance + correspondingCollider.radius);
    25.         if (collided)
    26.         {
    27.             transform.position = hit.point + (correspondingCollider.transform.up * correspondingCollider.radius);
    28.         }
    29.         else
    30.         {
    31.             transform.position = colliderCenter - (correspondingCollider.transform.up * correspondingCollider.suspensionDistance);
    32.         }
    33.         // Set the wheel rotation to the rotation of the collider combined with a new rotation value. This new value
    34.         // is the rotation around the axle, and the rotation from steering input.
    35.         transform.rotation = correspondingCollider.transform.rotation * Quaternion.Euler(rotationVel, correspondingCollider.steerAngle, 0);
    36.         // Increase the rotation value by the rotation speed (in degrees per second)
    37.         rotationVel += correspondingCollider.rpm * (360 / 60) * Time.deltaTime;
    38.     }
    39. }
     
    passer likes this.
  5. passer

    passer

    Joined:
    Dec 1, 2013
    Posts:
    12
    Oh. Thank you very much. It solves my problem.

    I almost started to think about hinge joints))