Advertisement
Guest User

Untitled

a guest
Feb 15th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour
  6. {
  7.     public bool HaveBeamShot = false;
  8.  
  9.     [SerializeField]
  10.     private GameObject _LasersPrefab;
  11.  
  12.     [SerializeField]
  13.     private float _FireRate = 0.25f;
  14.     private float _CanFire = 0.0f;
  15.  
  16.     [SerializeField]
  17.     private GameObject _BeamShotPrefab;
  18.  
  19.     [SerializeField]
  20.     private float _FireRateBeam = 1.0f;
  21.     private float _CanFireBeam = 0.0f;
  22.  
  23.     private float _Speed = 10.0f;
  24.  
  25.  
  26.  
  27.     void Start()
  28.     {
  29.         transform.position = new Vector3(0, -2.1f, 0);
  30.     }
  31.  
  32.     void Update()
  33.     {
  34.         Movement();
  35.  
  36.         if (HaveBeamShot == false)
  37.         {
  38.             if (Input.GetKey(KeyCode.C))
  39.             {
  40.                 Lasers();
  41.             }
  42.         }
  43.         else
  44.         {
  45.             if (Input.GetKey(KeyCode.C))
  46.             {
  47.                 BeamShot();
  48.             }
  49.         }
  50.     }
  51.  
  52.     void Lasers()
  53.     {
  54.         if (Time.time > _CanFire)
  55.         {
  56.             Instantiate(_LasersPrefab, transform.position + new Vector3(0, 1.1f, 0), Quaternion.identity);
  57.             _CanFire = Time.time + _FireRate;
  58.         }
  59.     }
  60.  
  61.     void BeamShot()
  62.     {
  63.             if (Time.time > _CanFireBeam)
  64.             {
  65.                 Instantiate(_BeamShotPrefab, transform.position + new Vector3(0, 7.2f, 0), Quaternion.identity);
  66.                 _CanFireBeam = Time.time + _FireRateBeam;
  67.             }
  68.     }
  69.  
  70.     void Movement()
  71.     {
  72.         // Horizontal movement
  73.         float HorizontalInput = Input.GetAxis("Horizontal");
  74.         transform.Translate(Vector3.right * _Speed * HorizontalInput * Time.deltaTime);
  75.  
  76.         // Vertical movement
  77.         float VerticalInput = Input.GetAxis("Vertical");
  78.         transform.Translate(Vector3.up * _Speed * VerticalInput * Time.deltaTime);
  79.  
  80.         // Bounds
  81.         if (transform.position.y > 0)
  82.         {
  83.             transform.position = new Vector3(transform.position.x, 0, 0);
  84.         }
  85.         else if (transform.position.y < -4.2f)
  86.         {
  87.             transform.position = new Vector3(transform.position.x, -4.2f, 0);
  88.         }
  89.  
  90.         // Wrap
  91.         if (transform.position.x > 10.5f)
  92.         {
  93.             transform.position = new Vector3(-10.5f, transform.position.y, 0);
  94.         }
  95.         else if (transform.position.x < -10.5f)
  96.         {
  97.             transform.position = new Vector3(10.5f, transform.position.y, 0);
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement