본문 바로가기

Project/TheMoment

1. 1인칭 시점 플레이어 구현




1. 유니티에 빈 오브젝트를 생성하여 이름을 Player라고 지었다.  Player 오브젝트에 움직이는 스크립트를 넣은 후 이동하면 Main Camera가 플레이어를 쫓아오게끔 만들어 주기 위해 Player 오브젝트 안에 Main Camera를 넣어주었다.










2. The Moment 프로젝트는 물리력을 사용하지 않는 환경의 게임이다. 따라서 Rigidbody 물리적 특성을 사용하지 않는 3인칭 또는 1인칭 플레이어 제어에 주로 사용되는 Character Controller 컴포넌트를 사용할 것이다. Player에 Character Controller 컴포넌트를 붙여 주었다.







프로퍼티:기능:
Slope Limit콜라이더가 표시된 값 이하의 기울기만을 오르도록 제한합니다. (degree)
Step Offset표시된 값보다 지면에 가까운 경우에만 캐릭터가 계단을 오릅니다. This should not be greater than the Character Controller’s height or it will generate an error.
Skin width두 콜라이더가 Skin Width와 동일한 깊이에서 서로 관통합니다. Skin Wdith가 커지면 지터가 줄어듭니다. Skin Width가 낮으면 캐릭터가 움직일 수 없게 되는 경우가 있습니다. Radius가 10%일 때 이 값을 설정하는 것이 좋습니다.
Min Move Distance캐릭터가 표시된 값 미만으로 움직이려 해도 움직이지 않습니다. 지터(Jitter)를 줄이는 데 사용할 수 있습니다. 대부분의 경우 이 값은 0의 상태로 두십시오.
Center월드 공간에서 캡슐 콜라이더가 상쇄되지만, 캐릭터 피벗이 어떻게 회전하는지에는 영향을 주지 않습니다.
Radius캡슐 콜라이더 로컬 반경. 이것은 기본적으로 콜라이더의 폭입니다.
Height캐릭터의 Capsule Collider 높이. 이 값을 변경하면 양수와 음수의 방향으로 Y축을 따라 콜라이더가 확대 축소됩니다.
3. Character Controller 컴포넌트 Inspector 창 [유니티 매뉴얼]







     


4. Main Camera의 위치를 플레이어 얼굴 부분에 위치할 수 있도록 해주었다.






using UnityEngine;
using System.Collections;

[RequireComponent (typeof(CharacterController))]

public class FirstPersonControl : MonoBehaviour {

	public float movementSpeed = 5f;
	public float mouseSensitivity = 2f;
	public float upDownRange= 90;
	public float jumpSpeed = 5;
	public float downSpeed = 5;

	private Vector3 speed;
	private float forwardSpeed;
	private float sideSpeed;

	private float rotLeftRight;
	private float rotUpDown;
	private float verticalRotation = 0f;

	private float verticalVelocity = 0f;


	private CharacterController cc;

	// Use this for initialization
	void Start () {
		cc = GetComponent ();
		Cursor.lockState = CursorLockMode.Locked;
	
	}
	
	// Update is called once per frame
	void Update () {

		FPMove ();
		FPRotate ();
	}


	//Player의 x축, z축 움직임을 담당 
	void FPMove()
	{

		forwardSpeed = Input.GetAxis ("Vertical") * movementSpeed;
		sideSpeed = Input.GetAxis ("Horizontal") * movementSpeed;

		//막아 놓은 점프 기능
		//if (cc.isGrounded && Input.GetButtonDown ("Jump"))
			//verticalVelocity = jumpSpeed;
		//if (!cc.isGrounded)
			//verticalVelocity = downSpeed;
		

		verticalVelocity += Physics.gravity.y * Time.deltaTime;

		speed = new Vector3 (sideSpeed, verticalVelocity, forwardSpeed);
		speed = transform.rotation * speed;

		cc.Move (speed * Time.deltaTime);
	}

	//Player의 회전을 담당
	void FPRotate()
	{
		//좌우 회전
		rotLeftRight = Input.GetAxis ("Mouse X") * mouseSensitivity;
		transform.Rotate (0f, rotLeftRight, 0f);

		//상하 회전
		verticalRotation -= Input.GetAxis ("Mouse Y") * mouseSensitivity;
		verticalRotation = Mathf.Clamp (verticalRotation, -upDownRange, upDownRange);
		Camera.main.transform.localRotation = Quaternion.Euler (verticalRotation, 0f, 0f);
	}
}

5. First Person Controller 스크립트 (Player의 이동과 회전을 담당하는 스크립트)