Search Unity

How to add touch buttons and input

Discussion in 'Scripting' started by Karamveer1991, Apr 20, 2014.

  1. Karamveer1991

    Karamveer1991

    Joined:
    Oct 7, 2013
    Posts:
    47
    This is the original code for Pc controls. How can I change it to android and use two touch buttons for left and right movement instead of normal touch, coz I tried it and it doesn't work for me.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.  
    6.     public float sideSpeed;
    7.     public float speed;
    8.     public GUIText countText;
    9.     public GUIText winText;
    10.     public GUIText fallText;
    11.     public Transform ground;
    12.     private int coins;
    13.  
    14.     void Start ()
    15.     {
    16.         coins = 0;
    17.         SetCountText ();
    18.         winText.text = "";
    19.     }
    20.    
    21.  
    22.     void Update ()
    23.     {
    24.         if (coins>=10)
    25.         {
    26.             winText.text = "You Win!!!";
    27.         }
    28.         if(transform.position.y < ground.position.y)
    29.         {
    30.             fallText.text = "You're Down!!!";
    31.         }
    32.     }
    33.  
    34.     void SetCountText()
    35.     {
    36.         countText.text = "Coins:" + coins.ToString ();
    37.     }
    38.  
    39.     void FixedUpdate()
    40.     {
    41.         float moveSideways = Input.GetAxis ("Horizontal");
    42.         Vector3 movement = new Vector3 (moveSideways, 0.0f, 0.0f);
    43.         rigidbody.AddForce (movement * sideSpeed * Time.deltaTime);
    44.         transform.position += Vector3.forward * speed * Time.deltaTime;
    45.     }
    46.  
    47.     void OnTriggerEnter(Collider other)
    48.     {
    49.         if(other.gameObject.tag=="coin")
    50.         {
    51.             other.gameObject.SetActive(false);
    52.             coins = coins + 1;
    53.             SetCountText();
    54.         }
    55.     }
    56. }
    57.