Hello there. I’m quite new to coding so these are some nooby questions. I am making a 2D Platformer and I have the movement setup but I have a couple of things that annoy me.
- When you are moving and then you take your finger off the key, instead of stopping completely and immediately, it slides for a second or so before stopping. I am not 100% sure how to make it just instantly stop.
- The player can stick to walls, meaning it can pretty much climb walls. I want it to just fall when it hits a wall, but I am not 100% sure how to do that.
Here is my code:
public float jumpHeight;
public float moveSpeed;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool grounded;
private bool doubleJumped;
// Use this for initialization
void Start () {
}
void FixedUpdate(){
grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);
}
// Update is called once per frame
void Update () {
if (grounded) {
doubleJumped = false;
}
if (Input.GetKeyDown (KeyCode.W)&& grounded ) {
GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
Jump ();
}
if (Input.GetKeyDown (KeyCode.W) && !doubleJumped && !grounded ) {
GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
Jump ();
doubleJumped = true;
}
if (Input.GetKey (KeyCode.D)) {
GetComponent<Rigidbody2D>().velocity = new Vector2 (moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
} if (Input.GetKey (KeyCode.A)) {
GetComponent<Rigidbody2D>().velocity = new Vector2 (-moveSpeed, GetComponent<Rigidbody2D>().velocity.y); }
}
public void Jump() {
GetComponent ().velocity = new Vector2 (GetComponent ().velocity.x, jumpHeight);
}
}