Search Unity

Character slidesaround ground

Discussion in 'Scripting' started by Anwarus, Nov 22, 2014.

  1. Anwarus

    Anwarus

    Joined:
    Feb 5, 2014
    Posts:
    3
    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):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class movementPLayer : MonoBehaviour {
    5.  
    6.     Animator animator;
    7.     Vector2 position = Vector2.zero;
    8.     public float speed = 0.1f;
    9.     bool didInputLeft = false;
    10.     bool didInputRight = false;
    11.  
    12.  
    13.     Rigidbody2D body;
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.    
    18.         animator = transform.GetComponent<Animator> ();
    19.         position = transform.position;
    20.  
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void Update () {
    25.  
    26.         if(Input.GetAxis("Horizontal") < 0){
    27.             didInputLeft = true;
    28.  
    29.  
    30.         }
    31.  
    32.         if(Input.GetAxis("Horizontal") > 0){
    33.             didInputRight = true;
    34.  
    35.        
    36.  
    37.         }
    38.  
    39.         if(Input.GetAxis("Horizontal") == 0){
    40.             didInputRight = false;
    41.             didInputLeft = false;
    42.  
    43.         }
    44. }
    45.     void FixedUpdate () {
    46.  
    47.         if (didInputLeft == true) {
    48.             rigidbody2D.AddRelativeForce(-Vector2.right * speed);
    49.             animator.SetInteger("move", 1);      
    50.         }
    51.  
    52.         if (didInputRight == true) {
    53.             rigidbody2D.AddRelativeForce(Vector2.right * speed);
    54.             animator.SetInteger("move", 2);
    55.                 }
    56.         if (didInputLeft == false || didInputRight == false) {
    57.             animator.SetInteger("move", 0);
    58.         }
    59.     }
    60. }
    61.  
     
  2. Stoven

    Stoven

    Joined:
    Jul 28, 2014
    Posts:
    171
    in the block of code:

    Code (CSharp):
    1.         if (didInputLeft == false || didInputRight == false) {
    2.             animator.SetInteger("move", 0);
    3.         }
    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.
     
  3. Anwarus

    Anwarus

    Joined:
    Feb 5, 2014
    Posts:
    3
    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.