Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.32 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. namespace PlayerHandler
  6. {
  7.  
  8.     public class PlayerHandler : MonoBehaviour
  9.     {
  10.  
  11.         public float gridSize = 1f;//Determines grid size in meters. Default value is 1f.
  12.         public float moveSpeed = 3f;//Determines normal walking speed. Default value is 3f, also stored into originalSpeed when run button is pressed.
  13.         public float runSpeed = 9f;//Determines normal running speed. Default value is 9f.
  14.  
  15.         Vector3 destPos; //The target that the script attempts to move the player into
  16.         public Vector3 nextPos; //A vector that is added to destPos
  17.         public Vector3 direction; //The current direction that the player is facing
  18.         public bool isLocked = false;
  19.         Vector3 lastDirection;
  20.         bool isMoving = false;
  21.         bool isAnimMoving = false; //Determines if the walk animation should play
  22.  
  23.         private float turnTimer = 0.15f; //The min amount of time the player has to hold a key down before they start moving in seconds
  24.         private float timePressed = 0f; //The timer used to see how long the player has held a movement key
  25.         private float originalSpeed; //The temp container for walking speed while the player is running. Resets the walk speed to this value after they let go of the run button.
  26.  
  27.         [SerializeField]
  28.         private Animator animator;
  29.  
  30.         void Start()
  31.         {
  32.             destPos = transform.position;
  33.             nextPos = new Vector3(0,1,0.25f);
  34.             lastDirection = Vector3.forward;
  35.             if (moveSpeed != 0)
  36.             {
  37.                 originalSpeed = moveSpeed;
  38.                 //Stores walking speed into temp variable. Used after the player lets go of the run button.
  39.             }
  40.             else if (moveSpeed == 0)
  41.             {
  42.                 moveSpeed = 3f;
  43.                 originalSpeed = 3f;
  44.                 //Sets default value for walking if it's set to 0.
  45.             }
  46.         }
  47.  
  48.         // Update is called once per frame
  49.         void Update()
  50.         {
  51.             if (Input.GetButton("Run") || Input.GetButton("Run2") || Input.GetButton("Run3")) //Running Behavior
  52.             {
  53.                 moveSpeed = runSpeed;
  54.                 animator.speed = 3;
  55.             }
  56.             if (Input.GetButtonUp("Run") || Input.GetButtonUp("Run2") || Input.GetButtonUp("Run3")) //Returning to walking from running
  57.             {
  58.                 moveSpeed = originalSpeed;
  59.                 animator.speed = 1;
  60.             }
  61.  
  62.             //Timer to determine if the player will walk or if the player will turn in the same spot
  63.             if (Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
  64.             {
  65.                 timePressed += Time.deltaTime;
  66.             }
  67.             if (Input.GetButtonUp("Horizontal") || Input.GetButtonUp("Vertical"))
  68.             {
  69.                 timePressed = 0f;
  70.             }
  71.  
  72.             //Walking Movement Code
  73.             if (transform.position == destPos)
  74.             {
  75.                 if (!isLocked)
  76.                 {
  77.                     if (timePressed >= turnTimer)
  78.                     {
  79.                         if (Input.GetAxisRaw("Vertical") > 0) //Queues movement for walking forward
  80.                         {
  81.                             nextPos = Vector3.forward * gridSize;
  82.                             direction = Vector3.forward;
  83.                             isMoving = true;
  84.                             isAnimMoving = true;
  85.                         }
  86.                         else if (Input.GetAxisRaw("Vertical") < 0) //Queues movement for walking backward
  87.                         {
  88.                             nextPos = Vector3.back * gridSize;
  89.                             direction = Vector3.back;
  90.                             isMoving = true;
  91.                             isAnimMoving = true;
  92.                         }
  93.                         else if (Input.GetAxisRaw("Horizontal") < 0) //Queues movement for walking left
  94.                         {
  95.                             nextPos = Vector3.left * gridSize;
  96.                             direction = Vector3.left;
  97.                             isMoving = true;
  98.                             isAnimMoving = true;
  99.                         }
  100.                         else if (Input.GetAxisRaw("Horizontal") > 0) //Queues movement for walking right
  101.                         {
  102.                             nextPos = Vector3.right * gridSize;
  103.                             direction = Vector3.right;
  104.                             isMoving = true;
  105.                             isAnimMoving = true;
  106.                         }
  107.                     }
  108.                     if (timePressed <= turnTimer && !isMoving)
  109.                     {
  110.                         if (Input.GetAxisRaw("Vertical") > 0 && direction != Vector3.forward) //Turns the player forward, checks to see if the last direction was the same as this one to prevent taking a step without moving a tile
  111.                         {
  112.                             lastDirection = direction;
  113.                             direction = Vector3.forward;
  114.                             isMoving = false;
  115.                             isAnimMoving = true;
  116.                         }
  117.                         else if (Input.GetAxisRaw("Vertical") < 0 && direction != Vector3.back) //Turns the player backward, checks to see if the last direction was the same as this one to prevent taking a step without moving a tile
  118.                         {
  119.                             lastDirection = direction;
  120.                             direction = Vector3.back;
  121.                             isMoving = false;
  122.                             isAnimMoving = true;
  123.                         }
  124.                         else if (Input.GetAxisRaw("Horizontal") < 0 && direction != Vector3.left) //Turns the player left, checks to see if the last direction was the same as this one to prevent taking a step without moving a tile
  125.                         {
  126.                             lastDirection = direction;
  127.                             direction = Vector3.left;
  128.                             isMoving = false;
  129.                             isAnimMoving = true;
  130.                         }
  131.                         else if (Input.GetAxisRaw("Horizontal") > 0 && direction != Vector3.right) //Turns the player right, checks to see if the last direction was the same as this one to prevent taking a step without moving a tile
  132.                         {
  133.                             lastDirection = direction;
  134.                             direction = Vector3.right;
  135.                             isMoving = false;
  136.                             isAnimMoving = true;
  137.                         }
  138.                     }
  139.  
  140.                     if (Input.GetAxisRaw("Horizontal") == 0 && Input.GetAxisRaw("Vertical") == 0) //Sets the player into an idle state when no input is received
  141.                     {
  142.                         isMoving = false;
  143.                     isAnimMoving = false;
  144.                     }
  145.                 }
  146.             }
  147.  
  148.             //Walking Animation Code
  149.             animator.SetFloat("Horizontal", direction.x);
  150.             animator.SetFloat("Vertical", direction.z);
  151.             animator.SetBool("IsAnimMoving", isAnimMoving);
  152.  
  153.             transform.position = Vector3.MoveTowards(transform.position, destPos, Time.deltaTime * moveSpeed); //Moves the player from their current position to destPos.
  154.  
  155.             //Movement Operations
  156.             if (Vector3.Distance(destPos, transform.position) <= 0.00001f)
  157.             {
  158.                 if (isMoving)
  159.                 {
  160.                     if (CanMove()) //Uses raycast to determine if the player can move into the next tile using GameObject tags
  161.                     {
  162.                         destPos = transform.position + nextPos;
  163.                         isMoving = false;
  164.                     }
  165.                 }
  166.             }
  167.         }
  168.         bool CanMove()
  169.         {
  170.             Ray myRay = new Ray(transform.position + new Vector3(0, 0.5f, 0), direction);
  171.             RaycastHit hit;
  172.             Debug.DrawRay(myRay.origin, myRay.direction, Color.magenta);
  173.  
  174.             if (Physics.Raycast(myRay, out hit, gridSize))
  175.             {
  176.                 if (hit.collider.tag == "Object" || hit.collider.tag == "NPC" || hit.collider.tag == "MapCollision")
  177.                 {
  178.                     return false;
  179.                 }
  180.             }
  181.             return true;
  182.         }
  183.     }
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement