Player Input
using UnityEngine;
public class PlayerInput : MonoBehaviour
{
public float move_x { get; private set; }
public float move_z { get; private set; }
public bool jump { get; private set; }
private void Awake()
{
InitFields();
}
private void InitFields()
{
move_x = 0;
move_z = 0;
jump = false;
}
private void Update()
{
// if (GameManager.game_over) { InitFields(); return; }
move_x = Input.GetAxisRaw("Horizontal");
move_z = Input.GetAxisRaw("Vertical");
jump = Input.GetKeyDown(KeyCode.Space);
}
}
Player Action
using UnityEngine;
public class PlayerAction : MonoBehaviour
{
private PlayerInput pi;
private Rigidbody rb;
private float move_speed;
private float jump_force;
private float vel_damp;
[SerializeField] private Transform cam_transform;
private void Awake()
{
GetReferences();
InitFields();
}
private void GetReferences()
{
pi = GetComponent<PlayerInput>();
rb = GetComponent<Rigidbody>();
}
private void InitFields()
{
move_speed = 100f;
jump_force = 500f;
vel_damp = 0.75f;
}
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
private void FixedUpdate()
{
Move();
// if (pi.jump) Jump();
}
private void Update()
{
if (pi.jump && IsGrounded()) Jump();
transform.rotation = Quaternion.Euler(transform.eulerAngles.x, cam_transform.eulerAngles.y, transform.eulerAngles.x);
}
private bool IsGrounded()
{
Collider[] hits = Physics.OverlapBox(new Vector3(transform.position.x, transform.position.y - 1f, transform.position.z), new Vector3(0.5f, 0.25f, 0.5f), Quaternion.identity, LayerMask.GetMask("Ground"));
if (hits.Length > 0) return true;
return false;
}
private void Move()
{
Vector3 move_dir = (transform.right * pi.move_x + transform.forward * pi.move_z).normalized;
rb.linearVelocity += move_dir * move_speed * Time.fixedDeltaTime;
rb.linearVelocity = new Vector3(rb.linearVelocity.x * vel_damp, rb.linearVelocity.y, rb.linearVelocity.z * vel_damp);
}
private void Jump()
{
rb.linearVelocity = new Vector3(rb.linearVelocity.x, 0, rb.linearVelocity.z);
rb.AddForce(Vector3.up * jump_force);
}
}
Main Camera
Virtual Camera
- Cinemachine Camera
Tracking Target : Player
Position Control : Orbital Follow
Rotation Control : Rotation Composer
- Cinemachine Orbital Follow
Position Damping : X, Y, Z 전부 0 (안 그러면 카메라가 플레이어를 제대로 따라가지 못하고, 중간에 뚝뚝 끊김!)
Orbit Style : Sphere
Radius : 10
- Cinemachine Rotation Composer
- Cinemachine FreeLook Modifier
- Cinemachine Input Axis Controller
읽어주셔서 감사합니다!