Eriknem

shooter

Nov 6th, 2016
357
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2.  
  3. public class PlayerMovement : MonoBehaviour
  4. {
  5.     public float speed = 6f;            // The speed that the player will move at.
  6.  
  7.     Vector3 movement;                   // The vector to store the direction of the player's movement.
  8.     Animator anim;                      // Reference to the animator component.
  9.     Rigidbody playerRigidbody;          // Reference to the player's rigidbody.
  10.     int floorMask;                      // A layer mask so that a ray can be cast just at gameobjects on the floor layer.
  11.     float camRayLength = 100f;          // The length of the ray from the camera into the scene.
  12.  
  13.     void Awake ()
  14.     {
  15.         // Create a layer mask for the floor layer.
  16.         floorMask = LayerMask.GetMask ("Floor");
  17.  
  18.         // Set up references.
  19.         anim = GetComponent <Animator> ();
  20.         playerRigidbody = GetComponent <Rigidbody> ();
  21.     }
  22.  
  23.  
  24.     void FixedUpdate ()
  25.     {
  26.         // Store the input axes.
  27.         float h = Input.GetAxisRaw ("Horizontal");
  28.         float v = Input.GetAxisRaw ("Vertical");
  29.  
  30.         // Move the player around the scene.
  31.         Move (h, v);
  32.  
  33.         // Turn the player to face the mouse cursor.
  34.         Turning ();
  35.  
  36.         // Animate the player.
  37.         Animating (h, v);
  38.     }
  39.  
  40.     void Move (float h, float v)
  41.     {
  42.         // Set the movement vector based on the axis input.
  43.         movement.Set (h, 0f, v);
  44.        
  45.         // Normalise the movement vector and make it proportional to the speed per second.
  46.         movement = movement.normalized * speed * Time.deltaTime;
  47.  
  48.         // Move the player to it's current position plus the movement.
  49.         playerRigidbody.MovePosition (transform.position + movement);
  50.     }
  51.  
  52.     void Turning ()
  53.     {
  54.         // Create a ray from the mouse cursor on screen in the direction of the camera.
  55.         Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
  56.  
  57.         // Create a RaycastHit variable to store information about what was hit by the ray.
  58.         RaycastHit floorHit;
  59.  
  60.         // Perform the raycast and if it hits something on the floor layer...
  61.         if(Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
  62.         {
  63.             // Create a vector from the player to the point on the floor the raycast from the mouse hit.
  64.             Vector3 playerToMouse = floorHit.point - transform.position;
  65.  
  66.             // Ensure the vector is entirely along the floor plane.
  67.             playerToMouse.y = 0f;
  68.  
  69.             // Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
  70.             Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
  71.  
  72.             // Set the player's rotation to this new rotation.
  73.             playerRigidbody.MoveRotation (newRotation);
  74.         }
  75.     }
  76.  
  77.     void Animating (float h, float v)
  78.     {
  79.         // Create a boolean that is true if either of the input axes is non-zero.
  80.         bool walking = h != 0f || v != 0f;
  81.  
  82.         // Tell the animator whether or not the player is walking.
  83.         anim.SetBool ("IsWalking", walking);
  84.     }
  85. }
RAW Paste Data