Raycast stabilizing

Ok, so I posted last week about Kart physics. I’ve started working on it and it’s starting to come together. Instead of making it with wheel colliders, I’m using a rigid body box collider(possible cylinders) to move around and I’ll be animating the wheels turning, moving, etc. I don’t understand how to use a raycast to keep the kart aligned with whatever is under it. I want the physics to be Mario Kartish, but right now they aren’t. The car needs to be impossible to flip,wobble or tilt, and if possible stay perpendicular to half-pipe ramps. In the air I think I’ll be controlling the movement with code, but a raycast would be nice to keep it stable if not.

I know you need to shoot the ray from the bottom(possibly the four corners of the vehicle?) and somehow use the ray to align the object to the ground. I’ve seen all the post I can find for this, but none of them help me. I try them out, but you can still flip with them. They don’t keep you aligned with the ground.

I know(think) the line to cast the ray is this:

Physics.Raycast (transform.position, -Vector3.up, Ground);

I can’t find anything on using the the way I want. Most help for raycasts is for shooting guns, or AI.

Anyone know How I can do this?

I’m guessing a bit here as I’ve not attempt something like this yet but… if you want the Kart to align to the ground under it you need to know the normal of the ground and then work out the angle between your kart and that normal and stop the cart from rotating beyond that.

So…

Normal (geometry) - Wikipedia (just in case…)

would be where I’d start.

I’ll check it out and get back to you! Thanks!

What’s the difference between hit.normal and hit.point?
I looked up some of this sort of thing it it seems that slerps, lerps, or Quaternion.eulerAngles would be used. Not sure how I would use them though.

That was in the link he gave
normal is the direction that the surface faces
point is the position that the ray hits
http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit.html

I had read them, but wasn’t sure what the difference was. I’ve never worked with raycasts before.

Ok, so while researching this I’ve come up with this:

	var hit : RaycastHit;
    var castRot = Vector3(transform.position.x,transform.position.y-.50,transform.position.z);
    if (Physics.Raycast (castRot, -transform.up, hit)) {
        transform.rotation = Quaternion.FromToRotation (Vector3.up, hit.normal);
    }

The problem is when it “corrects” my rotation on a slope it sets my Y rotation to a certain angle every time rather than keeping that angle. I tried getting rid of “transform.position.y-.50”, but if I do that I can’t turn, can’t go on slopes and the Y rotation is forced to a certain Y rotation again. Anyone know the problem in this code?