# using buttons for touch controls

**URL:** https://discussions.unity.com/t/using-buttons-for-touch-controls/730101
**Category:** C# & .NET
**Tags:** Scripting, Mono
**Created:** [January 27, 2019, 12:39pm UTC](https://discussions.unity.com/t/using-buttons-for-touch-controls/730101 "2019-01-27T12:39:42Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Kiesco91](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@Kiesco91](https://discussions.unity.com/u/Kiesco91)
#### Post date: [January 27, 2019, 12:39pm UTC](https://discussions.unity.com/t/using-buttons-for-touch-controls/730101/1 "2019-01-27T12:39:42Z")

</div>

hello,

I have a player controller working perfectly using W, S and Space keys to move up, down and shoot but I want to convert these into mobile touch controls. I have added some buttons to my canvas but I don’t know how to convert to touch controls. I know I probably need a separate script and set the buttons up with on click functions on that script but I don’t know how to go about the script.

Here is my current player controller script:

```csharp
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

    public float maxSpeed = 5f;

    public float shipBoundryradious = 0.5f;

    public GameManager theGameManager;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

        Vector3 pos = transform.position;

        pos.y += Input.GetAxis("Vertical") * maxSpeed * Time.deltaTime;

        if (pos.y + shipBoundryradious > Camera.main.orthographicSize)
        {
            pos.y = Camera.main.orthographicSize - shipBoundryradious;
        }
        if (pos.y - shipBoundryradious < -Camera.main.orthographicSize)
        {
            pos.y = -Camera.main.orthographicSize + shipBoundryradious;
        }

        transform.position = pos;
    }

    void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.tag == "killbox")
        {
            theGameManager.RestartGame();
        }
    }
}

```

thanks,

---

<div class="post-metadata">

### Author: ![eses](https://avatars.discourse-cdn.com/v4/letter/e/e495f1/32.png) [@eses](https://discussions.unity.com/u/eses)
#### Post date: [January 27, 2019, 12:51pm UTC](https://discussions.unity.com/t/using-buttons-for-touch-controls/730101/2 "2019-01-27T12:51:50Z")

</div>

@Kiesco91

You know, if you don’t know this, you probably can google for this :)?

This is more like a LMGTFY question. Google “unity touch buttons” / “unity touch” and you’ll find several tutorials on this topic.

Unity UI system buttons work with touch too without any extra work IIRC.

> **[Learn game development w/ Unity | Courses & tutorials in game design, VR,...](https://learn.unity.com/search?k=%5B%22tag%3A581391a30909150018ad9268%22%5D)**
>
> Free tutorials, courses, and guided pathways for mastering real-time 3D development skills to make video games, VR, AR, and more.

But you could also use plain touches, without buttons if you also need multi-touch/gestures.

> **[Learn game development w/ Unity | Courses & tutorials in game design, VR,...](https://learn.unity.com/search?k=%5B%22tag%3A5c5138607fbf7d0057f5d8f8%22%5D)**
>
> Free tutorials, courses, and guided pathways for mastering real-time 3D development skills to make video games, VR, AR, and more.

> **[Unity - Scripting API: Touch](https://docs.unity3d.com/ScriptReference/Touch.html)**

However - this is one setup what you can/could use:

![](https://europe1.discourse-cdn.com/unity/original/4X/e/7/7/e775bf1ecb1344b9fd48a200599a31e42103f803.png)

Create a script, that has a method, then assign it to your button. When you click button, it gets called.

Then from this method, you can call your movement code / set some variables instead of setting them using Input.GetKey.

Button variable here is just an enum type field, that you can change in inspector (left, right, up, down) so that you don’t have to use some string or different methods for each of your buttons.

---

<div class="post-metadata">

### Author: ![Kiesco91](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@Kiesco91](https://discussions.unity.com/u/Kiesco91)
#### Post date: [January 27, 2019, 1:31pm UTC](https://discussions.unity.com/t/using-buttons-for-touch-controls/730101/3 "2019-01-27T13:31:05Z")

</div>

Appriciate your reply, i have tried googling 2 or 3 different ways but they just don’t seem to work how I hoped. I will have another look and go through what you have said though and see where this leads me.

Thanks
