Excitebike-style rotation

New to Unity, following lots of tutorials, and I’ve got the basics of character movement, camera positioning and following, and collision down. So, I’ve decided to make a 3D side-scroller that is essentially like Excitebike. Here’s my problem:

When I approach a ramp, I need for the motorbike to rotate with the slope of the ramp. This seems like it would be simple to accomplish, but I just can’t figure it out. I’ve got a script called CalculateAngle.js attached to my motorbike:

function Update () {
transform.rotation.eulerAngles.z = ?!?!;
}

Where ?!?! is what I can’t figure out. It’s basic, but it’s driving me crazy. My thought is that it has to do with transform.position.x and transform.position.y, but I’m unsure. Attached is an image of the bike at an angle on the ramp (placed by hand to indicate what I’m going for).

Any help is greatly appreciated.

hdez

342543--11988--$motorbike_on_ramp_159.png

Like you, I am pretty new in Unity so I can’t give you any real advice but…shouldn’t you use physics components instead ? like putting colliders to your tires, something like that ?

can you not just do this through colliders instead of code?

Indeed, I needed to stop thinking about this in a 2001-esque Flash way (where I would have had to had an equation). Insead, I put two wheel colliders on the tires and a rigid body on the entire motorbike. Now, I’ve got a problem of rotation on X and Y (I only need Z). I found a quick hack here:

http://forum.unity3d.com/viewtopic.php?p=235287

But as mentioned in the post, hard-coding the rigidbody.rotation.x and rigidbody.rotation.y does cause shaking in the model (in this case, a rocking back and forth). The post mentions using rigidbody.AddForceAtPosition but I can’t quite get it to work (still causes some rotation on X and Y at certain Z angles).

any other hints?

hdez

you can use a raycast to detect the normal of the ground and then set the bike’s transform.up to that normal. haven’t tested this myself, but this would be my first attempt