Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

2D touch input

Discussion in 'Scripting' started by simone9725, Aug 10, 2015.

  1. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    Hi
    I would ccontrol my spaceship in 2d top down space shooter on my adnroid device I would add touch input but when I play the inputs don't exist.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class Boundary
    6. {
    7.     public float xMin, xMax, yMin, yMax; //Screen Boundary dimentions
    8. }
    9.  
    10. public class Player_Script : MonoBehaviour
    11. {
    12.     //Public Var
    13.     public float speed;             //Player Ship Speed
    14.     public Boundary boundary;         //make an Object from Class Boundary
    15.     public GameObject shot;            //Fire Prefab
    16.     public Transform shotSpawn;        //Where the Fire Spawn
    17.     public Transform shotSpawn1;        //Where the Fire Spawn1 extra
    18.     public Transform shotSpawn2;        //Where the Fire Spawn2  extra
    19.     public float fireRate = 0.5F;    //Fire Rate between Shots
    20.     public GameObject Explosion;    //Explosion Prefab
    21.     public GameObject restartDialog; //eND gAME
    22.     public GameObject heart1;        // heart icon #1
    23.     public GameObject heart2;        // heart icon #2
    24.     public GameObject heart3;        // heart icon #3
    25.    
    26.     //Private Var
    27.     private float nextFire = 0.0F;    //First fire & Next fire Time
    28.     private bool  dead;
    29.     private Shield shield;
    30.     private int hits = 0;
    31.     private Rigidbody2D rb;
    32.     private Vector3 pos;                //Position
    33.     void Start ()
    34.     {
    35.         restartDialog.SetActive(false);
    36.         rb = GetComponent<Rigidbody2D>();
    37.         shield = GameObject.Find("Shield").GetComponent<Shield>();
    38.         HeartsCount(); // draw the right number of hearts on the screen
    39.         hits = shield.lives; // make hits equal to shield.lives to start
    40.        
    41.     }
    42.  
    43.     void Update ()
    44.     {
    45.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
    46.             int pos = Input.GetTouch(0).position;
    47.             pos = Camera.main.ScreenToWorldPoint(pos);
    48.             transform.position    = new Vector3(transform.position.x, 0, transform.position.z);
    49.         //Excute When the Current Time is bigger than the nextFire time
    50.         if (Time.time > nextFire)
    51.         {
    52.             nextFire = Time.time + fireRate;                                 //Increment nextFire time with the current system time + fireRate
    53.             Instantiate (shot , shotSpawn.position ,shotSpawn.rotation);     //Instantiate fire shot
    54.             GetComponent<AudioSource>().Play ();                             //Play Fire sound
    55.         }
    56.         if(hits != shield.lives)
    57.         {
    58.             HeartsCount();
    59.         }
    60.     }
    61.     }  
    62.     // FixedUpdate is called one per specific time
    63.     void FixedUpdate ()
    64.     {
    65.    
    66.         //Lock the position in the screen by putting a boundaries
    67.         GetComponent<Rigidbody2D>().position = new Vector2
    68.             (
    69.                 Mathf.Clamp (GetComponent<Rigidbody2D>().position.x, boundary.xMin, boundary.xMax),  //X
    70.                 Mathf.Clamp (GetComponent<Rigidbody2D>().position.y, boundary.yMin, boundary.yMax)     //Y
    71.                 );
    72.     }
    73.    
    74.     //Called when the Trigger entered
    75.     void OnTriggerEnter2D(Collider2D other)
    76.     {
    77.         if (other.gameObject.tag == "Shield") // if we encounter a shield pickup...
    78.         {
    79.             shield.resetShield(); // reset shield durability to max
    80.             HeartsCount(); // draw the right number of hearts on the screen
    81.             // Debug.Log(gameObject.name + " got shields!");
    82.             Destroy(other.gameObject); // and destroy the shield pickup object.
    83.         //DoubleShot
    84.  
    85.  
    86.             }
    87.         //Excute if the object tag was equal to one of these
    88.         if(other.tag == "Enemy" || other.tag == "Asteroid" || other.tag == "EnemyShot") // if we have no shields & we're hit by an enemy, bullet, or asteroid
    89.         {
    90.             // Debug.Log(gameObject.name + " has hit an " + other.gameObject.name);
    91.             HeartsCount(); // draw the right number of hearts on the screen
    92.             if (shield.lives == 0) // if we're out of shields, explode, self-destruct, and show the restart dialog.
    93.             {
    94.                 // Debug.Log("Player has no shields left! Destroying player!");
    95.                 Instantiate (Explosion, transform.position , transform.rotation);    
    96.                 //Trigger That its a GameOver
    97.                 Destroy(gameObject);
    98.                 restartDialog.SetActive(true);
    99.             }
    100.         }
    101.     }
    102.  
    103.     void HeartsCount()
    104.     {
    105.         hits = shield.lives;
    106.         if(shield.lives == 3) // if we have full shields (3)...
    107.         {
    108.             heart3.gameObject.GetComponent<SpriteRenderer>().enabled = true; // turn on heart #3.
    109.             heart2.gameObject.GetComponent<SpriteRenderer>().enabled = true; // turn on heart #2.
    110.             heart1.gameObject.GetComponent<SpriteRenderer>().enabled = true; // turn on heart #1.
    111.         }
    112.         if(shield.lives == 2) // if shields are at 2...
    113.         {
    114.             heart3.gameObject.GetComponent<SpriteRenderer>().enabled = false; // turn off heart #3.
    115.             heart2.gameObject.GetComponent<SpriteRenderer>().enabled = true; // turn on heart #2.
    116.             heart1.gameObject.GetComponent<SpriteRenderer>().enabled = true; // turn on heart #1.
    117.         }
    118.         if(shield.lives == 1) // if shields are at 1...
    119.         {
    120.             heart3.gameObject.GetComponent<SpriteRenderer>().enabled = false; // turn off heart #3.
    121.             heart2.gameObject.GetComponent<SpriteRenderer>().enabled = false; // turn off heart #2.
    122.             heart1.gameObject.GetComponent<SpriteRenderer>().enabled = true; // turn on heart #1.
    123.         }
    124.         if(shield.lives == 0) // if shields are at 0...
    125.         {
    126.             heart3.gameObject.GetComponent<SpriteRenderer>().enabled = false; // turn off heart #3.
    127.             heart2.gameObject.GetComponent<SpriteRenderer>().enabled = false; // turn off heart #2.
    128.             heart1.gameObject.GetComponent<SpriteRenderer>().enabled = false; // turn off heart #1.
    129.         }
    130.     }
    131.     void FireBullet(){
    132.         GameObject Clone1;
    133.         GameObject Clone2;
    134.  
    135.         Clone1 = (Instantiate(shot,shotSpawn1.position ,shotSpawn1.rotation)) as GameObject;
    136.         Clone2 = (Instantiate(shot,shotSpawn2.position ,shotSpawn2.rotation)) as GameObject;
    137.         Debug.Log ("Bullet is found");
    138.     }
    139.     public void RestartMenu()
    140.     {
    141.         Application.LoadLevel (Application.loadedLevelName);
    142.     }
    143.    
    144.     public void ExitToMenu()
    145.     {
    146.         Application.LoadLevel ("Menu");
    147.     }
    148.    
    149. }
     
  2. sterynkng14

    sterynkng14

    Joined:
    Sep 19, 2013
    Posts:
    36
    It looks like you are using pos as several data types here. I'm no expert but you initially state pos as a Vector3 and then declare pos as an int for Input.GetTouch(0).position (returns Vector2 btw) and then calling it back as both a Vector2 and Vector3 when you try to declare pos in world coordinates. I mean if I am wrong by all means, bash me for it. But this just doesn't look right haha.
     
  3. sterynkng14

    sterynkng14

    Joined:
    Sep 19, 2013
    Posts:
    36
    Instead for lines 45 to 47, I'd change to


    Code (CSharp):
    1. if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
    2. {
    3. Vector2 screenPos = Input.GetTouch(0).position;
    4. pos = Camera.main.ScreenToWorldPoint(screenPos);
    5. //Continue with rest of code
     
  4. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    Thanks for your reply but it still no get input when I touch the screen
     
  5. sterynkng14

    sterynkng14

    Joined:
    Sep 19, 2013
    Posts:
    36
    I was reading through the forums came across someone having the issue. Is you unity client up to date?
     
  6. simone9725

    simone9725

    Joined:
    Jul 19, 2014
    Posts:
    234
    yes why?
     
  7. sterynkng14

    sterynkng14

    Joined:
    Sep 19, 2013
    Posts:
    36
    Thought it could've been the cause of a earlier editor. Honestly, i can't give you a good reason to why it's happening cause i am not familiar with touch devices and unity but i thought i would at least try since there were no replies. Try googling the problem and maybe someone has a solution, wish i could've been more of a help.
     
  8. sterynkng14

    sterynkng14

    Joined:
    Sep 19, 2013
    Posts:
    36
    Last edited: Aug 11, 2015