Hi I’m trying to convert my shooter from windows to andoid but It doesn’t work the touch input
My problem isn’t the shot but the movement I woulld a movement which respect matfh clamp but free in the screen.
Someone could help?
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, yMin, yMax; //Screen Boundary dimentions
}
public class Player_Script : MonoBehaviour
{
//Public Var
public float speed; //Player Ship Speed
public Boundary boundary; //make an Object from Class Boundary
public GameObject shot; //Fire Prefab
public Transform shotSpawn; //Where the Fire Spawn
public float fireRate = 0.5F; //Fire Rate between Shots
public GameObject Explosion; //Explosion Prefab
public GameObject restartDialog; //eND gAME
public GameObject heart1; // heart icon #1
public GameObject heart2; // heart icon #2
public GameObject heart3; // heart icon #3
//Private Var
private float nextFire = 0.0F; //First fire & Next fire Time
private bool dead;
private Shield shield;
private int hits = 0;
private Rigidbody2D rb;
private Vector3 pos; //Position
void Start ()
{
restartDialog.SetActive(false);
rb = GetComponent<Rigidbody2D>();
shield = GameObject.Find("Shield").GetComponent<Shield>();
HeartsCount(); // draw the right number of hearts on the screen
hits = shield.lives; // make hits equal to shield.lives to start
}
// Update is called once per frame
void MoveUpdate()
{
//If the game is running on a android device
if (Application.platform == RuntimePlatform.Android)
{
//Get screen position
pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 1));
}
}
void Update ()
{
//Excute When the Current Time is bigger than the nextFire time
if (Time.time > nextFire)
{
nextFire = Time.time + fireRate; //Increment nextFire time with the current system time + fireRate
Instantiate (shot , shotSpawn.position ,shotSpawn.rotation); //Instantiate fire shot
GetComponent<AudioSource>().Play (); //Play Fire sound
}
if(hits != shield.lives)
{
HeartsCount();
}
}
// FixedUpdate is called one per specific time
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal"); //Get if Any Horizontal Keys pressed
float moveVertical = Input.GetAxis ("Vertical"); //Get if Any Vertical Keys pressed
Vector2 movement = new Vector2 (moveHorizontal, moveVertical); //Put them in a Vector2 Variable (x,y)
GetComponent<Rigidbody2D>().velocity = movement * speed; //Add Velocity to the player ship rigidbody
//Lock the position in the screen by putting a boundaries
GetComponent<Rigidbody2D>().position = new Vector2
(
Mathf.Clamp (GetComponent<Rigidbody2D>().position.x, boundary.xMin, boundary.xMax), //X
Mathf.Clamp (GetComponent<Rigidbody2D>().position.y, boundary.yMin, boundary.yMax) //Y
);
}
//Called when the Trigger entered
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Shield") // if we encounter a shield pickup...
{
shield.resetShield(); // reset shield durability to max
HeartsCount(); // draw the right number of hearts on the screen
// Debug.Log(gameObject.name + " got shields!");
Destroy(other.gameObject); // and destroy the shield pickup object.
}
//Excute if the object tag was equal to one of these
if(other.tag == "Enemy" || other.tag == "Asteroid" || other.tag == "EnemyShot") // if we have no shields & we're hit by an enemy, bullet, or asteroid
{
// Debug.Log(gameObject.name + " has hit an " + other.gameObject.name);
HeartsCount(); // draw the right number of hearts on the screen
if (shield.lives == 0) // if we're out of shields, explode, self-destruct, and show the restart dialog.
{
// Debug.Log("Player has no shields left! Destroying player!");
Instantiate (Explosion, transform.position , transform.rotation);
//Trigger That its a GameOver
Destroy(gameObject);
restartDialog.SetActive(true);
}
}
}
void HeartsCount()
{
hits = shield.lives;
if(shield.lives == 3) // if we have full shields (3)...
{
heart3.gameObject.GetComponent<SpriteRenderer>().enabled = true; // turn on heart #3.
heart2.gameObject.GetComponent<SpriteRenderer>().enabled = true; // turn on heart #2.
heart1.gameObject.GetComponent<SpriteRenderer>().enabled = true; // turn on heart #1.
}
if(shield.lives == 2) // if shields are at 2...
{
heart3.gameObject.GetComponent<SpriteRenderer>().enabled = false; // turn off heart #3.
heart2.gameObject.GetComponent<SpriteRenderer>().enabled = true; // turn on heart #2.
heart1.gameObject.GetComponent<SpriteRenderer>().enabled = true; // turn on heart #1.
}
if(shield.lives == 1) // if shields are at 1...
{
heart3.gameObject.GetComponent<SpriteRenderer>().enabled = false; // turn off heart #3.
heart2.gameObject.GetComponent<SpriteRenderer>().enabled = false; // turn off heart #2.
heart1.gameObject.GetComponent<SpriteRenderer>().enabled = true; // turn on heart #1.
}
if(shield.lives == 0) // if shields are at 0...
{
heart3.gameObject.GetComponent<SpriteRenderer>().enabled = false; // turn off heart #3.
heart2.gameObject.GetComponent<SpriteRenderer>().enabled = false; // turn off heart #2.
heart1.gameObject.GetComponent<SpriteRenderer>().enabled = false; // turn off heart #1.
}
}
public void RestartMenu()
{
Application.LoadLevel (Application.loadedLevelName);
}
public void ExitToMenu()
{
Application.LoadLevel ("Menu");
}
}