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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How to make player move and play animation using UI buttons

Discussion in 'Input System' started by Nymeki, Aug 14, 2020.

  1. Nymeki

    Nymeki

    Joined:
    Jun 16, 2020
    Posts:
    2
    I made an animation for my character to move in 4 directions but its only working with arrow or asdw keys not buttons.. Does anyone know how to fix this? I don't know what should I add to my script.


    this is my player script
    Code (CSharp):
    1. public class Player : MonoBehaviour
    2.  
    3. {
    4.  
    5.  
    6.     public Animator animator;
    7.    
    8.     InputManager inputManager;
    9.  
    10.  
    11.     [SerializeField] float playerSpeed = 5f;
    12.  
    13.     private void Awake()
    14.     {
    15.         inputManager = GetComponent<InputManager>();
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         transform.Translate(inputManager.CurrentInput * Time.deltaTime * playerSpeed);
    21.  
    22.         Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0.0f);
    23.  
    24.  
    25.         animator.SetFloat("Horizontal", movement.x);
    26.         animator.SetFloat("Vertical", movement.y);
    27.         animator.SetFloat("Speed", movement.magnitude);
    28.  
    29.     }
    30. }
    31.  

    And this is the script for my buttons


    Code (CSharp):
    1. public class TouchButton : MonoBehaviour
    2. {
    3.     bool pressedDown;
    4.     bool pressedLastFrame;
    5.  
    6.     public InputManager.ButtonState CurrentState;
    7.  
    8.     public void PressDown()
    9.     {
    10.         pressedDown = true;
    11.     }
    12.  
    13.     public void Release()
    14.     {
    15.         pressedDown = false;
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.      
    21.         if (pressedDown)
    22.         {
    23.             if (pressedLastFrame)
    24.             {
    25.                
    26.                 CurrentState = InputManager.ButtonState.Held;
    27.             }
    28.             else
    29.             {
    30.                
    31.                 CurrentState = InputManager.ButtonState.PressedDown;
    32.             }
    33.         }
    34.         else
    35.         {
    36.          
    37.             if (pressedLastFrame)
    38.             {
    39.                
    40.                 CurrentState = InputManager.ButtonState.Released;
    41.             }
    42.             else
    43.             {
    44.                
    45.                 CurrentState = InputManager.ButtonState.None;
    46.             }
    47.         }
    48.     }
    49.  
    50.     private void LateUpdate()
    51.     {
    52.        
    53.         pressedLastFrame = pressedDown;
    54.     }
    55. }
    56.  
     
  2. Nymeki

    Nymeki

    Joined:
    Jun 16, 2020
    Posts:
    2
    Alright! Thanks for help everyone! :)) i figured it out on my own. Just use the crossplatform input from unity. Yes it has bugs but when you delete the other folders it works perfectly.