Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Convert PC horizontal controls to mobile buttons left/right input

Discussion in 'Input System' started by Dariusnator2, May 13, 2024.

  1. Dariusnator2

    Dariusnator2

    Joined:
    Apr 28, 2021
    Posts:
    9
    A while back I've made a PC endless runner game. Now I want to convert to mobile but I cannot wrap my head around how to convert the controls from A/D to left/right buttons for mobile
    This is the script which controlls the player:
    Code (CSharp):
    1.  
    2.     public float speed = 1;
    3.     public float maxSpeed;
    4.     public Rigidbody rb;
    5.     public bool PlayerIsAlive;
    6.     public Vector3 movement;
    7.     public SpawnManager spawnManager;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.      
    13.         PlayerIsAlive = true;
    14.         rb = this.GetComponent<Rigidbody>();
    15.     }
    16.  
    17.  
    18.  
    19.     void FixedUpdate()
    20.     {
    21.      
    22.         moveCharacter(movement);
    23.         movement = new Vector3(Input.GetAxis("Horizontal"), 0, 1.9f);
    24.         movement = Vector3.ClampMagnitude(movement, 1);
    25.  
    26.     }
    27.     public void moveCharacter(Vector3 direction)
    28.     {
    29.         rb.MovePosition(transform.position + (direction * speed * Time.deltaTime));
    30.     }
    31.    
    32.     private void OnTriggerEnter(Collider other)
    33.     {
    34.         spawnManager.SpawnTriggerEntered();
    35.     }