Hi, first of all I have to mention that I’m new to unity and programming.
I have started working on a Unity 2D platform game by the help of tutorials. My problem is, after player stops pressing the run button(left/right arrow), character keeps moving for a little bit more. I tried to decrease the speed and increase the rubbing but it is not working as I want it to be.
I want it to stop moving instantly when player stops pressing run buttons.
Hope someone can help me with it.
Here is my character controller script;
using UnityEngine;
using System.Collections;
public class HeroControllerScript : MonoBehaviour
{
public float maxSpeed = 5f;
bool facingRight = true;
Animator anim;
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
public float JumpForce = 500f;
void Start ()
{
anim = GetComponent<Animator>();
}
void FixedUpdate ()
{
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
anim.SetBool ("Ground", grounded);
anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (move));
rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
if (move > 0 !facingRight)
Flip ();
else if (move < 0 facingRight)
Flip ();
}
void Update()
{
if (grounded Input.GetKeyDown (KeyCode.Space))
{
anim.SetBool ("Ground", false);
rigidbody2D.AddForce(new Vector2(0, JumpForce));
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}