캐릭터 이동 및 점프 구현 수정
- Rigidbody -> CharacterController 사용으로 변경
This commit is contained in:
parent
d3996efede
commit
c8d193ab47
|
@ -5,25 +5,26 @@ using UnityEngine;
|
||||||
|
|
||||||
public class PlayerController : MonoBehaviour
|
public class PlayerController : MonoBehaviour
|
||||||
{
|
{
|
||||||
[SerializeField] private GameObject _playerObj;
|
|
||||||
[SerializeField] private MinigameController _minigameController;
|
[SerializeField] private MinigameController _minigameController;
|
||||||
private float _turnSpeed = 40;
|
|
||||||
private float _moveSpeed = 5;
|
|
||||||
|
|
||||||
private bool _isPushed = false;
|
private bool _isPushed = false;
|
||||||
|
|
||||||
//Move values
|
//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; }
|
public bool IsBlock { get; private set; }
|
||||||
|
|
||||||
void Start()
|
void Start()
|
||||||
{
|
{
|
||||||
|
_playerMove = GetComponent<PlayerMove>();
|
||||||
EventBus.Instance.Subscribe<EventSendMinigamePoint>(OnGetMinigamePoint);
|
EventBus.Instance.Subscribe<EventSendMinigamePoint>(OnGetMinigamePoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,21 +37,17 @@ public class PlayerController : MonoBehaviour
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_camRotate = Input.GetAxis("Mouse X");
|
_playerMove.Rotate(Input.GetAxis("Mouse X"));
|
||||||
|
|
||||||
|
var _moveHorizontal = Input.GetAxis("Horizontal");
|
||||||
|
var _moveVertical = Input.GetAxis("Vertical");
|
||||||
|
|
||||||
CamRotate();
|
_playerMove.Move(_moveHorizontal, _moveVertical);
|
||||||
|
|
||||||
_moveHorizontal = Input.GetAxis("Horizontal");
|
if(Input.GetKeyDown(KeyCode.Space))
|
||||||
_moveVertical = Input.GetAxis("Vertical");
|
|
||||||
|
|
||||||
Move();
|
|
||||||
|
|
||||||
if(!_isJumping && Input.GetKeyDown(KeyCode.Space))
|
|
||||||
{
|
{
|
||||||
_isJumping = true;
|
_playerMove.Jump();
|
||||||
GetComponent<Rigidbody>().AddForce(Vector3.up * 300, ForceMode.Acceleration);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (!_isPushed && Input.GetKeyDown(KeyCode.Q))
|
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()
|
private void PushStart()
|
||||||
{
|
{
|
||||||
_isPushed = true;
|
_isPushed = true;
|
||||||
|
@ -110,14 +94,6 @@ public class PlayerController : MonoBehaviour
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Collision
|
#region Collision
|
||||||
private void OnCollisionEnter(Collision collision)
|
|
||||||
{
|
|
||||||
//벽이 아닌 물체(오브젝트, 바닥)
|
|
||||||
if (collision.gameObject.layer == LayerMask.NameToLayer("Floor"))
|
|
||||||
{
|
|
||||||
_isJumping = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnCollisionExit(Collision collision)
|
private void OnCollisionExit(Collision collision)
|
||||||
{
|
{
|
||||||
|
|
36
Assets/Scripts/Stage/Controller/PlayerMove.cs
Normal file
36
Assets/Scripts/Stage/Controller/PlayerMove.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
11
Assets/Scripts/Stage/Controller/PlayerMove.cs.meta
Normal file
11
Assets/Scripts/Stage/Controller/PlayerMove.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 957ce77a8667f9045a2fc0c3416b0c91
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
Loading…
Reference in New Issue
Block a user