본문 바로가기
KDT/유니티 심화

23/08/17 이동시키기

by 잰쟁 2023. 8. 18.
728x90

PlayerController

using System.Collections;
using System.Collections.Generic;
using UnityEditor.Experimental.GraphView;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public enum eControlType 
    {
       Keyboad, Joystick
    }
    public enum eAnimState 
    {
        Idle,
        RunB,
        RunF,
        RunL,
        RunR
    }


    private Transform tr;
    public float moveSpeed;
    [SerializeField]
    private float turnSpeed;
    [SerializeField]
    private VariableJoystick joystick;
    [SerializeField]
    private eControlType controlType;
    private Animation anim;
    private bool isDown;
    private Vector3 downPosition;


    private void Start()
    {
        this.tr = this.GetComponent<Transform>();
        this.anim = this.GetComponent<Animation>();
        //방법1
        //this.anim.Play(eAnimState.Idle.ToString());
        //방법2
        this.anim.clip = this.anim.GetClip(eAnimState.Idle.ToString());
        this.anim.Play();
    }

    void Update()
    {
        float h = 0f;
        float v = 0f;

        if(this.controlType == eControlType.Keyboad)
        {
            h = Input.GetAxisRaw("Horizontal");
            v = Input.GetAxisRaw("Vertical");
        }
        else if(this.controlType == eControlType.Joystick)
        {
            h = this.joystick.Direction.x;
            v = this.joystick.Direction.y; 
        }
     
        //키보드
        var dir = new Vector3(h, 0, v);

        //회전
        //float angle = Mathf.Atan2(dir.x, dir.z) * Mathf.Rad2Deg;
        //Debug.Log(angle);
        //this.transform.localRotation = Quaternion.AngleAxis(angle, Vector3.up);

        //마우스를 따라서 회전하기
        //var r = Input.GetAxis("Mouse X");
        //this.transform.Rotate(축 * 방향 * 시간 * 속도);
        //this.transform.Rotate(Vector3.up * r * Time.deltaTime * this.turnSpeed);

        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("Down");
            this.isDown = true;
            this.downPosition = Input.mousePosition;
        }
        else if (Input.GetMouseButtonUp(0))
        {
            Debug.Log("Up");
            this.isDown = false;
        }

        if (this.isDown)
        {
            if(this.downPosition != Input.mousePosition) 
            {
                //마우스를 따라서 움직임
                var r = Input.GetAxis("Mouse X");
                //마우스 위치를 -1,0,1로 나타냄
                var rotDir = Mathf.Sign(r);
                //마우스 위치를 따라서 회전
                this.transform.Rotate(Vector3.up * rotDir * Time.deltaTime * this.turnSpeed);
                this.downPosition = Input.mousePosition;
            }
            
           
        }

        if (dir != Vector3.zero)
        {
            //이동
            this.transform.Translate(dir * this.moveSpeed * Time.deltaTime,Space.World);
        }

        this.PlayAnimation(dir);

  
    }

    private void PlayAnimation(Vector3 dir) 
    {
        if(dir.x > 0)
        {
            //right
            this.anim.CrossFade(eAnimState.RunR.ToString(), 0.25f);
        }
        else if(dir.x < 0)
        {
            //Left
            this.anim.CrossFade(eAnimState.RunL.ToString(), 0.25f);
        }
        else if(dir.z > 0)
        {
            //Forward
            this.anim.CrossFade(eAnimState.RunF.ToString(), 0.25f);
        }
        else if(dir.z < 0)
        {
            //Back
            this.anim.CrossFade(eAnimState.RunB.ToString(), 0.25f);
        }
    }
        
    
}

Follow Cam

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

public class FollowCam : MonoBehaviour
{
    [SerializeField]
    private Transform playerTrans;
    [SerializeField]
    private Transform point;
    [SerializeField]
    private float distance;
    [SerializeField]
    private float height = 1f;


    // Start is called before the first frame update
    void Start()
    {
        //벡터 연산

        this.point.position = this.playerTrans.position;

        var tpos = this.playerTrans.position + this.playerTrans.forward * -1 * this.distance;

        tpos = tpos + Vector3.up * height;

        this.transform.position = tpos;

        this.transform.LookAt(this.playerTrans.position);

    }

    // Update is called once per frame
    void LateUpdate()
    {
        //this.transform.position = this.playerTrans.position;
    }


}