Wheel transform position problem with height increasing road

Hello,

I have a problem with my car setup and height increasing or decreasing roads. The wheels are dispersing every time a little more inside the mesh after after the wheel collider starts to collide with the next face of the track.

I already tried to subdivide the road in blender. The poly count got much higher but the problem still persist.

The setup don’t shows this behavior on a flat track.

I’m using the following code to position the wheels:

    void WheelPosition()
    {
        int i = 0;
        
        foreach (WheelCollider w in wheelColliders)
        {
            // define a hit point for wheel collision
            WheelHit hit;
            
            Vector3 center = w.transform.TransformPoint(w.center);

            if (w.isGrounded == true)
            {
                w.GetGroundHit(out hit);

                Vector3 temp = wheels[i].position;
                temp.y = hit.point.y + (w.transform.up.y * w.radius);
                wheels[i].position = temp;
            }
            else
            {
                Vector3 temp = wheels[i].position;
                temp.y = center.y - (w.transform.up.y * w.suspensionDistance);
                wheels[i].position = temp;
            }

            i++;
        }
    }

Peter.

wheel radius is correct for your wheels?
GetGroundHit is also working correctly? (Debug.DrawLine helps to identify such issues straight in the editor)

and the physics settings have been made correspondingly for it? (I guess that might most likely be it as the performance you have on the iphone is significantly lower and mesh collision is commonly avoided for the good especially if you have that many polygons as you have there)

Dreamora,

The wheel collider radius is set for all wheels equal to the radius of the wheel graphics object(in my case 0.49).

For the car I’m using two box colliders in combination with a Rigid body. I removed the default mesh collider from the main body of the car.

For the track I simple created with the import function an default mesh collider. Would it be better to import the drive track in separate meshes with custom made box colliders? I found an article to automate this process somewhat.
http://www.anicombo.com/blog/?p=404

I lowered the default timestamp from 0.02 to 0.033. This setting will give my demo app a smooth 30 frames.

I will try to debug this when I’m back home. The current setup works ok on the flat parts of the track. For now I’m guessing that the GetGroundHit function is working correctly.

Hello,

I have written a line of extra code for the debug.Drawline function.

    // Update is called once per frame
    void WheelPosition()
    {
        int i = 0;
        
        foreach (WheelCollider w in wheelColliders)
        {
            // define a hit point for wheel collision
            WheelHit hit;
            
            Vector3 center = w.transform.TransformPoint(w.center);

            if (w.isGrounded == true)
            {
                w.GetGroundHit(out hit);
                Debug.DrawLine(center, hit.point, Color.red);
                Vector3 temp = wheels[i].position;
                temp.y = hit.point.y + (w.transform.up.y * w.radius);
                wheels[i].position = temp;
            }
            else
            {
                Vector3 temp = wheels[i].position;
                temp.y = center.y - (w.transform.up.y * w.suspensionDistance);
                wheels[i].position = temp;
            }

            i++;
        }
    }

The GetGroundHit function seems to work correct on all parts of the track (notice the four red lines on the wheels).

I also started to play around with the Edit | Project Settings | physics set-up. It only slowed the issue.

The original script was using an ray cast for every wheel. That did really NO good for the over-all frame rate on the iPhone.

Any ideas how to solve this issue with an only wheel collider set-up are greatly appreciate. :smile:

Peter.

Firstly, you should position the wheels with:-

wheels[i].position = hit.point + (w.transform.up * w.radius);

(ie, it’s not just the Y coordinate of the point that is significant.)

Secondly, make sure that the positioning of the wheel graphics is done in the LateUpdate function. You want the physics effects to be applied before you get the hit position to move the graphics.

Why positioning the wheel graphics in lateUpdate? Isn’t the physic updated just before FixedUpdate ( and not between FixedUpdate and LateUpdate )?

Hello,

I tried the given suggestions but it introduced some other problems :(. Anyway I’m trying to upload a compressed zip project of 4.7 MB. I got an error message “Tried to upload empty file”.

Peter.

Hello,

I have managed to just upload the script file. Please note that it is work in progress 8).

237049–8481–$carphysics_187.cs (5.69 KB)

2 suggestions for high-speed :
1/ play with spring values, essentially with the distance, the smaller the more stable it should be.

2/ tweek the physic, and add “gravity” force on each wheel depending on speed to “scotch” the car on the ground.

Hello,

I have made an Unity package. That’s a lot smaller then the hole scene :lol: .

I still have no clue how to make an correct animated suspension system with the Ray Cast that is build-in inside the wheel collider object.

Peter.

238230–8519–$cartest01_195.unitypackage (1000 KB)

Hello,

I dropped the whole wheel collider set-up. It’s given me tons of problems on none flat terrain with an high speed car. I started two weeks ago with this project:

http://forestjohnson.blogspot.com/2009/10/car-physics-example-2.html#

I have attached the coding inside the same scene as above and with a little tweaking it’s already behaving nicely. :smile:

I also managed to code the main scripts that make up the Car and the wheels into one c# file for better performance on the iPhone.

Peter.

After implementing the code from the Forest Johnson blog I realized that a wheel collider set-up shouldn’t make that match difference. I have combined some of the RayCast car code and JCar inside my test scene. The only problem I still can’t fix is that the car starts to bounce over edges between triangles in combination with a low poly track.

Please help to resolve this last matter for my basic wheelcollider arcade race car set-up. :smile:

Peter.

297694–10559–$cartest02_600.unitypackage (1.69 MB)

Have you tried experimenting with different spring/damper values to minimise the bouncing?

Andeeee,

I tried the last two weeks lots of different settings. I didn’t manage to fully neutralize this behavior with different wheel collider spring/damper values. I have limit the car’s velocity to 352 km/h. The engine should in my opinion be able to handle that kind of speed. I have no problems that the car is going trough meshes. I’m now thinking about to implement an extra joint for every wheel that will eliminate (clamp) the shock from the edge.

Another possible solution to this problem is discussed on GameDev sphere triangle edge collision issue - Math and Physics - GameDev.net. I’m not sure this problem is also related to Unity but the behavior described looks the same in my test scene. Is there some way in Unity to get the bounds of the current plane that the wheel colliders is hitting (Mesh.bounds)? I want to check if the hit.point is on an edge and then try to change it to a position after or before the edge.

One remarkable thing I noticed is that the hit.force value will drop to zero if the collider is going through an edge.

On the Nivida forms there is also a suggestion to check smooth sphere collisions. It didn’t make any difference.

I also have written some simple logic for the suspension spring target position. The result is that the car will not flipping easily over anymore. My whole collider set-up is fully scripted but exact the same problem will happen with manual installed wheel colliders.

Please check the scene out if you have some Unity spare time left. :slight_smile:

Peter.

I couldn’t import the package because of the .jas files - if you can post the whole project or change the files for FBX, I will have a look.

To make sure that everything will function I have recreated the scene with fbx files.

Thanks for looking into it :wink:

Peter.

299163–10601–$cartest03_204.unitypackage (1.78 MB)

Andeee

I just found the new car tutorial. Learn game development w/ Unity | Courses & tutorials in game design, VR, AR, & Real-time 3D | Unity Learn

I’m pretty sure that the coding will solve my current problems.

Peter. :smile:

Hello,

I have a problem with my car setup and height increasing or decreasing roads. The wheels are dispersing every time a little more inside the mesh after after the wheel collider starts to collide with the next face of the track.

I already tried to subdivide the road in blender. The poly count got much higher but the problem still persist.

So,Please Send Me The Step by step process for this Problem