ltrout

PlayerAttack

Dec 24th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.66 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using EnemyInfo;
  5. using Shell;
  6.  
  7. namespace PlayerInfo
  8. {
  9.     public class PlayerAttack : MonoBehaviour
  10.     {
  11.         public int PlayerNumber = 1;
  12.         public Rigidbody Shell;
  13.         public Transform FireTransform;
  14.         public Slider AimSlider;
  15.         public AudioSource ShootingAudio;
  16.         public AudioClip ChargingClip;
  17.         public AudioClip FireClip;
  18.         public float MinForce = 15f;
  19.         public float MaxForce = 30f;
  20.         public float MaxCharge = 0.75f;
  21.  
  22.         string FireButton;
  23.         float CurrentForce;
  24.         float ChargeSpeed;
  25.         bool Fired;
  26.  
  27.         void OnEnable ()
  28.         {
  29.             CurrentForce = MinForce;
  30.             AimSlider.value = MinForce;
  31.         }
  32.  
  33.         void Start ()
  34.         {
  35.             FireButton = "Fire";
  36.  
  37.             ChargeSpeed = (MaxForce - MinForce) / MaxCharge;
  38.         }
  39.  
  40.         void Update ()
  41.         {
  42.             AimSlider.value = MinForce;
  43.  
  44.             if (CurrentForce >= MaxForce && !Fired)
  45.             {
  46.                 CurrentForce = MaxForce;
  47.                 Fire ();
  48.             }
  49.             else if (Input.GetButtonDown (FireButton))
  50.             {
  51.                 Fired = false;
  52.                 CurrentForce = MinForce;
  53.  
  54.                 ShootingAudio.clip = ChargingClip;
  55.                 ShootingAudio.Play ();
  56.             }
  57.             else if (Input.GetButton (FireButton) && !Fired)
  58.             {
  59.                 CurrentForce += ChargeSpeed * Time.deltaTime;
  60.  
  61.                 AimSlider.value = CurrentForce;
  62.             }
  63.             else if (Input.GetButtonUp (FireButton) && !Fired)
  64.             {
  65.                 Fire ();
  66.             }
  67.         }
  68.  
  69.         void Fire ()
  70.         {
  71.             Fired = true;
  72.  
  73.             Rigidbody shellInstance = Instantiate (Shell, FireTransform.position, FireTransform.rotation) as Rigidbody;
  74.  
  75.             shellInstance.velocity = CurrentForce * FireTransform.forward;
  76.  
  77.             ShootingAudio.clip = FireClip;
  78.             ShootingAudio.Play ();
  79.  
  80.             CurrentForce = MinForce;
  81.         }
  82.     }
  83. }
Add Comment
Please, Sign In to add comment