캐릭터 이동 및 점프 구현 수정

- Rigidbody -> CharacterController 사용으로 변경
This commit is contained in:
villaingames 2023-10-07 18:08:40 +09:00
parent d3996efede
commit c8d193ab47
3 changed files with 64 additions and 41 deletions

View File

@ -5,25 +5,26 @@ using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField] private GameObject _playerObj;
[SerializeField] private MinigameController _minigameController;
private float _turnSpeed = 40;
private float _moveSpeed = 5;
private bool _isPushed = false;
//Move values
private float _camRotate = 0;
private float _moveHorizontal = 0;
private float _moveVertical = 0;
private Vector3 _moveDir;
private bool _isJumping = false;
//private Rigidbody _rigidBody;
private PlayerMove _playerMove;
public bool IsBlock { get; private set; }
void Start()
{
_playerMove = GetComponent<PlayerMove>();
EventBus.Instance.Subscribe<EventSendMinigamePoint>(OnGetMinigamePoint);
}
@ -36,21 +37,17 @@ public class PlayerController : MonoBehaviour
return;
}
_camRotate = Input.GetAxis("Mouse X");
_playerMove.Rotate(Input.GetAxis("Mouse X"));
var _moveHorizontal = Input.GetAxis("Horizontal");
var _moveVertical = Input.GetAxis("Vertical");
CamRotate();
_moveHorizontal = Input.GetAxis("Horizontal");
_moveVertical = Input.GetAxis("Vertical");
Move();
if(!_isJumping && Input.GetKeyDown(KeyCode.Space))
_playerMove.Move(_moveHorizontal, _moveVertical);
if(Input.GetKeyDown(KeyCode.Space))
{
_isJumping = true;
GetComponent<Rigidbody>().AddForce(Vector3.up * 300, ForceMode.Acceleration);
_playerMove.Jump();
}
if (!_isPushed && Input.GetKeyDown(KeyCode.Q))
{
@ -65,19 +62,6 @@ public class PlayerController : MonoBehaviour
}
}
private void CamRotate()
{
_playerObj.transform.Rotate(Vector3.up * _turnSpeed * Time.deltaTime * _camRotate);
}
private void Move()
{
_moveDir = (_playerObj.transform.forward * _moveVertical) + (_playerObj.transform.right * _moveHorizontal);
transform.Translate(_moveDir.normalized * Time.deltaTime * _moveSpeed);
}
private void PushStart()
{
_isPushed = true;
@ -110,14 +94,6 @@ public class PlayerController : MonoBehaviour
}
#region Collision
private void OnCollisionEnter(Collision collision)
{
//벽이 아닌 물체(오브젝트, 바닥)
if (collision.gameObject.layer == LayerMask.NameToLayer("Floor"))
{
_isJumping = false;
}
}
private void OnCollisionExit(Collision collision)
{

View File

@ -0,0 +1,36 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
[SerializeField] private Transform _playerObj;
[SerializeField] private CharacterController _characterController;
private Vector3 _moveDir;
public void Jump()
{
if (_characterController.isGrounded == true)
{
_moveDir.y = Constants.JUMP_FORCE;
}
}
public void Move(float horizontal, float vertical)
{
var dir = (_playerObj.transform.forward * vertical) + (_playerObj.transform.right * horizontal);
_moveDir.x = dir.x;
_moveDir.z = dir.z;
if (_characterController.isGrounded == false)
{
_moveDir.y += Physics.gravity.y * Time.deltaTime;
}
_characterController.Move(_moveDir * Constants.MOVE_SPEED * Time.deltaTime);
}
public void Rotate(float rotate)
{
_playerObj.transform.Rotate(Vector3.up * Constants.CAM_TURN_SPEED * Time.deltaTime * rotate);
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 957ce77a8667f9045a2fc0c3416b0c91
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: