When i go down slopes with character controller if the speed is too high the character just flies of the slope and lands at the bottom. I Want my character to “stick” to the slope rather then just jump of it. i came up with a code to do so but it didnt turn out as i planned. now the character just slides on the slope when i stop pressing the arrows ( not staying at its place on the slope).
Physics.Raycast (transform.position, Vector3.down, out hit);
transform.position =transform.position + new Vector3(0f,distance-hit.distance,0f);
maybe use something like -
Physics.Raycast (transform.position, Vector3.down, out hit,1000,1 << 7);
Transform temptrans = new Vector3(transform.position.x, hit.position.y,transform.position.z);
transform.position =temptrans;
that should do it, but make sure you have some correcting routine, to make sure you don’t end up falling through the ground (maybe doing the raycast from below the ground and fireing it UP to only hit the ground (no other objects) like in my example above i put in a range of 1000, then put in a mask(1 << 7) for the raycast to ONLY hit layer 7. if you find out what layer your ground is replace the number 7 with the layer number of the ground.
If you’re using a character controller you need to use it’s move function to move it.
Here’s pseudo code on the way I’ve always done it.
- Raycast downwards 0.5 units (adjust based on how steep you want slopes to be)
- If it hits the floor, move the payer down 1 unit
This will make it so at long as he’s just a little bit above the floor, it will constantly be pulling him down, so he can naturally run up and down slopes. When he’s more than 0.5 off the ground, then it won’t pull him and he’ll fall naturally.