Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Player : MonoBehaviour {
- private Transform TS;
- public int pspeed = 8;
- public static int playerLives = 3;
- public static int score = 0;
- float timer = 0f;
- //variable to reference prefab.
- public GameObject LaserFab;
- // Use this for initialization
- void Start () {
- TS = transform;
- TS.position = new Vector3(0, -3.7f, 0); //Spawn point
- }
- // Update is called once per frame
- void Update () {
- TS.Translate(Vector3.right * Input.GetAxis("Horizontal") * pspeed * Time.deltaTime);
- if (TS.position.x>11.25f || TS.position.x<-11.25f) {
- TS.position = new Vector3 (TS.position.x*-1, TS.position.y, TS.position.z);
- }
- //Fire a laser with the Spacebar.
- if (Input.GetKeyDown("space")) {
- //Set Laser position
- Vector3 position = new Vector3(TS.position.x, TS.position.y+.8f, TS.position.z);
- //Fire projectile.
- Instantiate(LaserFab, position, Quaternion.identity);
- }
- if (Time.time - timer > 0.4f) {
- renderer.enabled = true;
- }
- print ("Lives remaining: " + playerLives + " Score: " + score + " Current playing time: " + Time.time);
- }
- void OnTriggerEnter(Collider collider)
- {
- if (collider.CompareTag ("Enemy")) { //Tag is name of prefab.
- //When the enemy hits the player, destroy the player and the enemy.
- playerLives--;
- renderer.enabled = false;
- timer = Time.time;
- print (playerLives);
- }
- if (playerLives < 1)
- Destroy(this.gameObject);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement