Advertisement
aceroy

Project1

Mar 2nd, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.08 KB | None | 0 0
  1. //JM
  2. public class PlayerController : MonoBehaviour {
  3.     public float speed;
  4.     public float sensitivity;
  5.     public Slider healthBar;
  6.     public int health;
  7.     private Rigidbody rig;
  8.     public bool controlType;
  9.     float mouse;
  10.     float horiz;
  11.     float vert;
  12.     public Transform shotSpawn;
  13.     public GameObject bullet;
  14.  
  15.     // Use this for initialization
  16.     void Start () {
  17.         rig = GetComponent<Rigidbody>();
  18.         controlType = false;
  19.         health = 100;
  20.        
  21.     }
  22.    
  23.     // Update is called once per frame
  24.     void Update () {
  25.         healthBar.value = health;
  26.  
  27.         if (controlType == false)
  28.         {
  29.             vert = Input.GetAxis("Vertical");
  30.             horiz = Input.GetAxis("Horizontal");
  31.             mouse = Input.GetAxis("Mouse X");
  32.         }
  33.         else
  34.         {
  35.             vert = Input.GetAxis("Vertical2");
  36.             horiz = Input.GetAxis("Horizontal2");
  37.             mouse = Input.GetAxis("Mouse X2");
  38.         }
  39.         Vector3 whereToSpawn = new Vector3(shotSpawn.position.x, shotSpawn.position.y, shotSpawn.position.z);
  40.         Quaternion direction = new Quaternion(shotSpawn.rotation.x, shotSpawn.rotation.y, shotSpawn.rotation.z, shotSpawn.rotation.w);
  41.         if (health <= 0)
  42.             Destroy(this);
  43.         if (Input.GetKeyDown(KeyCode.Space))
  44.             Instantiate(bullet, whereToSpawn,direction);
  45.         if (Input.GetKeyDown(KeyCode.V) && controlType == false)
  46.             controlType = true;
  47.         else if (Input.GetKeyDown(KeyCode.V))
  48.             controlType = false;
  49.     }
  50.     private void FixedUpdate()
  51.     {
  52.  
  53.        rig.MovePosition(transform.position+ (transform.forward * vert * speed) + (transform.right * horiz * speed));
  54.        rig.MoveRotation(rig.rotation * Quaternion.Euler(new Vector3(0, mouse * sensitivity, 0)));
  55.         if (Input.GetKeyDown(KeyCode.C))
  56.         {
  57.             transform.rotation = new Quaternion(0, transform.rotation.y, 0, 0);
  58.             transform.position = new Vector3(transform.position.x, 1, transform.position.z);
  59.         }
  60.     }
  61. }
  62. public class Enemy : MonoBehaviour {
  63.     public int health;
  64.     public Slider healthBar;
  65.     public GameObject justhit;
  66.  
  67.     private void Start()
  68.     {
  69.         health = 100;
  70.        
  71.     }
  72.  
  73.     private void Update()
  74.     {
  75.         healthBar.value = health;
  76.         if (health <= 0)
  77.             Destroy(this.gameObject);
  78.     }
  79. }
  80. public class Bullet : MonoBehaviour {
  81.     public float speed;
  82.     public GameObject shot;
  83.     private Stopwatch sw;
  84.     // Use this for initialization
  85.     void Start () {
  86.         sw = new Stopwatch();
  87.         sw.Start();
  88.     }
  89.    
  90.     // Update is called once per frame
  91.     void Update () {
  92.         if (sw.ElapsedMilliseconds >= 10000)
  93.             Destroy(this.gameObject);
  94.     }
  95.     private void FixedUpdate()
  96.     {
  97.         transform.Translate(new Vector3(0, speed, 0));
  98.     }
  99.     private void OnTriggerEnter(Collider other)
  100.     {
  101.         shot = other.gameObject;
  102.         if(shot.CompareTag("Enemy"))
  103.         {
  104.             shot.GetComponent<Enemy>().health -= 10;
  105.         }
  106.         Destroy(this.gameObject);
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement