-
04/29 유니티 Hero Adventure 컨트롤 시스템 구현유니티 2021. 4. 29. 18:02
공격 모션시, 이동과 다시 공격을 못하도록 구현
공격 시, 캐릭터 앞 부채꼴 영역에 들어온 Enemy태그의 적을 취득하여 데미지를 주도록 함
참고 사이트
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); } }
'유니티' 카테고리의 다른 글
05/10 TortoiseSVN Clean up, Update가 안될 때 (0) 2021.05.10 04/30 유니티 UGUI 캐릭터 위에 hud 띄우기 (0) 2021.05.03 04/29 유니티 케릭터 컨트롤 + 인풋 R&D (0) 2021.04.29 04/26 유니티 입력값 출력 복습 NGUI, UGUI (0) 2021.04.27 04/26 유니티 NGUI slider 사용 연습 (0) 2021.04.26