角色控制
This commit is contained in:
@@ -4,7 +4,7 @@ namespace EasyPeasyFirstPersonController
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
public partial class FirstPersonController : MonoBehaviour
|
||||
public class FirstPersonController : MonoBehaviour
|
||||
{
|
||||
[Range(0, 100)] public float mouseSensitivity = 25f;
|
||||
[Range(0f, 200f)] private float snappiness = 100f;
|
||||
@@ -55,7 +55,6 @@ namespace EasyPeasyFirstPersonController
|
||||
private Camera cam;
|
||||
private AudioSource slideAudioSource;
|
||||
private float bobTimer;
|
||||
private float defaultPosY;
|
||||
private Vector3 recoil = Vector3.zero;
|
||||
private bool isLook = true, isMove = true;
|
||||
private float currentCameraHeight;
|
||||
@@ -67,15 +66,13 @@ namespace EasyPeasyFirstPersonController
|
||||
private float currentTiltAngle;
|
||||
private float tiltVelocity;
|
||||
|
||||
public float CurrentCameraHeight => isCrouching || isSliding ? crouchCameraHeight : originalCameraParentHeight;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
characterController = GetComponent<CharacterController>();
|
||||
cam = playerCamera.GetComponent<Camera>();
|
||||
originalHeight = characterController.height;
|
||||
originalCameraParentHeight = cameraParent.localPosition.y;
|
||||
defaultPosY = cameraParent.localPosition.y;
|
||||
// defaultPosY = cameraParent.localPosition.y;
|
||||
slideAudioSource = gameObject.AddComponent<AudioSource>();
|
||||
slideAudioSource.playOnAwake = false;
|
||||
slideAudioSource.loop = false;
|
||||
@@ -118,10 +115,11 @@ namespace EasyPeasyFirstPersonController
|
||||
transform.rotation = Quaternion.Euler(0f, xVelocity, 0f);
|
||||
}
|
||||
|
||||
HandleHeadBob();
|
||||
// HandleHeadBob();
|
||||
|
||||
bool wantsToCrouch = canCrouch && Input.GetKey(KeyCode.LeftControl) && !isSliding;
|
||||
Vector3 point1 = transform.position + characterController.center - Vector3.up * (characterController.height * 0.5f);
|
||||
Vector3 point1 = transform.position + characterController.center -
|
||||
Vector3.up * (characterController.height * 0.5f);
|
||||
Vector3 point2 = point1 + Vector3.up * characterController.height * 0.6f;
|
||||
float capsuleRadius = characterController.radius * 0.95f;
|
||||
float castDistance = isSliding ? originalHeight + 0.2f : originalHeight - crouchHeight + 0.2f;
|
||||
@@ -130,6 +128,7 @@ namespace EasyPeasyFirstPersonController
|
||||
{
|
||||
postSlideCrouchTimer = 0.3f;
|
||||
}
|
||||
|
||||
if (postSlideCrouchTimer > 0)
|
||||
{
|
||||
postSlideCrouchTimer -= Time.deltaTime;
|
||||
@@ -144,7 +143,9 @@ namespace EasyPeasyFirstPersonController
|
||||
{
|
||||
isSliding = true;
|
||||
slideTimer = slideDuration;
|
||||
slideDirection = moveInput.magnitude > 0.1f ? (transform.right * moveInput.x + transform.forward * moveInput.y).normalized : transform.forward;
|
||||
slideDirection = moveInput.magnitude > 0.1f
|
||||
? (transform.right * moveInput.x + transform.forward * moveInput.y).normalized
|
||||
: transform.forward;
|
||||
currentSlideSpeed = sprintSpeed;
|
||||
}
|
||||
|
||||
@@ -156,6 +157,7 @@ namespace EasyPeasyFirstPersonController
|
||||
{
|
||||
isSliding = false;
|
||||
}
|
||||
|
||||
float targetSlideSpeed = slideSpeed * Mathf.Lerp(0.7f, 1f, slideProgress);
|
||||
currentSlideSpeed = Mathf.SmoothDamp(currentSlideSpeed, targetSlideSpeed, ref slideSpeedVelocity, 0.2f);
|
||||
characterController.Move(slideDirection * currentSlideSpeed * Time.deltaTime);
|
||||
@@ -165,7 +167,9 @@ namespace EasyPeasyFirstPersonController
|
||||
characterController.height = Mathf.Lerp(characterController.height, targetHeight, Time.deltaTime * 10f);
|
||||
characterController.center = new Vector3(0f, characterController.height * 0.5f, 0f);
|
||||
|
||||
float targetFov = isSprinting ? sprintFov : (isSliding ? sprintFov + (slideFovBoost * Mathf.Lerp(0f, 1f, 1f - slideProgress)) : normalFov);
|
||||
float targetFov = isSprinting
|
||||
? sprintFov
|
||||
: (isSliding ? sprintFov + (slideFovBoost * Mathf.Lerp(0f, 1f, 1f - slideProgress)) : normalFov);
|
||||
currentFov = Mathf.SmoothDamp(currentFov, targetFov, ref fovVelocity, 1f / fovChangeSpeed);
|
||||
cam.fieldOfView = currentFov;
|
||||
|
||||
@@ -174,7 +178,8 @@ namespace EasyPeasyFirstPersonController
|
||||
|
||||
private void HandleHeadBob()
|
||||
{
|
||||
Vector3 horizontalVelocity = new Vector3(characterController.velocity.x, 0f, characterController.velocity.z);
|
||||
Vector3 horizontalVelocity =
|
||||
new Vector3(characterController.velocity.x, 0f, characterController.velocity.z);
|
||||
bool isMovingEnough = horizontalVelocity.magnitude > 0.1f;
|
||||
|
||||
float targetBobOffset = isMovingEnough ? Mathf.Sin(bobTimer) * bobbingAmount : 0f;
|
||||
@@ -190,7 +195,8 @@ namespace EasyPeasyFirstPersonController
|
||||
currentCameraHeight + currentBobOffset,
|
||||
cameraParent.localPosition.z);
|
||||
recoil = Vector3.zero;
|
||||
cameraParent.localRotation = Quaternion.RotateTowards(cameraParent.localRotation, Quaternion.Euler(recoil), recoilReturnSpeed * Time.deltaTime);
|
||||
cameraParent.localRotation = Quaternion.RotateTowards(cameraParent.localRotation,
|
||||
Quaternion.Euler(recoil), recoilReturnSpeed * Time.deltaTime);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -218,14 +224,16 @@ namespace EasyPeasyFirstPersonController
|
||||
recoil = Vector3.zero;
|
||||
}
|
||||
|
||||
cameraParent.localRotation = Quaternion.RotateTowards(cameraParent.localRotation, Quaternion.Euler(recoil), recoilReturnSpeed * Time.deltaTime);
|
||||
cameraParent.localRotation = Quaternion.RotateTowards(cameraParent.localRotation, Quaternion.Euler(recoil),
|
||||
recoilReturnSpeed * Time.deltaTime);
|
||||
}
|
||||
|
||||
private void HandleMovement()
|
||||
{
|
||||
moveInput.x = Input.GetAxis("Horizontal");
|
||||
moveInput.y = Input.GetAxis("Vertical");
|
||||
isSprinting = canSprint && Input.GetKey(KeyCode.LeftShift) && moveInput.y > 0.1f && isGrounded && !isCrouching && !isSliding;
|
||||
isSprinting = canSprint && Input.GetKey(KeyCode.LeftShift) && moveInput.y > 0.1f && isGrounded &&
|
||||
!isCrouching && !isSliding;
|
||||
|
||||
float currentSpeed = isCrouching ? crouchSpeed : (isSprinting ? sprintSpeed : walkSpeed);
|
||||
if (!isMove) currentSpeed = 0f;
|
||||
@@ -256,27 +264,5 @@ namespace EasyPeasyFirstPersonController
|
||||
characterController.Move(moveDirection * Time.deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetControl(bool newState)
|
||||
{
|
||||
SetLookControl(newState);
|
||||
SetMoveControl(newState);
|
||||
}
|
||||
|
||||
public void SetLookControl(bool newState)
|
||||
{
|
||||
isLook = newState;
|
||||
}
|
||||
|
||||
public void SetMoveControl(bool newState)
|
||||
{
|
||||
isMove = newState;
|
||||
}
|
||||
|
||||
public void SetCursorVisibility(bool newVisibility)
|
||||
{
|
||||
Cursor.lockState = newVisibility ? CursorLockMode.None : CursorLockMode.Locked;
|
||||
Cursor.visible = newVisibility;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user