아카이브

캐릭터 이동 본문

유니티 심화

캐릭터 이동

timbercat 2023. 8. 17. 18:21

PlayerController

using System;
using System.Collections;
using System.Collections.Generic;
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 = 10.0f;
    [SerializeField]
    private float turnSpeed = 80f;
    [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>();

        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;
        //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");
                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);
        }
        else
        {
            //idle 
            this.anim.CrossFade(eAnimState.Idle.ToString(), 0.25f);
        }
    }
}

Main

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

public class Main : MonoBehaviour
{
    [SerializeField]
    private Button btn;
    [SerializeField]
    private Transform playerTrans;
    //[SerializeField]
    //private float rotateSpeed = 1f;

    // Start is called before the first frame update
    void Start()
    {
        this.btn.onClick.AddListener(() => {
            //this.playerTrans.Rotate(new Vector3(0, 30f, 0));
            //this.playerTrans.Rotate(0, 30f, 0);
            this.playerTrans.Rotate(Vector3.up, 30f);   //y축으로 30도 만큼 회전 
        });
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}