- Home /
Player slides down slope.
This question has been asked before but im having a hard time getting it to work for me. I have a 2D platformer where the Player has a Rigidbody 2D attached to it. As a lot of people know, if the player stands on a hill, the gravity (or something) from the Rigidbody will make the player slowly slide down the hill. How do I stop this from happening? Can I adjust something for the Rigidbody or even the slanted platforms that he is standing on? Here is some of the code I use for moving the player, any help is greatly appreciated!
using UnityEngine;
using System.Collections;
public class Controls : MonoBehaviour
{
public Rigidbody2D rb;
public float movespeed;
public float jumpheight;
public bool moveright;
public bool moveleft;
public bool jump;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool onGround;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
onGround = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.LeftArrow))
{
rb.velocity = new Vector2(-movespeed, rb.velocity.y);
}
if (Input.GetKey(KeyCode.RightArrow))
{
rb.velocity = new Vector2(movespeed, rb.velocity.y);
}
if (Input.GetKey(KeyCode.Space))
{
if (onGround)
{
rb.velocity = new Vector2(rb.velocity.x, jumpheight);
}
}
if (jump)
{
if (onGround)
{
rb.velocity = new Vector2(rb.velocity.x, jumpheight);
}
jump = false;
}
if (moveright)
{
rb.velocity = new Vector2(movespeed, rb.velocity.y);
}
if (moveleft)
{
rb.velocity = new Vector2(-movespeed, rb.velocity.y);
}
}
}
Your answer
Follow this Question
Related Questions
Do character controllers work with dynamic gravity? 1 Answer
toss up game object into air with freefall physics 1 Answer
How to make a 2D Player move a box properly ? 1 Answer
How do I get the platform to effect these coins? 2 Answers
Adding gravity to character and grounded checks are not working? 0 Answers