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? Code (CSharp): 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); } } }
in the block of code: Code (CSharp): if (didInputLeft == false || didInputRight == false) { animator.SetInteger("move", 0); } Try also adding rigidbody2D.velocity = Vector2.zero; Edit: My apologies. So you want snap-movement between left and right directions? Then you probably should just set velocity directly or before adding force in another direction, zero out velocity.
Ok, thanks for reply but i have another question. I created game object and attached another to it. Both objects have own colliders. The problem is how to recognize in code which is from parent and which is from child. In my script im using GetComponentsInChildren<BoxCollider2D>(); but program finds also boxcollider from parent and use it.