Hello!
I am new to Unity and would like to know if I could make my 2D character stop sliding when I release the W key or the S key (W=Forward, S=Backwards). I tried messing with Rigidbody2D but it keeps sliding. Any help? Here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterMove : MonoBehaviour
{
public Rigidbody2D rb;
public bool isGrounded = false;
void Update()
{
if(Input.GetKey("w")){
rb.velocity += new Vector2(1,0);
transform.rotation = Quaternion.Euler(0, 0, 0);
}
if(Input.GetKey("s")){
rb.velocity += new Vector2(-1,0);
transform.rotation = Quaternion.Euler(0, 180, 0);
}
if(Input.GetKeyDown("space")){
if(isGrounded == true){
rb.velocity += new Vector2(0,10);
}
}
}
void OnCollisionEnter2D (Collision2D col){
if(col.collider.tag=="Ground"){
isGrounded=true;
}
}
void OnCollisionExit2D (Collision2D col){
if(col.collider.tag=="Ground"){
isGrounded=false;
}
}
}