I’m currently creating a skateboarding game and have hit a bit of a problem with half-pipes. At the moment my character simply moves at a constant forward velocity and adjusts it’s orientation based on a downwards raycast (to hug the terrain).
The problem arises when my character goes up a half-pipe. What I want to happen, ala Tony Hawks, is that the character simply continues in it’s current motion straight upwards and lands back down on the halfpipe. However, my character is retaining some forward momentum and travels up and over the pipe instead.
My initial solution was to place a trigger at the top op the pipe that adjusts the characters local orientation so they are perpendicular to the imaginary wall of the halfpipe. Here’s the code (angleX is set to 270)
Vector3 newLocalAngle = new Vector3(angleX, coll.gameObject.transform.localRotation.eulerAngles.y, coll.gameObject.transform.localRotation.eulerAngles.z); Vector3 oldLocalAngle = coll.gameObject.transform.localRotation.eulerAngles; coll.gameObject.transform.localRotation = Quaternion.Euler(newLocalAngle);
I then get rid of any local y velocity on the character, to remove any motion forwards or backwards.
Vector3 newVel = coll.gameObject.transform.InverseTransformDirection(coll.gameObject.GetComponent<Rigidbody>().velocity); newVel.y = 0; coll.gameObject.GetComponent<Rigidbody>().velocity = coll.gameObject.transform.TransformDirection(newVel);
This unfortunately is snapping the player to face straight up in the world y direction, stopping any sideways motion (relative to the halfpipe).
Can anyone suggest a fix for this code, or a better way to approach the problem?