ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 04/29 유니티 Hero Adventure 컨트롤 시스템 구현
    유니티 2021. 4. 29. 18:02

     

    공격 모션시, 이동과 다시 공격을 못하도록 구현

    공격 시, 캐릭터 앞 부채꼴 영역에 들어온 Enemy태그의 적을 취득하여 데미지를 주도록 함

     

     

    참고 사이트

    m.blog.naver.com/PostView.nhn?blogId=gooldare&logNo=221479323579&proxyReferer=https:%2F%2Fwww.google.com%2F

     

    using System.Collections;
    using System.Collections.Generic;
    using UnityEditor;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class JoystickTest : MonoBehaviour
    {
        public VariableJoystick joystick;
        public bool isSnap;
        public Transform heroTrans;
        public float speed = 1f;
        public float joypos;
        public Animation anim;
    
        public Button button;
    
    
        public GameObject[] targets;
        public List<GameObject> attackTargets;
    
        public float angleRange = 90f;
        public float distance = 5f;
        public bool isCollision = false;
    
        Color _blue = new Color(0f, 0f, 1f, 0.2f);
        Color _red = new Color(1f, 0f, 0f, 0.2f);
    
        Vector3 direction;
    
        float dotValue = 0f;
    
        // Start is called before the first frame update
        void Start()
        {
            joystick.SnapX = isSnap;
    
            button.onClick.AddListener(() =>
            {
                if (anim.IsPlaying("attack_sword_01"))
                {
                    return;
                }
                anim.Stop();
                anim.Play("attack_sword_01");
                foreach (var enemy in attackTargets)
                {
                    enemy.GetComponent<Enemy>().Damage();
                }
            });
        }
    
        // Update is called once per frame
        void Update()
        {
            
    
            if(Input.GetKeyDown(KeyCode.Space))
            {
                if (anim.IsPlaying("attack_sword_01"))
                {
                    return;
                }
                anim.Stop();
                anim.Play("attack_sword_01");
    
                foreach (var enemy in attackTargets)
                {
                    enemy.GetComponent<Enemy>().Damage();
                }
            }
            //Debug.LogFormat("{0}", joystick.Direction);
            var dir = new Vector3(joystick.Direction.x, 0, joystick.Direction.y);
            var movement = Vector3.forward * this.speed * Time.deltaTime;
            joypos = Mathf.Atan2(joystick.Horizontal, joystick.Vertical) * Mathf.Rad2Deg;
            if (!anim.IsPlaying("attack_sword_01"))
            {
                if(joypos != 0)
                {
                    anim.Play("run@loop");
                    this.heroTrans.Translate(movement);
                    this.heroTrans.eulerAngles = new Vector3(0, joypos, 0);
                }
                else
                {
                    anim.Play("idle@loop");
                }
            }
    
    
            attackTargets.Clear();
    
            targets = GameObject.FindGameObjectsWithTag("Enemy");
    
            dotValue = Mathf.Cos(Mathf.Deg2Rad * (angleRange / 2));
            foreach (var targetGo in targets)
            {
                var target = targetGo.transform;
                direction = target.position - transform.position;
                if (direction.magnitude < distance)
                {
                    if (Vector3.Dot(direction.normalized, transform.forward) > dotValue)
                    {
                        isCollision = true;
                        attackTargets.Add(targetGo);
                    }
                    else
                        isCollision = false;
                }
                else
                    isCollision = false;
            }
    
    
            
            
    
        }
        private void OnDrawGizmos()
        {
            Handles.color = isCollision ? _red : _blue;
    
            Handles.DrawSolidArc(this.transform.position, Vector3.up, transform.forward, angleRange / 2, distance);
            Handles.DrawSolidArc(transform.position, Vector3.up, transform.forward, -angleRange / 2, distance);
    
        }
    
    }
    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Enemy : MonoBehaviour
    {
        public Animation anim;
        public int hp = 50;
        // Start is called before the first frame update
        void Start()
        {
            anim.Play("Anim_Idle");
        }
    
        // Update is called once per frame
        void Update()
        {
            if(!anim.isPlaying)
            {
                anim.Play("Anim_Idle");
            }
        }
    
        public void Damage()
        {
            anim.Play("Anim_Damage");
            hp -= 10;
            if(hp <= 0)
            {
                anim.Play("Anim_Death");
                StartCoroutine(DestroyEnemy());
            }
        }
    
        public IEnumerator DestroyEnemy()
        {
            yield return new WaitForSeconds(0.967f);
            GameObject.Destroy(this.gameObject);
        }
    
    }
    
Designed by Tistory.