티스토리 뷰

#player의 애니매이션 구현, 내각 만들어주기

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

public class DeerStandard : MonoBehaviour
{
    public enum eState
    {
        None = -1, Idle, Run, Attack
    }

    public float radius = 30;
    public Transform target;
    private Animator anim;
    private Vector3 dir;
    private eState state;
    private Coroutine routine; 
    public float speed = 1f;
    public GameObject modelGo; //player

    public bool isPlayer;
    void Start()
    {
        this.anim = this.modelGo.GetComponent<Animator>();

    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space)) //Attack
        {
            if (this.routine != null) StopCoroutine(this.routine); //실행중인 코루틴이있다면
            this.routine = this.StartCoroutine(this.CorAttack());
        }

        if (this.state != eState.Attack)
        {
            var h = Input.GetAxisRaw("Horizontal");
            var v = Input.GetAxisRaw("Vertical");
            dir = Vector3.Normalize(new Vector3(h, 0, v));

            if (dir != Vector3.zero) //h,v가 0이 아니면
            {
                var angle = Mathf.Atan2(dir.x, dir.z) * Mathf.Rad2Deg; //player의 내각
                this.modelGo.transform.rotation = Quaternion.AngleAxis(angle, Vector3.up);
                this.anim.SetInteger("State", 1);
                this.transform.Translate(this.modelGo.transform.forward * this.speed * Time.deltaTime, Space.World);
                this.state = eState.Run;
            }
            else //dir=Vector3.zero
            {
                this.state = eState.Idle;
                this.anim.SetInteger("State", 0);
            }
        }
        this.OnDrawGizmos();
    }
    private IEnumerator CorAttack()
    {
        this.anim.SetInteger("State", 2);
        var prevState = this.state;

        this.state = eState.Attack;
        Debug.LogFormat("state: <color=yellow>{0}</color>", this.state);

        yield return new WaitForSeconds(0.833f);
        if (prevState == eState.Run)
        {
            this.anim.SetInteger("State", 1);
            this.state = eState.Run;
        }
        else if (prevState == eState.Idle)
        {
            this.anim.SetInteger("State", 0);
            this.state = eState.Idle;
        }
    }
    private void OnDrawGizmos()
    {
        if (!this.isPlayer) return;

        DrawExtension.DrawArrow.DrawWireArc(this.transform.position, this.transform.forward, 90, 1);
    }
}

#Dot를 이용한 player의 내각 구현

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

public class DeerStandard : MonoBehaviour
{
    public enum eState
    {
        None = -1, Idle, Run, Attack
    }

    public float radius = 30;
    public Transform target;
    private Animator anim;
    private Vector3 dir;
    private eState state;
    private Coroutine routine; 
    public float speed = 1f;
    public GameObject modelGo; //player

    public bool isPlayer;
    void Start()
    {
        this.anim = this.modelGo.GetComponent<Animator>();

    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space)) //Attack
        {
            if (this.routine != null) StopCoroutine(this.routine); //실행중인 코루틴이있다면
            this.routine = this.StartCoroutine(this.CorAttack());
        }

        if (this.state != eState.Attack)
        {
            var h = Input.GetAxisRaw("Horizontal");
            var v = Input.GetAxisRaw("Vertical");
            dir = Vector3.Normalize(new Vector3(h, 0, v));

            if (dir != Vector3.zero) //h,v가 0이 아니면
            {
                var angle = Mathf.Atan2(dir.x, dir.z) * Mathf.Rad2Deg; //player의 내각
                this.modelGo.transform.rotation = Quaternion.AngleAxis(angle, Vector3.up);
                this.anim.SetInteger("State", 1);
                this.transform.Translate(this.modelGo.transform.forward * this.speed * Time.deltaTime, Space.World);
                this.state = eState.Run;
            }
            else //dir=Vector3.zero
            {
                this.state = eState.Idle;
                this.anim.SetInteger("State", 0);
            }
        }
        this.OnDrawGizmos();
    }
    private IEnumerator CorAttack()
    {
        this.anim.SetInteger("State", 2);
        var prevState = this.state;

        this.state = eState.Attack;
        Debug.LogFormat("state: <color=yellow>{0}</color>", this.state);

        yield return new WaitForSeconds(0.833f);
        if (prevState == eState.Run)
        {
            this.anim.SetInteger("State", 1);
            this.state = eState.Run;
        }
        else if (prevState == eState.Idle)
        {
            this.anim.SetInteger("State", 0);
            this.state = eState.Idle;
        }
    }
    private void OnDrawGizmos()
    {
        if (!this.isPlayer) return;

        DrawExtension.DrawArrow.DrawWireArc(this.transform.position, this.transform.forward, 90, 1);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestDot2 : MonoBehaviour
{
    //hero의 내각 구하기
    public Transform hero; //hero 
    public Transform monster; //monster
    
    private float cos;
    
    void Start()
    {
        this.cos = Mathf.Cos(45 * Mathf.Deg2Rad);
    }

    void Update()
    {
        this.monster.transform.RotateAround(this.hero.transform.position, Vector3.up, 50 * Time.deltaTime);
        var toOther = Vector3.Normalize(this.monster.position - this.hero.transform.position);
        DrawExtension.DrawArrow.ForDebug(this.hero.position, toOther, 0f, Color.red, DrawExtension.ArrowType.Solid);
        var dot = Vector3.Dot(this.hero.transform.forward, toOther);
        //Debug.LogFormat("Dot:{0}", dot);
        if (dot > this.cos)
        {
            Debug.Log("<color=red>in sight!</color>");
        }
        else
        {
            Debug.Log("<color=blue>out sight!</color>");

        }
    }
}

 

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함