Hi!
I have problem with my script. I am making a simple 2d game to improve my skills. I writed movement script for player to let him move. But here is problem. When I’m pressing left axis for example character move correctly but when i stop doing that player don’t stop moving instantly instead he slide along for a while. Can you help me?
using UnityEngine;
using System.Collections;
public class movementPLayer : MonoBehaviour {
Animator animator;
Vector2 position = Vector2.zero;
public float speed = 0.1f;
bool didInputLeft = false;
bool didInputRight = false;
Rigidbody2D body;
// Use this for initialization
void Start () {
animator = transform.GetComponent<Animator> ();
position = transform.position;
}
// Update is called once per frame
void Update () {
if(Input.GetAxis("Horizontal") < 0){
didInputLeft = true;
}
if(Input.GetAxis("Horizontal") > 0){
didInputRight = true;
}
if(Input.GetAxis("Horizontal") == 0){
didInputRight = false;
didInputLeft = false;
}
}
void FixedUpdate () {
if (didInputLeft == true) {
rigidbody2D.AddRelativeForce(-Vector2.right * speed);
animator.SetInteger("move", 1);
}
if (didInputRight == true) {
rigidbody2D.AddRelativeForce(Vector2.right * speed);
animator.SetInteger("move", 2);
}
if (didInputLeft == false || didInputRight == false) {
animator.SetInteger("move", 0);
}
}
}