이번에도 똑같이 일단 Assets에 FollowCamera라는 스크립트 파일을 준비합니다.
그리고는 이렇게 작성을 합시다.
using UnityEngine;
using System.Collections;
public class FollowCamera : MonoBehaviour {
public float distance = 5.0f;
public float horizontalAngle = 0.0f;
public float rotAngle = 180.0f;
public float verticalAngle = 10.0f;
public Transform lookTarget;
public Vector3 offset = Vector3.zero;
InputManager inputManager;
// Use this for initialization
void Start () {
inputManager = FindObjectOfType<InputManager>();
}
// Update is called once per frame
void LateUpdate () {
if (inputManager.Moved())
{
float anglePerPixel = rotAngle / (float)Screen.width;
Vector2 delta = inputManager.GetDeltaPosition();
horizontalAngle += delta.x * anglePerPixel;
horizontalAngle = Mathf.Repeat(horizontalAngle, 360.0f);
verticalAngle -= delta.y * anglePerPixel;
verticalAngle = Mathf.Clamp(verticalAngle, -60.0f, 60.0f);
}
if (lookTarget != null)
{
Vector3 lookPosition = lookTarget.position + offset;
Vector3 relativePos = Quaternion.Euler(verticalAngle, horizontalAngle, 0) * new Vector3(0, 0, -distance);
transform.position = lookPosition + relativePos;
transform.LookAt(lookPosition);
RaycastHit hitInfo;
if (Physics.Linecast(lookPosition, transform.position, out hitInfo, 1 << LayerMask.NameToLayer("Ground")))
transform.position = hitInfo.point;
}
}
}
그리고는 Hierarchy의 Main Camera에 FollowCamera 스크립트를 추가합니다.
---------------------------------------------------------------
그런 다음 Main Camera를 선택하고 Inspector에서 Follow Camera의 Look Target속성에 Player [ 카메라로 따라갈 대상 ]을 설정합니다.
그리고는 Offset에 Y축의 값도 1.5로 변경을 진행해 주세요
'프로그래밍일지 (고등학생 - 인문계) > 유니티 3D' 카테고리의 다른 글
카메라를 조종하자! - 처리순서 바꾸기 (0) | 2018.08.15 |
---|---|
카메라를 조종하자! - 3인칭 (2/2) (0) | 2018.08.15 |
캐릭터를 조종하자! - 클릭 이동 (2/2) (0) | 2018.08.13 |
캐릭터를 조종하자! - 클릭 이동 (1/2) (0) | 2018.08.13 |
캐릭터를 조종하자! - 입력 관리 (2/2) (0) | 2018.08.12 |