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. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Question GetKeyDown don't move character

Discussion in 'Scripting' started by zyzzcashh, Mar 3, 2023.

  1. zyzzcashh

    zyzzcashh

    Joined:
    Apr 9, 2022
    Posts:
    4
    Hi, how can I create a way to when I slighty press A or LeftArrow my character dont move but just looks to the left, I tried using GetKey to movement and GeKeyDown to only make him face the other way but the characters keeps moving anyway, I want to do something like Pokemon where you can slightly press the key and the characters dont move and only rotates, this is my player controller:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController1 : MonoBehaviour
    6. {
    7.     public float moveSpeed;
    8.     public Vector3 velocity = Vector3.zero;
    9.     public LayerMask solidObjectsLayer;
    10.     public LayerMask grassLayer;
    11.     public Sprite esquerda;
    12.     public Sprite direita;
    13.     public Sprite cima;
    14.     public Sprite baixo;
    15.  
    16.     private bool isMoving;
    17.     private Vector2 input;
    18.     private Animator animator;
    19.  
    20.     private void Awake()
    21.     {
    22.         animator = GetComponent<Animator>();
    23.     }
    24.     private void Update()
    25.     {
    26.        var imageComp = GetComponent<SpriteRenderer>();
    27.  
    28.         if (!isMoving)
    29.         {
    30.  
    31.  
    32.             input = Vector2.zero;
    33.             if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
    34.             {
    35.                 input.x = -1;
    36.             }
    37.             if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
    38.             {
    39.                 imageComp.sprite = esquerda;
    40.             }
    41.             else if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKey(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D) || Input.GetKey(KeyCode.D))
    42.             {
    43.                 input.x = 1;
    44.             }
    45.             if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKey(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W) || Input.GetKey(KeyCode.W))
    46.             {
    47.                 input.y = 1;
    48.             }
    49.             else if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKey(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S) || Input.GetKey(KeyCode.S))
    50.             {
    51.                 input.y = -1;
    52.             }
    53.  
    54.             if (input.x != 0)
    55.             {
    56.                 input.y = 0;
    57.             }
    58.  
    59.  
    60.             if (input != Vector2.zero)
    61.             {
    62.                 animator.SetFloat("moveX", input.x);
    63.                 animator.SetFloat("moveY", input.y);
    64.                 var targetPos = transform.position;
    65.                 targetPos.x += input.x;
    66.                 targetPos.y += input.y;
    67.                 if (IsWalkable(new Vector3(targetPos.x,targetPos.y-0.85f)))
    68.                 {
    69.                     StartCoroutine(Move(targetPos));
    70.                 }
    71.             }
    72.         }
    73.  
    74.         animator.SetBool("isMoving", isMoving);
    75.     }
    76.  
    77.     IEnumerator Move(Vector3 targetPos)
    78.     {
    79.         isMoving = true;
    80.         while ((targetPos - transform.position).sqrMagnitude > Mathf.Epsilon)
    81.         {
    82.             transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
    83.             yield return null;
    84.         }
    85.         transform.position = targetPos;
    86.  
    87.         isMoving = false;
    88.  
    89.         CheckForEncounters();
    90.     }
    91.    
    92.     private bool IsWalkable(Vector3 targetPos)
    93.     {
    94.         if (Physics2D.OverlapCircle(targetPos, 0.01f, solidObjectsLayer) != null)
    95.         {
    96.             return false;
    97.         }
    98.         return true;
    99.     }
    100.  
    101.     private void CheckForEncounters()
    102.     {
    103.         if (Physics2D.OverlapCircle(transform.position, 0.2f, grassLayer) != null)
    104.         {
    105.             if (Random.Range(1,101) <= 10)
    106.             {
    107.                 Debug.Log("Encountered a wild pokemon");
    108.             }
    109.         }
    110.     }
    111. }
    112.  
     
  2. dogmachris

    dogmachris

    Joined:
    Sep 15, 2014
    Posts:
    1,373
    One way would be to add a small delay to your movement:

    • Create a float variable with a nice descriptive name for a delay and set it to something small like 0.1
    • Create another float variable with a name that is good for time.
    • Have a condition for when the player taps the key in the direction opposite to where that character is lookin
      • The player is allowed to turn around right away, but not to move. Before he can move, some time must pass (your delay)
      • So as the key is tapped in that situation, start a counter and use time to store the time past, since the tapping of the key.
      • As soon as time > delay, allow the character to move.
    This should result in the exact behaviour you wan't, though I'm no specialist in 2D games and don't know if there's a more standardized way of doing this or not, but I know what you mean from playing Super Mario.