How do I make my character slide down high-angled slopes?
Posted
by
keinabel
on Game Development
See other posts from Game Development
or by keinabel
Published on 2012-03-17T15:24:53Z
Indexed on
2012/03/18
18:24 UTC
Read the original article
Hit count: 222
unity
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?
Thanks in advance.
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);
}
}
© Game Development or respective owner