Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace PlayerHandler
- {
- public class PlayerHandler : MonoBehaviour
- {
- public float gridSize = 1f;//Determines grid size in meters. Default value is 1f.
- public float moveSpeed = 3f;//Determines normal walking speed. Default value is 3f, also stored into originalSpeed when run button is pressed.
- public float runSpeed = 9f;//Determines normal running speed. Default value is 9f.
- Vector3 destPos; //The target that the script attempts to move the player into
- public Vector3 nextPos; //A vector that is added to destPos
- public Vector3 direction; //The current direction that the player is facing
- public bool isLocked = false;
- Vector3 lastDirection;
- bool isMoving = false;
- bool isAnimMoving = false; //Determines if the walk animation should play
- private float turnTimer = 0.15f; //The min amount of time the player has to hold a key down before they start moving in seconds
- private float timePressed = 0f; //The timer used to see how long the player has held a movement key
- 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.
- [SerializeField]
- private Animator animator;
- void Start()
- {
- destPos = transform.position;
- nextPos = new Vector3(0,1,0.25f);
- lastDirection = Vector3.forward;
- if (moveSpeed != 0)
- {
- originalSpeed = moveSpeed;
- //Stores walking speed into temp variable. Used after the player lets go of the run button.
- }
- else if (moveSpeed == 0)
- {
- moveSpeed = 3f;
- originalSpeed = 3f;
- //Sets default value for walking if it's set to 0.
- }
- }
- // Update is called once per frame
- void Update()
- {
- if (Input.GetButton("Run") || Input.GetButton("Run2") || Input.GetButton("Run3")) //Running Behavior
- {
- moveSpeed = runSpeed;
- animator.speed = 3;
- }
- if (Input.GetButtonUp("Run") || Input.GetButtonUp("Run2") || Input.GetButtonUp("Run3")) //Returning to walking from running
- {
- moveSpeed = originalSpeed;
- animator.speed = 1;
- }
- //Timer to determine if the player will walk or if the player will turn in the same spot
- if (Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
- {
- timePressed += Time.deltaTime;
- }
- if (Input.GetButtonUp("Horizontal") || Input.GetButtonUp("Vertical"))
- {
- timePressed = 0f;
- }
- //Walking Movement Code
- if (transform.position == destPos)
- {
- if (!isLocked)
- {
- if (timePressed >= turnTimer)
- {
- if (Input.GetAxisRaw("Vertical") > 0) //Queues movement for walking forward
- {
- nextPos = Vector3.forward * gridSize;
- direction = Vector3.forward;
- isMoving = true;
- isAnimMoving = true;
- }
- else if (Input.GetAxisRaw("Vertical") < 0) //Queues movement for walking backward
- {
- nextPos = Vector3.back * gridSize;
- direction = Vector3.back;
- isMoving = true;
- isAnimMoving = true;
- }
- else if (Input.GetAxisRaw("Horizontal") < 0) //Queues movement for walking left
- {
- nextPos = Vector3.left * gridSize;
- direction = Vector3.left;
- isMoving = true;
- isAnimMoving = true;
- }
- else if (Input.GetAxisRaw("Horizontal") > 0) //Queues movement for walking right
- {
- nextPos = Vector3.right * gridSize;
- direction = Vector3.right;
- isMoving = true;
- isAnimMoving = true;
- }
- }
- if (timePressed <= turnTimer && !isMoving)
- {
- 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
- {
- lastDirection = direction;
- direction = Vector3.forward;
- isMoving = false;
- isAnimMoving = true;
- }
- 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
- {
- lastDirection = direction;
- direction = Vector3.back;
- isMoving = false;
- isAnimMoving = true;
- }
- 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
- {
- lastDirection = direction;
- direction = Vector3.left;
- isMoving = false;
- isAnimMoving = true;
- }
- 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
- {
- lastDirection = direction;
- direction = Vector3.right;
- isMoving = false;
- isAnimMoving = true;
- }
- }
- if (Input.GetAxisRaw("Horizontal") == 0 && Input.GetAxisRaw("Vertical") == 0) //Sets the player into an idle state when no input is received
- {
- isMoving = false;
- isAnimMoving = false;
- }
- }
- }
- //Walking Animation Code
- animator.SetFloat("Horizontal", direction.x);
- animator.SetFloat("Vertical", direction.z);
- animator.SetBool("IsAnimMoving", isAnimMoving);
- transform.position = Vector3.MoveTowards(transform.position, destPos, Time.deltaTime * moveSpeed); //Moves the player from their current position to destPos.
- //Movement Operations
- if (Vector3.Distance(destPos, transform.position) <= 0.00001f)
- {
- if (isMoving)
- {
- if (CanMove()) //Uses raycast to determine if the player can move into the next tile using GameObject tags
- {
- destPos = transform.position + nextPos;
- isMoving = false;
- }
- }
- }
- }
- bool CanMove()
- {
- Ray myRay = new Ray(transform.position + new Vector3(0, 0.5f, 0), direction);
- RaycastHit hit;
- Debug.DrawRay(myRay.origin, myRay.direction, Color.magenta);
- if (Physics.Raycast(myRay, out hit, gridSize))
- {
- if (hit.collider.tag == "Object" || hit.collider.tag == "NPC" || hit.collider.tag == "MapCollision")
- {
- return false;
- }
- }
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement