본문 바로가기

C

3D 쿼터뷰 액션게임 - 플레이어 이동

 

플레이어의 기본 이동 구현

 

 

플레이어 프리팹을 씬 뷰에 넣고 rigidbody, capsule collider, player.cs 컴포넌트를 Add.

이동 중, 관성에 의한 넘어짐을 없애기 위해 rigidbody의 Freeze rotation X, Z 축을 체크.

 

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
	public float speed;
	float hAxis;
    float vAxis;
    

    
    Vector3 moveVec;
    
    void Start()
    {
    
    }
    
    void Update()
    {
    	hAxis = Input.GetAxisRaw("Horizontal"); //edit-project settings-input manager-Axes 의 Horizontal
        vAxis = Input.GetAxisRaw("Vertical"); //edit-project settings-input manager-Axes 의 Vertical
        
        moveVec = new Vector3(hAxis, 0, vAxis).normalized; //벡터를 노멀라이즈드
        
        transform.position += moveVec * speed * Time.deltaTime; //time.deltaTime 으로 update 프레임에 따라 trasnform의 변화를 주는대신 time의 변화에 따라 transform 변화.
    }

}

 

 

 

플레이어의 transform 이동은 물리 충돌을 무시하는 경우가 발생하므로

플레이어 인스펙터에서 rigidbody 컴포넌트에 collision Detection 옵션을 Continuous 로 변경. 

 

 

 

 

 

 

출처 - https://youtu.be/WkMM7Uu2AoA?list=PLO-mt5Iu5TeYI4dbYwWP8JqZMC9iuUIW2