유니티 심화/복습

[복습] SpaceShooter - 줌인, 줌아웃, 캐릭터 이동

다모아 2023. 8. 18. 01:17

FollowCam

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

namespace Test_Play
{
    public class FollowCam : MonoBehaviour
    {
        [SerializeField]
        private Transform endTrans;
        [SerializeField]
        private Transform startTrans;
        [SerializeField]
        private Transform shoesTrans;
        [SerializeField]
        private Transform headTrans;
        [SerializeField]
        private float height = 2f;
        [SerializeField]
        private float distance = 10f;
        [SerializeField]
        [Range(0, 1)]
        private float valueLerp;

        private Vector3 tpos;
        // Start is called before the first frame update
        void Start()
        {
            //시작위치
            this.tpos = this.shoesTrans.position + -this.shoesTrans.forward * this.distance;
            this.tpos = this.tpos + Vector3.up * this.height;
            this.transform.position = this.tpos;
        }

        // Update is called once per frame
        void Update()
        {
            //endTrans와 startTrans 카메라 앞 , 뒤 위치 지정
            this.endTrans.position = this.transform.position + -this.transform.forward * 3f;
            this.startTrans.position = this.transform.position + this.transform.forward * 3f;

            //ValueLerp를 움직이면 카메라 시야 Shoes에서 head 사이 이동 Lerp사용
            Vector3 lerp = Vector3.Lerp(this.shoesTrans.position, this.headTrans.position, this.valueLerp);

            //카메라가 보고있는 위치
            this.transform.LookAt(lerp);

            float wheel = Input.GetAxis("Mouse ScrollWheel");

            if (wheel > 0)
            {
                //마우스 휠 위로 [줌인]
                this.transform.position = this.transform.position + this.transform.forward * 0.1f;
            }
            else if(wheel < 0)
            {
                //마우스 휠 아래 [줌아웃]
                this.transform.position = this.transform.position + -this.transform.forward * 0.1f;
            }
        }
    }
}

 

PlayerController

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

namespace Test_Play
{
    public class PlayerController : MonoBehaviour
    {
        private enum eAnimState { 
            Idle, RunB, RunF, RunL, RunR
        }

        [SerializeField]
        private float moveSpeed = 5f;
        private Animation anim;
        // Start is called before the first frame update
        void Start()
        {
            this.anim = this.GetComponent<Animation>();
        }

        // Update is called once per frame
        void Update()
        {
            float h = Input.GetAxisRaw("Horizontal"); // x좌표 -1 ... 1
            float v = Input.GetAxisRaw("Vertical"); // y좌표 -1 ... 1

            Vector3 dir = new Vector3(h, 0, v);

            //이걸 이용해서 .. 키보드 입력으로 캐릭터 움직임
            //dir에 정보들어있음

            if(dir != Vector3.zero) // dir이 (0,0,0)이 아니라면
            {
                this.transform.Translate(dir * this.moveSpeed * Time.deltaTime);
                this.PlayAnimation(dir);
            }
        }
        private void PlayAnimation(Vector3 dir)
        {
            if(dir.x > 0)
            {
                //오른쪽
                this.anim.CrossFade(eAnimState.RunR.ToString(), 0.25f);
            }
            else if(dir.x < 0)
            {
                //왼쪽
                this.anim.CrossFade(eAnimState.RunL.ToString(), 0.25f);
            }
            else if(dir.z > 0)
            {
                //위
                this.anim.CrossFade(eAnimState.RunF.ToString(), 0.25f);
            }
            else if(dir.z < 0)
            {
                //아래
                this.anim.CrossFade(eAnimState.RunB.ToString(), 0.25f);
                //애니메이션 이름, 시간
            }
            else
            {
                //정지
                this.anim.CrossFade(eAnimState.Idle.ToString(), 0.25f);
            }
        }
    }
}

 

마우스 조종 부분은 아직 추가되지 않았다.