Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to modify the Walking by the Input

Discussion in '2D' started by RIw, Feb 14, 2015.

  1. RIw

    RIw

    Joined:
    Jan 2, 2015
    Posts:
    90
    Hello
    I've just add a simple Input movement to my 2D game but when I will hit the D Key 100 times the fastest as I can,when I will release the D Key the character is still moving.
    I want to make that the Player will stop when I will release the button but I don't know how.
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Are you using Rigidbody2d and forces? Then set rigidbody2D.velocity = Vector2.zero, that will stop your character.
     
  3. RIw

    RIw

    Joined:
    Jan 2, 2015
    Posts:
    90
    I'm not using the RigidBody2D.
    Here is a part of my script

    Code (CSharp):
    1. void Update () {
    2.      
    3.         if (Input.GetKeyDown(KeyCode.D))
    4.         {
    5.             if(DWalk && NextWalk)
    6.             {
    7.                 NextWalk = false;
    8.                 WWalk = false;
    9.                 SWalk = false;
    10.                 AWalk = false;
    11.                 pos += Vector3.right * (float)6.4;
    12.              
    13.                 rowNumber = 2;
    14.                 colNumber = 0;
    15.                 totalCells = 4;
    16.              
    17.                 transform.position = Vector3.MoveTowards(transform.position, pos,Time.deltaTime * (float)speed);
    18.                 traveling = true;
    19.                 StartCoroutine(WalkAnimationCourtine());
    20.                 NextWalk = false;
    21.                 StartCoroutine(WaitCourtine());
    22.             }
    23.          
    24.         }
    NextWalk is checking that for 0.5 second Player can move again,but as I said When I will hold the Button many times,the Player will walk even when the NextWalk is false :(
     
  4. RIw

    RIw

    Joined:
    Jan 2, 2015
    Posts:
    90
    And I've got another problem.
    When my character will collide with a NPC,the Screen is shaking.
     
  5. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Is there a problem with your coroutine? If you do something like this instead:
    Code (CSharp):
    1. float NextWalk = 0;
    2.  
    3. void Update () {
    4.         if (Input.GetKeyDown(KeyCode.D))
    5.         {
    6.             if(DWalk && NextWalk < Time.time )
    7.             {
    8.                 NextWalk = Time.time + 0.5f;
    9.             }
    10.         }
     
  6. RIw

    RIw

    Joined:
    Jan 2, 2015
    Posts:
    90
    DWalk and the NextWalk are booleans so the operand "<" cannot be used.
     
  7. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    That is why I redefined it as a float instead. (You could certainly use a different variable name if you want to...)
     
    Last edited: Feb 17, 2015