Advertisement
Ch0nG

Player

Aug 21st, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.49 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Player : MonoBehaviour {
  5.     private Transform TS;
  6.     public int pspeed = 8;
  7.     public static int playerLives = 3;
  8.     public static int score = 0;
  9.  
  10.     float timer = 0f;
  11.  
  12.     //variable to reference prefab.
  13.     public GameObject LaserFab;
  14.  
  15.     // Use this for initialization
  16.     void Start () {
  17.  
  18.         TS = transform;
  19.  
  20.         TS.position = new Vector3(0, -3.7f, 0); //Spawn point
  21.     }
  22.    
  23.     // Update is called once per frame
  24.     void Update () {
  25.         TS.Translate(Vector3.right * Input.GetAxis("Horizontal") * pspeed * Time.deltaTime);
  26.  
  27.         if (TS.position.x>11.25f || TS.position.x<-11.25f) {
  28.             TS.position = new Vector3 (TS.position.x*-1, TS.position.y, TS.position.z);
  29.         }
  30.  
  31.         //Fire a laser with the Spacebar.
  32.         if (Input.GetKeyDown("space")) {
  33.  
  34.             //Set Laser position
  35.             Vector3 position = new Vector3(TS.position.x, TS.position.y+.8f, TS.position.z);
  36.  
  37.             //Fire projectile.
  38.             Instantiate(LaserFab, position, Quaternion.identity);
  39.         }
  40.  
  41.         if (Time.time - timer > 0.4f) {
  42.             renderer.enabled = true;
  43.         }
  44.  
  45.         print ("Lives remaining: " + playerLives + " Score: " + score + " Current playing time: " + Time.time);
  46.     }
  47.  
  48.     void OnTriggerEnter(Collider collider)
  49.     {
  50.         if (collider.CompareTag ("Enemy")) { //Tag is name of prefab.
  51.             //When the enemy hits the player, destroy the player and the enemy.
  52.             playerLives--;
  53.             renderer.enabled = false;
  54.             timer = Time.time;
  55.             print (playerLives);
  56.         }
  57.  
  58.         if (playerLives < 1)
  59.             Destroy(this.gameObject);
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement