ltrout

PlayerMovement

Dec 24th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.16 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using EnemyInfo;
  4. using Shell;
  5.  
  6. namespace PlayerInfo
  7. {
  8.     public class PlayerMovement : MonoBehaviour
  9.     {
  10.         public int PlayerNumber = 1;
  11.         public float Speed = 10f;
  12.         public float TurnSpeed = 180f;
  13.         public AudioSource MovementAudio;
  14.         public AudioClip EngineIdling;
  15.         public AudioClip EngineDriving;
  16.  
  17.         string MovementAxisName;
  18.         string TurnAxisName;
  19.         Rigidbody Rigidbody;
  20.         float MovementInputValue;
  21.         float TurnInputValue;
  22.  
  23.         void Awake ()
  24.         {
  25.             Rigidbody = GetComponent<Rigidbody> ();
  26.         }
  27.  
  28.         void OnEnable ()
  29.         {
  30.             Rigidbody.isKinematic = false;
  31.  
  32.             MovementInputValue = 0f;
  33.             TurnInputValue = 0f;
  34.         }
  35.  
  36.         void OnDisable ()
  37.         {
  38.             Rigidbody.isKinematic = true;
  39.         }
  40.  
  41.         void Start ()
  42.         {
  43.             MovementAxisName = "Vertical";
  44.             TurnAxisName = "Horizontal";
  45.         }
  46.  
  47.         void Update ()
  48.         {
  49.             MovementInputValue = Input.GetAxis (MovementAxisName);
  50.             TurnInputValue = Input.GetAxis (TurnAxisName);
  51.  
  52.             EngineAudio ();
  53.         }
  54.  
  55.         void EngineAudio ()
  56.         {
  57.             // If there is no input (the tank is stationary)...
  58.             if (Mathf.Abs (MovementInputValue) < 0.1f && Mathf.Abs (TurnInputValue) < 0.1f)
  59.             {
  60.                 // ... and if the audio source is currently playing the driving clip...
  61.                 if (MovementAudio.clip == EngineDriving)
  62.                 {
  63.                     // ... change the clip to idling and play it.
  64.                     MovementAudio.clip = EngineIdling;
  65.                     MovementAudio.Play ();
  66.                 }
  67.             }
  68.             else
  69.             {
  70.                 // Otherwise if the tank is moving and if the idling clip is currently playing...
  71.                 if (MovementAudio.clip == EngineIdling)
  72.                 {
  73.                     // ... change the clip to driving and play.
  74.                     MovementAudio.clip = EngineDriving;
  75.                     MovementAudio.Play();
  76.                 }
  77.             }
  78.         }
  79.  
  80.         void FixedUpdate ()
  81.         {
  82.             Move ();
  83.             Turn ();
  84.         }
  85.  
  86.         void Move ()
  87.         {
  88.             Vector3 movement = transform.forward * MovementInputValue * Speed * Time.deltaTime;
  89.  
  90.             Rigidbody.MovePosition (Rigidbody.position + movement);
  91.         }
  92.  
  93.         void Turn ()
  94.         {
  95.             float turn = TurnInputValue * TurnSpeed * Time.deltaTime;
  96.  
  97.             Quaternion turnRotation = Quaternion.Euler (0f, turn, 0f);
  98.  
  99.             Rigidbody.MoveRotation (Rigidbody.rotation * turnRotation);
  100.         }
  101.  
  102.     }
  103. }
Add Comment
Please, Sign In to add comment