Hello everybody, I'm trying to make a game where you can skate. However, I'm running into some trouble with the script that involves how to make the player grind. This is the code I have right now:
function Update () {
var up = transform.TransformDirection(Vector3.up);
var hit : RaycastHit;
Debug.DrawRay(transform.position, -up * 10, Color.green);
if(Physics.Raycast(transform.position, -up, hit, 10)){
if(hit.collider.gameObject.CompareTag("Grinding")){
Debug.Log("hit");
}
}
}
So it uses a Raycast to find an Object tagged with "Grinding" which works. However, I want the player to automatically move across the object (in this case, a rail). What kind of code would I need to add for this?
The answer completely depends on how you are moving your player and how detailed your simulation is trying to be.
But generally, you need to change what you are doing with Input.getAxis if grinding is true.
You first need to check the angle between the player’s direction of movement and the main axis of the rail, and the player’s speed, make sure they are going fast enough and in almost the same direction as the rail to begin with or they bounce off.
Then you need to constrain their Z-forward motion to the main axis of the rail, and their sideways motion to X rotation, by a factor inversely proportional to their speed. If the tilt gets too much for how fast they’re going, they fall off.
Temporarily parenting them to a configurable joint might be the best way. You should also take them out of physics and fake gravitational acceleration based on the slope of the rail.
A curved rail has to be treated as a series of straight rails with a transition trigger at each kink, and the faster they’re going when they hit a transition the closer their X tilt has to be to the difference between the segments’ Y axes.
Finally, add random tilt forces to the player the whole time they’re grinding, to make it harder to do for a long time.
Ok, that should be enough to keep you busy for a while.
I'd expect you'd need an animation... That actually has the player grinding, and have it so the rail goes on the middle of the board automatically...
So it'd be like...
If player do ray cast and pushes the "f" button, then play animation while ray cast is true... Then have it run the method with the animation and the whole, if player pushes "space bar" to "jump off the rail" then do jump off animation...
If that makes sense? It'd take a lot of testing I'd expect... but yeah.. haaha.
Good question though... First time I seen one like this at least lol. Good luck!