728x90
CharacterController - 물리엔진사용x 게임엔진의 움직임을 로직으로 쉽게 처리
- 자체적으로 캡슐 collider 소지 (충돌처리)
- Slope Limit: 올라갈수 있는 경사도
- Step Offset: 올라갈수 있는 계단 높이
- Skin Width: 다른 collider랑 부딪혔을때 겹칠 수 있는 범위
- min move distance: 최소 이동값
RigidBody와 다르게. 중력, 공기저항도 설정을 해줘야한다.
하지만 설정만 해주면, 기본 함수들을 가져와서 쓸 수 있는 편함이 있다. Move, Isground 등
public class ControllerScript : MonoBehaviour
{
#region Variables
//basic variables about the movements
[SerializeField]
float speed = 5f;
[SerializeField]
float jumpHeight = 2.0f;
[SerializeField]
float dashDistance = 5.0f;
//gravity and drag
[SerializeField]
float gravity = -29.81f;
[SerializeField]
private Vector3 drags;
//to get CharacterController from the unity
private CharacterController characterController;
//to calculate
private Vector3 calcVelocity = Vector3.zero;
//movement(vector3)
private Vector3 inputDirection = Vector3.zero;
//prevent double jump
private bool isGround = false;
[SerializeField]
LayerMask groundLayerMask; // in the ground layer, player can jump only
[SerializeField]
float groundCheckDistance = 0.3f;
#endregion
// Start is called before the first frame update
void Start()
{
//to get CharacterController from the unity
characterController = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
// Check grounded
bool isGrounded = characterController.isGrounded;
//if player is on the ground, gravity = 0
if (isGrounded && calcVelocity.y < 0)
{
calcVelocity.y = 0f;
}
//user input - basic movments
Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
characterController.Move(move * Time.deltaTime * speed);
//actual code about user movements
if (move != Vector3.zero)
{
transform.forward = move;
}
//user input - jump
if (Input.GetButtonDown("Jump") && isGrounded)
{
calcVelocity.y += Mathf.Sqrt(jumpHeight * -2f * gravity);
}
//user input - dash
if (Input.GetButtonDown("Dash"))
{
Debug.Log("Dash");
calcVelocity += 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))
);
}
//gravity
calcVelocity.y += gravity * Time.deltaTime;
//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);
}
}
** 고생했던거 **
CharacterController에서 Move 등등의 함수를 가져와 쓰고싶은데, 자꾸
error CS1061: 'CharacterController' does not contain a definition for 'Move' and no accessible exten
이런 에러가 뜨면서 안됬었다.
이유를 알고보니까 내가 script을 만들떄 CharacterController라는 이름으로 만들어서 그런것 이었다.
고치는법: 단순히 script 이름을 바꿔 주면 된다.
***************
728x90
'Web' 카테고리의 다른 글
종합반 플러스 - 2주차 - 나만의사전 (0) | 2022.02.27 |
---|---|
스터디노트 04 - code: x (0) | 2020.09.28 |
스터디노트 03 - code: NavMesh (0) | 2020.09.28 |
스터디노트 01 - code: 이동,점프,대수 (0) | 2020.09.28 |
[C++/ SDL2.0 - 똥피하기만들기] - 03. main 정리 (0) | 2020.03.10 |