Advertisement
Munchy2007

PunTutPart3_6

Aug 18th, 2016
11,053
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. // #########################################################################################
  2. // This is a cut down version of the the Tank Movement script from the Tanks tutorial provided by Unity
  3. // https://www.assetstore.unity3d.com/en/#!/content/46209
  4. // #########################################################################################
  5. using UnityEngine;
  6.  
  7. namespace PUNTutorial
  8. {
  9.     public class PlayerMovement : Photon.PunBehaviour
  10.     {
  11.         public float m_Speed = 12f;                 // How fast the tank moves forward and back.
  12.         public float m_TurnSpeed = 180f;            // How fast the tank turns in degrees per second.
  13.         private Rigidbody m_Rigidbody;              // Reference used to move the tank.
  14.         private float m_MovementInputValue;         // The current value of the movement input.
  15.         private float m_TurnInputValue;             // The current value of the turn input.
  16.  
  17.  
  18.         private void Awake ()
  19.         {
  20.             m_Rigidbody = GetComponent<Rigidbody> ();
  21.  
  22.             if(!photonView.isMine)
  23.             {
  24.                 enabled = false;
  25.             }
  26.         }
  27.  
  28.  
  29.         private void OnEnable ()
  30.         {
  31.             // When the tank is turned on, make sure it's not kinematic.
  32.             m_Rigidbody.isKinematic = false;
  33.  
  34.             // Also reset the input values.
  35.             m_MovementInputValue = 0f;
  36.             m_TurnInputValue = 0f;
  37.         }
  38.  
  39.  
  40.         private void OnDisable ()
  41.         {
  42.             // When the tank is turned off, set it to kinematic so it stops moving.
  43.             m_Rigidbody.isKinematic = true;
  44.         }
  45.  
  46.         private void Update ()
  47.         {
  48.             // Store the value of both input axes.
  49.             m_MovementInputValue = Input.GetAxis ("Vertical");
  50.             m_TurnInputValue = Input.GetAxis ("Horizontal");
  51.         }
  52.  
  53.         private void FixedUpdate ()
  54.         {
  55.             // Adjust the rigidbodies position and orientation in FixedUpdate.
  56.             Move ();
  57.             Turn ();
  58.             m_Rigidbody.velocity = Vector3.zero;
  59.             m_Rigidbody.angularVelocity = Vector3.zero;
  60.         }
  61.  
  62.         private void Move ()
  63.         {
  64.             // Create a vector in the direction the tank is facing with a magnitude based on
  65.             // the input, speed and the time between frames.
  66.             Vector3 movement = transform.forward * m_MovementInputValue * m_Speed * Time.deltaTime;
  67.  
  68.             // Apply this movement to the rigidbody's position.
  69.             m_Rigidbody.MovePosition(m_Rigidbody.position + movement);
  70.         }
  71.  
  72.         private void Turn ()
  73.         {
  74.             // Determine the number of degrees to be turned based on the input,
  75.             // speed and time between frames.
  76.             float turn = m_TurnInputValue * m_TurnSpeed * Time.deltaTime;
  77.  
  78.             // Make this into a rotation in the y axis.
  79.             Quaternion turnRotation = Quaternion.Euler (0f, turn, 0f);
  80.  
  81.             // Apply this rotation to the rigidbody's rotation.
  82.             m_Rigidbody.MoveRotation (m_Rigidbody.rotation * turnRotation);
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement