Search Unity

How do i script a touch input?

Discussion in 'General Discussion' started by meshari94, Nov 22, 2014.

  1. meshari94

    meshari94

    Joined:
    Nov 22, 2014
    Posts:
    13
    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:

    Code (CSharp):
    1. public class PlayerMovement : MonoBehaviour {
    2.     public float moveSpeed;
    3.     public GameObject deathParticles;
    4.     private float maxSpeed = 5f;
    5.  
    6.     private Vector3 input;
    7.  
    8.     private Vector3 spawn;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         spawn = transform.position;
    13.     }  
    14.     // Update is called once per frame
    15.     void Update () {
    16.         input = new Vector3(Input.GetAxisRaw ("Horizontal"), 0, Input.GetAxisRaw ("Vertical"));
    17.         if(rigidbody.velocity.magnitude < maxSpeed)
    18.         {              
    19.             rigidbody.AddForce(input * moveSpeed);
    20.                
    21.         }
    Help would be really appreciated.

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

    Thanks.
     
  2. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    The standard assets have a mobile input script. From memory there is a joystick.cs which provides two joystick emulating touch pads.
     
  3. meshari94

    meshari94

    Joined:
    Nov 22, 2014
    Posts:
    13
    could you please instruct me on how to apply them to the player?
     
  4. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    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.
     
  5. meshari94

    meshari94

    Joined:
    Nov 22, 2014
    Posts:
    13
    Alright sure thing Graham, thanks for the reply.
     
  6. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    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.
     
  7. meshari94

    meshari94

    Joined:
    Nov 22, 2014
    Posts:
    13
    This is more like it lol

    Thanks brother.