Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

wheelmesh follow collider

Discussion in 'Scripting' started by ghost123_1234, Nov 20, 2015.

  1. ghost123_1234

    ghost123_1234

    Joined:
    Jan 9, 2015
    Posts:
    88
    hello
    I have this settings for my wheelcollider


    But when i use this code so that the wheelmesh follows the collider this happens:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class Wheel : MonoBehaviour {
    4.   public WheelCollider wheelC;
    5.   private Vector3 wheelCCenter;
    6.   private RaycastHit hit;
    7.   void Start () {
    8.   }
    9.   void Update () {
    10.     wheelCCenter = wheelC.transform.TransformPoint(wheelC.center);
    11.     if ( Physics.Raycast(wheelCCenter, -wheelC.transform.up, out hit, wheelC.suspensionDistance + wheelC.radius) ) {
    12.       transform.position = hit.point + (wheelC.transform.up * wheelC.radius);
    13.     } else {
    14.       transform.position = wheelCCenter - (wheelC.transform.up * wheelC.suspensionDistance);
    15.     }
    16.   }
    17.   void FixedUpdate() {
    18.   }
    19. }
    20.  
    But then this happens to my wheel:
     
    Last edited: Nov 20, 2015
  2. ghost123_1234

    ghost123_1234

    Joined:
    Jan 9, 2015
    Posts:
    88
  3. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    The code is supposed to simulate the correct suspension?
    Use raycast is not very recommended by while, is a real mess.
    Uses a simple script to the rotation of the wheels, and for the suspension, leaves in the configurations of wheelcollider.

    Add this on wheel mesh and corresponding wheelcollider

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. public var wheelsCollider : WheelCollider;
    4. public var SlipPrefab : GameObject;
    5. var slipMagnitude : float = 0.5;
    6.  
    7. function Start () {
    8.  
    9. }
    10.  
    11. function Update () {
    12.  
    13.     UpdatesMeshesPositionErotation ();
    14.     SlipEffects ();
    15. }
    16.  
    17. function UpdatesMeshesPositionErotation ()
    18. {
    19.     var rot : Quaternion;
    20.     var pos : Vector3;
    21.     wheelsCollider.GetWorldPose(pos, rot);
    22.    
    23.     this.transform.position = pos;
    24.     this.transform.rotation = rot;
    25. }
    26.  
    27. function SlipEffects ()
    28. {
    29.     var CorrespondingGroundHit : WheelHit;
    30.     wheelsCollider.GetGroundHit( CorrespondingGroundHit );
    31.  
    32.     if ( Mathf.Abs( CorrespondingGroundHit.sidewaysSlip ) > slipMagnitude ) {
    33.         if ( SlipPrefab ) {
    34.             Instantiate( SlipPrefab, CorrespondingGroundHit.point, Quaternion.identity );
    35.         }
    36.     }
    37. }
    38.  
     
  4. ghost123_1234

    ghost123_1234

    Joined:
    Jan 9, 2015
    Posts:
    88
    Thanks that worked