Point object straight into air, while keeping rotation

Hi.

I am creating a skateboard game, in which you can drive up a ramp.

The ramps have mesh colliders, the player has a rigidbody. And at the top of the ramp, is a trigger collider, that starts a function on the playerscript.

In that function, i want to make sure, that the player is pointing straight into the air, on one axis, while keeping the rest of the rotation.

Here is a picture:

The black arrow is the rotation of the player. The green is the rotation i want. I want to keep the front view rotation, and change the sideview rotation, to go straight into the air.

I tried, among other things, to set the players transform.up to the ramps transform.forward, and while that fixes the rotation in sideview, it breaks the rotation of the front view.

Any suggestions?

If your ramp is sitting along the x-axis (Vector3.right), you want to keep the player’s x- and y- rotation, and then set the z-rotation to 90. This is because the ramp is curving around the z-axis.

Now, if the ramp is along the z-axis, meaning that it curves around the x-axis, you want to keep the z- and y-rotation, and set the x-rotation to 90.

Both of those are easy, but what if the rotation is not along any of the axes? (Axises? Axii? Axis? ENGLISH)

You could do a bunch of different linear algebra magic to transform the rotation to the correct one. A much easier trick would be to, in a single frame:

  • Set the player as a child of the ramp
  • Set the player’s localRotation to make it point straight up
  • Set the player’s parent back to null.

Now, the second point there is a bit tricky, as if you have imported the model from Blender or something similar, you’ll see that it will have it’s rotation set to -90, 0, 0. If that’s the case, you’ll have to set the y-axis of the player’s local rotation to -90 when you have made it a child object, and then keep the other axes. So the code would be something like:

player.transform.parent = ramp.transform;
Vector3 oldRotation = player.transform.localRotation;
Quaternion newRotation = Quaternion.Euler (oldRotation.x, -90, oldRotation.z);
player.transform.localRotation = newRotation;
player.transform.parent = null;

If the standard rotation of your ramp is something different, the code above have to be changed. It’s kinda easy to check - just put your ramp into the scene, and check which of the three axes you have to change with 90 degrees to have it rotate along it’s curve.

Hope that helps!

What I’ve done so far, is figure out if the curve of the ramp is on forward, backward, left or right. Then i get the angle between player forward and ramp.up on the before mentioned axis. then set the player foward to ramp.up, and add the angle on the player.up.

This works fine, but rules out having ramps that are not 90 degrees on the y.