Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace GP
- {
- public class PlayerLocoMotion : MonoBehaviour
- {
- CameraHandler cameraHandler;
- PlayerManager playerManager;
- Transform cameraObject;
- InputHandler inputHandler;
- public Vector3 moveDirection;
- PlayerStats playerStats;
- [HideInInspector]
- public Transform myTransform;
- [HideInInspector]
- public PlayerAnimatorManager animatorHandler;
- public new Rigidbody rigidbody;
- public GameObject normalCamera;
- [Header("Stats")]
- [SerializeField]
- float movementSpeed = 1;
- public float jogSpeed = 5;
- [SerializeField]
- float rotationSpeed = 10;
- public float sprintSpeed = 7;
- public bool isDodgingForward;
- public bool isDodgingBack;
- public bool AttackingForceForward;
- public Transform hips;
- [Header("Roll Costs")]
- public int rollStaminaCost = 15;
- public int backstepStaminaCost = 12;
- public CapsuleCollider characterCollider;
- public CapsuleCollider characterCollisionBlocker;
- [Header("GroundAndAirDetection")]
- public float groundDetectionRayStartPoint = 0.5f;
- float minimumDistanceNeededToBeginFall = 1f;
- float groundDetectionRayDistance = -0.2f;
- LayerMask ignoreForGroundCheck;
- public float inAirTimer;
- public float fallingSpeed = 45;
- [Header("SprintCost")]
- public int sprintStaminaCost = 1;
- // Start is called before the first frame update
- void Start()
- {
- Physics.IgnoreCollision(characterCollider, characterCollisionBlocker, true);
- rigidbody = GetComponent<Rigidbody>();
- inputHandler = GetComponent<InputHandler>();
- cameraObject = Camera.main.transform;
- myTransform = transform;
- animatorHandler = GetComponentInChildren<PlayerAnimatorManager>();
- animatorHandler.Initialize();
- playerManager = GetComponent<PlayerManager>();
- playerManager.isGrounded = true;
- ignoreForGroundCheck = ~(1 << 8 | 1 << 11);
- cameraHandler = FindObjectOfType<CameraHandler>();
- playerStats = GetComponent<PlayerStats>();
- }
- private void Update()
- {
- Debug.Log("Light attack is "+AttackingForceForward);
- if (isDodgingForward)
- {
- float speed = 10;
- /*rigidbody.MovePosition(Vector3.SmoothDamp(rigidbody.position, targetDirection * 4 * 2 * Time.deltaTime, ref targetDirection, 10));*/
- Vector3 vel = rigidbody.velocity;
- float drag = -1f;
- rigidbody.AddForce(this.transform.forward * 700f * Time.deltaTime, ForceMode.VelocityChange);
- Vector3 v = -rigidbody.velocity;
- v.y = 0;
- Vector3 veloc = v * drag * Time.deltaTime;
- Vector3 force = veloc * Time.deltaTime;
- float coeff = (1 - Time.deltaTime * drag);
- rigidbody.AddForce((veloc + force) / coeff, ForceMode.VelocityChange);
- playerManager.isInteracting = true;
- animatorHandler.canRotate = false;
- }
- if (isDodgingBack)
- {
- float speed = 10;
- /*rigidbody.MovePosition(Vector3.SmoothDamp(rigidbody.position, targetDirection * 4 * 2 * Time.deltaTime, ref targetDirection, 10));*/
- Vector3 vel = rigidbody.velocity;
- float drag = -1f;
- rigidbody.AddForce(this.transform.forward * -300f * Time.deltaTime, ForceMode.VelocityChange);
- Vector3 v = -rigidbody.velocity;
- v.y = 0;
- Vector3 veloc = v * drag * Time.deltaTime;
- Vector3 force = veloc * Time.deltaTime;
- float coeff = (1 - Time.deltaTime * drag);
- rigidbody.AddForce((veloc + force) / coeff, ForceMode.VelocityChange);
- playerManager.isInteracting = true;
- animatorHandler.canRotate = false;
- }
- if(AttackingForceForward)
- {
- AttackForceForward();
- }
- }
- public void AttackForceForward()
- {
- if(AttackingForceForward)
- {
- float speed = animatorHandler.playerAttackForce;
- /*rigidbody.MovePosition(Vector3.SmoothDamp(rigidbody.position, targetDirection * 4 * 2 * Time.deltaTime, ref targetDirection, 10));*/
- Vector3 vel = rigidbody.velocity;
- float drag = -1f;
- rigidbody.AddForce(this.transform.forward * 30f * Time.deltaTime, ForceMode.VelocityChange);
- Vector3 v = -rigidbody.velocity;
- v.y = 0;
- Vector3 veloc = v * drag * Time.deltaTime;
- Vector3 force = veloc * Time.deltaTime;
- float coeff = (1 - Time.deltaTime * drag);
- rigidbody.AddForce((veloc + force) / coeff, ForceMode.VelocityChange);
- playerManager.isInteracting = true;
- }
- }
- #region Movement
- Vector3 normalVector;
- Vector3 targetPosition;
- public void HandleRotation(float delta)
- {
- if (playerManager.isInteracting == false)
- {
- if (animatorHandler.canRotate)
- {
- if (inputHandler.lockOnFlag)
- {
- if (inputHandler.sprintFlag == true || inputHandler.rollFlag == true)
- {
- Vector3 targetDirection = Vector3.zero;
- targetDirection = cameraHandler.cameraTransform.forward * inputHandler.vertical;
- targetDirection += cameraHandler.cameraTransform.right * inputHandler.horizontal;
- targetDirection.Normalize();
- targetDirection.y = 0;
- if (targetDirection == Vector3.zero)
- {
- targetDirection = transform.forward;
- }
- Quaternion tr = Quaternion.LookRotation(targetDirection);
- Quaternion targetRotation = Quaternion.Slerp(transform.rotation, tr, rotationSpeed * Time.deltaTime);
- transform.rotation = targetRotation;
- }
- else
- {
- Vector3 rotationDirection = moveDirection;
- rotationDirection = cameraHandler.currentLockOnTarget.transform.position - transform.position;
- rotationDirection.y = 0;
- rotationDirection.Normalize();
- Quaternion tr = Quaternion.LookRotation(rotationDirection);
- Quaternion targetRotation = Quaternion.Slerp(transform.rotation, tr, rotationSpeed * Time.deltaTime);
- transform.rotation = targetRotation;
- }
- }
- else
- {
- Vector3 targetDir = Vector3.zero;
- float moveOverride = inputHandler.moveAmount;
- targetDir = cameraObject.forward * inputHandler.vertical;
- targetDir += cameraObject.right * inputHandler.horizontal;
- targetDir.Normalize();
- targetDir.y = 0;
- if (targetDir == Vector3.zero)
- {
- targetDir = myTransform.forward;
- }
- float rs = rotationSpeed;
- Quaternion tr = Quaternion.LookRotation(targetDir);
- Quaternion targetRotation = Quaternion.Slerp(myTransform.rotation, tr, rs * delta);
- myTransform.rotation = targetRotation;
- }
- }
- }
- }
- public void HandleMovement(float delta)
- {
- if(inputHandler.rollFlag)
- {
- return;
- }
- if(playerManager.isInteracting)
- {
- return;
- }
- if (playerManager.isInteracting == false)
- {
- moveDirection = cameraObject.forward * inputHandler.vertical;
- moveDirection += cameraObject.right * inputHandler.horizontal;
- moveDirection.Normalize();
- moveDirection.y = 0;
- float speed = movementSpeed;
- Debug.Log(speed);
- if (inputHandler.sprintFlag && inputHandler.moveAmount > 0.5)
- {
- speed = sprintSpeed;
- playerManager.isSprinting = true;
- moveDirection *= speed;
- playerStats.TakeStaminaDamage(sprintStaminaCost);
- }
- else if (inputHandler.moveAmount < 0.5)
- {
- speed = movementSpeed * inputHandler.moveAmount;
- moveDirection *= speed;
- playerManager.isSprinting = false;
- }
- else if (inputHandler.moveAmount > 0.5)
- {
- speed = movementSpeed * inputHandler.moveAmount;
- moveDirection *= speed;
- playerManager.isSprinting = false;
- }
- Vector3 projectedVelocity = Vector3.ProjectOnPlane(moveDirection, normalVector);
- rigidbody.velocity = projectedVelocity;
- if (inputHandler.lockOnFlag && inputHandler.sprintFlag == false)
- {
- animatorHandler.UpdateAnimatorValues(inputHandler.vertical, inputHandler.horizontal, playerManager.isSprinting);
- }
- else
- {
- animatorHandler.UpdateAnimatorValues(inputHandler.moveAmount, 0, playerManager.isSprinting);
- }
- }
- else
- {
- movementSpeed = 0;
- }
- }
- public void HandleRollingAndSprinting(float delta)
- {
- if (animatorHandler.anim.GetBool("isInteracting"))
- {
- return;
- }
- if(playerStats.currentStamina <= 0)
- {
- return;
- }
- if (inputHandler.rollFlag)
- {
- moveDirection = cameraObject.forward * inputHandler.vertical;
- moveDirection += cameraObject.right * inputHandler.horizontal;
- if (inputHandler.moveAmount > 0)
- {
- moveDirection = cameraObject.forward * inputHandler.vertical;
- moveDirection += cameraObject.right * inputHandler.horizontal;
- moveDirection.Normalize();
- moveDirection.y = 0;
- animatorHandler.PlayTargetAnimation("Rolling", true);
- Quaternion rollRotation = Quaternion.LookRotation(moveDirection);
- myTransform.rotation = rollRotation;
- playerStats.TakeStaminaDamage(rollStaminaCost);
- }
- else
- {
- animatorHandler.PlayTargetAnimation("Backstep", true);
- playerStats.TakeStaminaDamage(backstepStaminaCost);
- }
- }
- }
- public void HandleFalling(float delta, Vector3 moveDirection)
- {
- playerManager.isGrounded = false;
- RaycastHit hit;
- Vector3 orgin = myTransform.position;
- orgin.y += groundDetectionRayStartPoint;
- if(Physics.Raycast(orgin, myTransform.forward, out hit, 0.4f))
- {
- moveDirection = Vector3.zero;
- }
- if(playerManager.isInAir)
- {
- rigidbody.AddForce(-Vector3.up * fallingSpeed);
- rigidbody.AddForce(moveDirection * fallingSpeed/3);
- }
- Vector3 dir = moveDirection;
- dir.Normalize();
- orgin = orgin + dir * groundDetectionRayDistance;
- targetPosition = myTransform.position;
- Debug.DrawRay(orgin, -Vector3.up * minimumDistanceNeededToBeginFall, Color.red, 0.1f, false);
- if(Physics.Raycast(orgin, -Vector3.up, out hit, minimumDistanceNeededToBeginFall, ignoreForGroundCheck))
- {
- normalVector = hit.normal;
- Vector3 tp = hit.point;
- playerManager.isGrounded = true;
- targetPosition.y = tp.y;
- if(playerManager.isInAir)
- {
- if(inAirTimer > 0.5f)
- {
- Debug.Log("You were in the air for over 0.5f" + inAirTimer);
- animatorHandler.PlayTargetAnimation("Land", true);
- inAirTimer = 0;
- }
- else
- {
- animatorHandler.PlayTargetAnimation("Empty", false);
- inAirTimer = 0;
- }
- playerManager.isInAir = false;
- }
- }
- else
- {
- if(playerManager.isGrounded)
- {
- playerManager.isGrounded = false;
- }
- if(playerManager.isInAir == false)
- {
- if(playerManager.isInteracting == false)
- {
- animatorHandler.PlayTargetAnimation("Falling", true);
- }
- Vector3 vel = rigidbody.velocity;
- vel.Normalize();
- rigidbody.velocity = vel * (movementSpeed / 2);
- playerManager.isInAir = true;
- }
- }
- if(playerManager.isInteracting || inputHandler.moveAmount > 0)
- {
- myTransform.position = Vector3.Lerp(myTransform.position, targetPosition, Time.deltaTime / 0.1f);
- }
- else
- {
- myTransform.position = targetPosition;
- }
- if(playerManager.isGrounded)
- {
- if(playerManager.isInteracting || inputHandler.moveAmount>0)
- {
- myTransform.position = Vector3.Lerp(myTransform.position, targetPosition, Time.deltaTime);
- }
- else
- {
- myTransform.position = targetPosition;
- }
- }
- }
- public void HandleJumping()
- {
- if (playerStats.currentStamina <= 0)
- {
- return;
- }
- if (playerManager.isInteracting)
- {
- return;
- }
- if(inputHandler.jump_Input)
- {
- if(inputHandler.moveAmount > 0)
- {
- moveDirection = cameraObject.forward * inputHandler.vertical;
- moveDirection += cameraObject.right * inputHandler.horizontal;
- animatorHandler.PlayTargetAnimation("JumpMove", true);
- moveDirection.y = 0;
- Quaternion jumpRotation = Quaternion.LookRotation(moveDirection);
- myTransform.rotation = jumpRotation;
- }
- else
- {
- animatorHandler.PlayTargetAnimation("JumpIdle", true);
- moveDirection.y = 0;
- }
- }
- }
- #endregion
- public void DodgeForward()
- {
- moveDirection = cameraObject.forward * inputHandler.vertical;
- moveDirection += cameraObject.right * inputHandler.horizontal;
- moveDirection.Normalize();
- moveDirection.y = 0;
- rigidbody.AddForce(moveDirection * 10000 * Time.deltaTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement