Hey all,
I currently have an issue with my platformer movement.
Currently my player can walk left and right and jump, however when we introduce a slope, the player can easily walk up and when stopping mid-way, the player will slowly glide down (as seen in this gif)
What I want to achieve is that the steeper the slope gets, the slower the player moves when moving upwards.
And when the angle of the slope is like 45+ degrees. the player is no longer able to move up the slope and will slowly glide downwards until it stops on ground that is between 45 and -45 degrees rotation. Next to that, if the slope is walkable, i want the player to ‘stick’ to the ground instead of gliding down.
How can i achieve this?
This is my current movement script:
public float moveSpeed;
public float jumpHeight;
private bool grounded;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask groundObjects;
private Rigidbody2D rigidBody;
// Use this for initialization
void Start () {
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundObjects);
rigidBody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundObjects);
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) {
if (!(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))) {
rigidBody.velocity = new Vector2(moveSpeed, rigidBody.velocity.y);
} else {
rigidBody.velocity = new Vector2(0, rigidBody.velocity.y);
}
}
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) {
if (!(Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))) {
rigidBody.velocity = new Vector2(-moveSpeed, rigidBody.velocity.y);
} else {
rigidBody.velocity = new Vector2(0, rigidBody.velocity.y);
}
}
if ((Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow)) && grounded) {
rigidBody.velocity = new Vector2(rigidBody.velocity.x, jumpHeight);
}
}
Kind regards and have a nice weekend!