๐ Rigidbody์์ ์ฐจ์ด์
slope limit : ์บ๋ฆญํฐ๊ฐ ์ฌ๋ผ๊ฐ ์ ์๋ ๊ฒฝ์ฌ๋
step offset : ๊ณ๋จ๊ณผ ๊ฐ์ด ์บ๋ฆญํฐ๊ฐ ์ฌ๋ผ๊ฐ ์ ์๋ ๋์ด
๋ฌผ๋ฆฌ์์ง์ ์ฌ์ฉํ์ง ์๊ธฐ ๋๋ฌธ์ ์ค๋ ฅ๊ณผ ์ด๋์๋ ๊ฐ์ ๊ณ์ฐ๋ฒ์ ์ง์ ์ ์ผ๋ก ๊ตฌํํด์ผ ํ๋ค.
์ค๋ธ์ ํธ ๊ตฌ์ฑ
์ค๋ธ์ ํธ์ Character Controller ์ปดํฌ๋ํธ ์ถ๊ฐ ๋ฐ ์์ฑ ๋ณ๊ฒฝ
โจ Character Controller
- skin width : ์บ๋ฆญํฐ ์ปจํธ๋กค๋ฌ์ ๋ค๋ฅธ ์ฝ๋ผ์ด๋๊ฐ ๋ถ๋ชํ์ ๋ ๊ฒน์น ์ ์๋ ๊ฐ
- min move distance : ์บ๋ฆญํฐ ์ด๋ ์ต์๊ฐ
์๋์ ์๋ capsule y position ๊ฐ๋1๋ก ๋ณ๊ฒฝํด์ค๋ค.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace FastCampus.Characters {
[RequireComponent(typeof(CharacterController))]
public class ControllerCharacter : MonoBehaviour {
#region Variables
[SerializeField] private float speed = 5f;
[SerializeField] private float jumpHeight = 2f;
[SerializeField] private float dashDistance = 5f;
[SerializeField] private float gravity = -9.81f;
[SerializeField] private float groundCheckDistance = 0.2f;
[SerializeField] private Vector3 drags;
private CharacterController characterController;
private Vector3 calcVelocity = Vector3.zero;
#endregion
#region Unity Methods
private void Start() {
characterController = GetComponent<CharacterController>();
}
private void Update() {
// Check grounded
bool isGrounded = characterController.isGrounded;
if (isGrounded && calcVelocity.y < 0) {
calcVelocity.y = 0f;
}
// Process move inputs
Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
characterController.Move(move * Time.deltaTime * speed);
if (move != Vector3.zero) {
transform.forward = move;
}
// Process jump input
if (Input.GetButtonDown("Jump") && isGrounded) {
calcVelocity.y += Mathf.Sqrt(jumpHeight * -2f * gravity);
}
// Process dash input
if (Input.GetButtonDown("Dash")) {
Vector3 dashVelocity = Vector3.Scale(transform.forward, dashDistance * new Vector3((Mathf.Log(1f / (Time.deltaTime * drags.x + 1)) / -Time.deltaTime), 0,
(Mathf.Log(1f / (Time.deltaTime * drags.z + 1)) / -Time.deltaTime)));
calcVelocity += dashVelocity;
}
// Process gravity
calcVelocity.y += gravity * Time.deltaTime;
// Process dash ground drags
calcVelocity.x /= 1 + drags.x * Time.deltaTime;
calcVelocity.y /= 1 + drags.y * Time.deltaTime;
calcVelocity.z /= 1 + drags.z * Time.deltaTime;
characterController.Move(calcVelocity * Time.deltaTime);
}
#endregion Unity Methods
}
}