ltrout

PlayerMovement

Apr 14th, 2016
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.28 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using EnemyInfo;
  4. using Shell;
  5. using UnityEngine.UI;
  6. using System.Collections.Generic;
  7.  
  8. namespace PlayerInfo
  9. {
  10.     public class PlayerMovement : MonoBehaviour
  11.     {
  12.         public float Speed = 10f;
  13.         public float TurnSpeed = 180f;
  14.         public AudioSource MovementAudio;
  15.         public AudioClip EngineIdling;
  16.         public AudioClip EngineDriving;
  17.         public Joystick joystick;
  18.  
  19.         string MovementAxisName;
  20.         string TurnAxisName;
  21.         Rigidbody Rigidbody;
  22.         float MovementInputValue;
  23.         float TurnInputValue;
  24.         //PowerUps PowerUp;
  25.  
  26.         void Awake ()
  27.         {
  28.             Rigidbody = GetComponent<Rigidbody> ();
  29.         }
  30.  
  31.         void OnEnable ()
  32.         {
  33.             Rigidbody.isKinematic = false;
  34.  
  35.             MovementInputValue = 0f;
  36.             TurnInputValue = 0f;
  37.         }
  38.  
  39.         void OnDisable ()
  40.         {
  41.             Rigidbody.isKinematic = true;
  42.         }
  43.  
  44.         void Start ()
  45.         {
  46.             MovementAxisName = "Vertical";
  47.             TurnAxisName = "Horizontal";
  48.         }
  49.  
  50.         void Update ()
  51.         {
  52.             MovementInputValue = joystick.Horizontal();
  53.             TurnInputValue = joystick.Vertical();
  54.  
  55.             //MovementInputValue = Input.GetAxis (MovementAxisName);
  56.             //TurnInputValue = Input.GetAxis (TurnAxisName);
  57.  
  58.             EngineAudio ();
  59.         }
  60.  
  61.         void EngineAudio ()
  62.         {
  63.             // If there is no input (the tank is stationary)...
  64.             if (Mathf.Abs (MovementInputValue) < 0.1f && Mathf.Abs (TurnInputValue) < 0.1f)
  65.             {
  66.                 // ... and if the audio source is currently playing the driving clip...
  67.                 if (MovementAudio.clip == EngineDriving)
  68.                 {
  69.                     // ... change the clip to idling and play it.
  70.                     MovementAudio.clip = EngineIdling;
  71.                     MovementAudio.Play ();
  72.                 }
  73.             }
  74.             else
  75.             {
  76.                 // Otherwise if the tank is moving and if the idling clip is currently playing...
  77.                 if (MovementAudio.clip == EngineIdling)
  78.                 {
  79.                     // ... change the clip to driving and play.
  80.                     MovementAudio.clip = EngineDriving;
  81.                     MovementAudio.Play();
  82.                 }
  83.             }
  84.         }
  85.  
  86.         void FixedUpdate ()
  87.         {
  88.             Move ();
  89.             Turn ();
  90.         }
  91.  
  92.         void Move ()
  93.         {
  94.             Vector3 movement = transform.forward * MovementInputValue * Speed * Time.deltaTime;
  95.  
  96.             Rigidbody.MovePosition (Rigidbody.position + movement);
  97.         }
  98.  
  99.         void Turn ()
  100.         {
  101.             float turn = TurnInputValue * TurnSpeed * Time.deltaTime;
  102.  
  103.             Quaternion turnRotation = Quaternion.Euler (0f, turn, 0f);
  104.  
  105.             Rigidbody.MoveRotation (Rigidbody.rotation * turnRotation);
  106.         }
  107.  
  108.         /*void OnTriggerEnter (Collider collider)
  109.         {
  110.              List<int> PowerUpsPossible = new List<int> ();
  111.  
  112.             if (collider.CompareTag("PowerUp"))
  113.             {
  114.                 PowerUp = collider.gameObject.GetComponent<PowerUps> ();
  115.  
  116.                 if (PowerUp.isActivable_speed)
  117.                 {
  118.                     PowerUpsPossible.Add (1);
  119.                 }
  120.                 if (PowerUp.isActivable_health)
  121.                 {
  122.                     PowerUpsPossible.Add (2);
  123.                 }
  124.                 if (PowerUp.isActivable_damage)
  125.                 {
  126.                     PowerUpsPossible.Add (3);
  127.                 }
  128.             }
  129.             if (PowerUpsPossible.Count < 1)
  130.             {
  131.                 StartCoroutine(PowerUp.PowerUpMessage("ALL POWERUPS ON COOLDOWN", 4));
  132.             }
  133.  
  134.             int PowerUpRand = Random.Range (0, PowerUpsPossible.Count);
  135.  
  136.             switch (PowerUpsPossible [PowerUpRand])
  137.             {
  138.                 case 1:
  139.                     StartCoroutine (PowerUp.PlayerSpeedPowerUp());
  140.                     break;
  141.                 case 2:
  142.                     StartCoroutine (PowerUp.PlayerHealthPowerUp());
  143.                     break;
  144.                 case 3:
  145.                     StartCoroutine (PowerUp.PlayerDamagePowerUp());
  146.                     break;
  147.             }
  148.         }*/
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment