유니티 심화

절대강좌 유니티 - 카메라 distance, height 이동

다모아 2023. 8. 18. 11:53

FollowCam

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

public class FollowCam : MonoBehaviour
{
    [SerializeField]
    private Transform targetTr;
    
    [SerializeField]
    [Range(0, 20f)]
    private float distance = 3f;

    [SerializeField]
    [Range(0, 10f)]
    private float height = 2f;
    private void LateUpdate()
    {
        this.transform.position = this.targetTr.position + -this.targetTr.forward * this.distance + Vector3.up * this.height;
    }

    private void OnDrawGizmos()
    {

        DrawArrow.ForGizmo(this.targetTr.position, -this.targetTr.forward * this.distance);
        DrawArrow.ForGizmo(-this.targetTr.forward * this.distance, Vector3.up * this.height);
    }
}

 

PlayerController

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

public class PlayerController : MonoBehaviour
{
    [SerializeField]
    private float moveSpeed = 5f;
    [SerializeField]
    private float turnSpeed = 800f;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        float x = Input.GetAxis("Mouse X");

        Vector3 moveDir = this.transform.forward * v + this.transform.right * h;

        this.transform.Translate(moveDir.normalized * this.moveSpeed * Time.deltaTime);

        DrawArrow.ForDebug(this.transform.position, moveDir.normalized, 0.1f);

        if(Input.GetMouseButton(0))
        {
            this.transform.Rotate(Vector3.up * x * this.turnSpeed * Time.deltaTime);
        }
        
    }


}

 

[Range(숫자, 숫자)] 로 카메라 이동시키기

'유니티 심화' 카테고리의 다른 글

Lerp, Slerp  (0) 2023.08.18
캐릭터 애니메이션 추가  (0) 2023.08.18
Vector3.normalized  (0) 2023.08.18
Lerp, 줌인, 줌아웃, LookAt, 카메라 팔로잉  (0) 2023.08.17
Vector  (0) 2023.08.17