있습니다. 걷고 있으면 애니메이터에서 crosshair_idle 이랑
crosshair_walk를 계속해서 왔다갔다해요.
요.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerController : MonoBehaviour
{
//스피드 조정 변수
[SerializeField]
private float walkSpeed;
[SerializeField]
private float runSpeed;
[SerializeField]
private float crouchSpeed;
private float applySpeed;
[SerializeField]
private float jumpForce;
//상태 변수
private bool isWalk = false;
private bool isRun = false;
private bool isCrouch = false;
private bool isGround = true;
//움직임 체크 변수
private Vector3 lastPos;
//앉았을 때 얼마나 앉을지 결정하는 변수
[SerializeField]
private float crouchPosY;
private float originPosY;
private float applyCrouchPosY;
//땅 착지 여부
private CapsuleCollider capsuleCollider;
// 민감도
[SerializeField]
private float lookSensitivity;
// 카메라 한계
[SerializeField]
private float cameraRotationLimit;
private float crrentCameraRotationX = 0;
//필요한 컴포넌트
[SerializeField]
private Camera theCamera;
private Rigidbody myRigid;
private GunController theGunController;
private crosshair thecrosshair;
// Start is called before the first frame update
void Start()
{
capsuleCollider = GetComponent<CapsuleCollider>();
myRigid = GetComponent<Rigidbody>();
theGunController = FindObjectOfType<GunController>();
thecrosshair = FindObjectOfType<crosshair>();
applySpeed = walkSpeed;
originPosY = theCamera.transform.localPosition.y;
applyCrouchPosY = originPosY;
}
// Update is called once per frame
void Update()
{
IsGround();
TryJump();
TryRun();
TryCrouch();
Move();
Movecheck();
cameraRotation();
CharacterRotation();
}
private void TryCrouch()
{
if(Input.GetKeyDown(KeyCode.LeftControl))
{
Crouch();
}
}
private void Crouch()
{
isCrouch = !isCrouch;
thecrosshair.crouchingAnimation(isCrouch);
if(isCrouch)
{
applySpeed = crouchSpeed;
applyCrouchPosY = crouchPosY;
}
else
{
applySpeed = walkSpeed;
applyCrouchPosY = originPosY;
}
StartCoroutine(CrouchCoroutine());
}
IEnumerator CrouchCoroutine()
{
float _posY = theCamera.transform.localPosition.y;
int count = 0;
while(_posY != applyCrouchPosY)
{
count++;
_posY = Mathf.Lerp(_posY, applyCrouchPosY, 0.3f);
theCamera.transform.localPosition = new Vector3(0, _posY, 0);
if(count > 15)
break;
yield return null;
}
theCamera.transform.localPosition = new Vector3(0, applyCrouchPosY, 0f);
}
private void IsGround()
{
isGround = Physics.Raycast(transform.position, Vector3.down, capsuleCollider.bounds.extents.y + 0.1f);
}
private void TryJump()
{
if(Input.GetKeyDown(KeyCode.Space) && isGround)
{
jump();
}
}
private void jump()
{
if(isCrouch)
Crouch();
myRigid.velocity = transform.up * jumpForce;
}
private void TryRun()
{
if(Input.GetKey(KeyCode.LeftShift))
{
Running();
}
if(Input.GetKeyUp(KeyCode.LeftShift))
{
RunningCancel();
}
}
private void Running()
{
if(isCrouch)
Crouch();
theGunController.CancelFineSight();
isRun = true;
thecrosshair.runningAnimation(isRun);
applySpeed = runSpeed;
}
private void RunningCancel()
{
isRun = false;
thecrosshair.runningAnimation(isRun);
applySpeed = walkSpeed;
}
private void Move()
{
float _MoveDirX = Input.GetAxisRaw("Horizontal");
float _MoveDirZ = Input.GetAxisRaw("Vertical");
Vector3 _moveHorizontal = transform.right * _MoveDirX;
Vector3 _moveVertical = transform.forward * _MoveDirZ;
Vector3 _velocity = (_moveHorizontal + _moveVertical).normalized * applySpeed;
myRigid.MovePosition(transform.position + _velocity * Time.deltaTime);
}
private void Movecheck()
{
if(!isRun && !isCrouch)
{
if(Vector3.Distance(lastPos, transform.position) >= 0.01f)
isWalk = true;
else
isWalk = false;
thecrosshair.walkingAnimation(isWalk);
Vector3 position = transform.position;
lastPos = position;
}
}
private void CharacterRotation()
{
float _yRotation = Input.GetAxisRaw("Mouse X");
Vector3 _characterRotationY = new Vector3(0f, _yRotation, 0f) * lookSensitivity;
myRigid.MoveRotation(myRigid.rotation * Quaternion.Euler(_characterRotationY));
}
private void cameraRotation()
{
float _xRotation = Input.GetAxisRaw("Mouse Y");
float _cameraRotationX = _xRotation * lookSensitivity;
crrentCameraRotationX -= _cameraRotationX;
crrentCameraRotationX = Mathf.Clamp(crrentCameraRotationX, -cameraRotationLimit, cameraRotationLimit);
theCamera.transform.localEulerAngles = new Vector3(crrentCameraRotationX, 0f, 0f);
}
}
저도 동일한 이슈를 겪고있네요. 원인찾기가 너무 난해해서 일단 뒷전으로 하고 있는중입니다..