# How do I make my character slide down high-angled slopes?

I am currently working on my character's movement in Unity3D. I managed to make him move relatively to the mouse cursor. I set a slope limit of 45°, which does not allow the character to walk up the mountains with higher degrees. But he can still jump them up.

How do I manage to make him slide down again when he jumped at places with too high slope?

edit: Code snippet of my basic movement. using UnityEngine; using System.Collections;

public class BasicMovement : MonoBehaviour {
private float speed;
private float jumpSpeed;
private float gravity;
private float slopeLimit;
private Vector3 moveDirection = Vector3.zero;

void Start()
{
PlayerSettings settings = GetComponent<PlayerSettings>();
speed = settings.GetSpeed();
jumpSpeed = settings.GetJumpSpeed();
gravity = settings.GetGravity();
slopeLimit = settings.GetSlopeLimit();
}

void Update() {
CharacterController controller = GetComponent<CharacterController>();
controller.slopeLimit = slopeLimit;

if (controller.isGrounded) {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;

if (Input.GetButton("Jump")) {
moveDirection.y = jumpSpeed;
}

}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}

• Need more information: are you using the physics? Are these slopes using the terrain system or something else? What component or components are controlling your character? Is this a 2D or 3D game? – Kylotan Mar 17 '12 at 17:24
• Do you mean slide when the player attempts to jump, or when he completes a jump and hits the slope? – tyjkenn Mar 17 '12 at 18:43
• @tyjkenn I mean when he completes the jump. Like in Super Mario 64. When he lands on the ground, and this ground is too high-angled, he should slide down to a point where the angle is equal to the slopeLimit. – keinabel Mar 17 '12 at 22:01
• @Kylotan well, I am quite a newbie to Unity. So, I try to give an answer as good as I can. I created a character with the character controller - no rigid body. I made him move when a key is pressed and I made him walk relatively to the cursor. I created a terrain and used the button (oh my, this has to sound stupid) to create some mountains. This will be a 3D game with looks and camera style like WarCraft III. – keinabel Mar 17 '12 at 22:05

Your braking when ground is hit is wrong, you simply stop the vertical movement, but that is only how a ground hit works when the ground is flat.

What you should do is to project moveDirection on the surface that is hit, that will give you the new moveDirection. If your library has a function to project a vector on a plane or surface that is what you should use. If not you should subtract moveDirection's projection on the surface's normal from moveDirection itself.

• there are games that do use same behavior, eg. skyrim, use exactly same method for braking. the problem is character can jump even if it's not on a flag ground. – Ali1S232 Mar 18 '12 at 20:48

just check before jumping if character is standing on a ground, I mean if he is not on a high angled slope. and leave anything else to the physics engine.

based on the code you provided I can see where the problem is:

according to Unity script reference, CharacterController.isGrounded is only returning if character is touching the ground. it doesn't check for slopelimit. after a little searching FPSWalkerEnhanced seems to be something that solves you issue.

• Well, I do check whether the character is grounded before every move. – keinabel Mar 17 '12 at 15:55
• @keinabel then he shouldn't be able to jump. the only way he can move up the slopes using jumping is if he can jump even if he is not on solid ground. I mean it's just as you check if he can't walk up the slopes. if he can't walk in at least one direction due to slope he shouldn't be able to jump at all. – Ali1S232 Mar 17 '12 at 18:40
• would it help, if I add my 'movement code' to my post? – keinabel Mar 17 '12 at 22:06
• I guess could point straightly at the problem. – Ali1S232 Mar 17 '12 at 22:07