How do i script a touch input?

Hello everyone,

I’m currently working with an android application, but stuck with the touch inputs for the main player. I already have the movements setup, but i don’t know how to convert this into touch input.

This is the player movement script:

public class PlayerMovement : MonoBehaviour {
    public float moveSpeed;
    public GameObject deathParticles;
    private float maxSpeed = 5f;

    private Vector3 input;

    private Vector3 spawn;

    // Use this for initialization
    void Start () {
        spawn = transform.position;
    }   
    // Update is called once per frame
    void Update () {
        input = new Vector3(Input.GetAxisRaw ("Horizontal"), 0, Input.GetAxisRaw ("Vertical"));
        if(rigidbody.velocity.magnitude < maxSpeed)
        {               
            rigidbody.AddForce(input * moveSpeed);
               
        }

Help would be really appreciated.

Ps. Will it also be compatible with both iOS and Android?

Thanks.

The standard assets have a mobile input script. From memory there is a joystick.cs which provides two joystick emulating touch pads.

could you please instruct me on how to apply them to the player?

Can I trouble you to google for “joystick.cs standard assets unity”? I am on a mobile device and can’t easily search through the best hits for you.

Alright sure thing Graham, thanks for the reply.

Am online for real now.

  1. Import Standard Assets (Mobile) and select all the items. You’ll end up with a Standard Assets (Mobile) folder which contains a Scripts folder.
  2. Take a look at Joystick.js. (I forgot it was a javascript file, not a c# one.) This is the code that handles the touch input.
  3. The Textures folder shows some simple artwork you can use for the thumb areas.
  4. To figure out what to do with this stuff create a new Project and download the Penelope tutorial from the Asset Store. The PDF it contains has a section on the input processing. It’s probably the tutorial you need to learn how to do touch handling on iOS and Android.

This is more like it lol

Thanks brother.