using System.Collections; using System.Collections.Generic; using UnityEngine; public class Sword : MonoBehaviour, IWeapon { [SerializeField] private GameObject slashAnimPrefab; [SerializeField] private Transform slashAnimSpawnPoint; [SerializeField] private WeaponInfo weaponInfo; private Transform weaponCollider; private Animator myAnimator; private GameObject slashAnim; private void Awake() { myAnimator = GetComponent(); } private void Start() { weaponCollider = PlayerController.Instance.GetWeaponCollider(); slashAnimSpawnPoint = GameObject.Find("SlashSpawnPoint").transform; } private void Update() { MouseFollowWithOffset(); } public WeaponInfo GetWeaponInfo() { return weaponInfo; } public void Attack() { myAnimator.SetTrigger("Attack"); weaponCollider.gameObject.SetActive(true); slashAnim = Instantiate(slashAnimPrefab, slashAnimSpawnPoint.position, Quaternion.identity); slashAnim.transform.parent = this.transform.parent; } public void DoneAttackingAnimEvent() { weaponCollider.gameObject.SetActive(false); } public void SwingUpFlipAnimEvent() { slashAnim.gameObject.transform.rotation = Quaternion.Euler(-180, 0, 0); if (PlayerController.Instance.FacingLeft) { slashAnim.GetComponent().flipX = true; } } public void SwingDownFlipAnimEvent() { slashAnim.gameObject.transform.rotation = Quaternion.Euler(0, 0, 0); if (PlayerController.Instance.FacingLeft) { slashAnim.GetComponent().flipX = true; } } private void MouseFollowWithOffset() { Vector3 mousePos = Input.mousePosition; Vector3 playerScreenPoint = Camera.main.WorldToScreenPoint(PlayerController.Instance.transform.position); float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg; if (mousePos.x < playerScreenPoint.x) { ActiveWeapon.Instance.transform.rotation = Quaternion.Euler(0, -180, angle); weaponCollider.transform.rotation = Quaternion.Euler(0, -180, 0); } else { ActiveWeapon.Instance.transform.rotation = Quaternion.Euler(0, 0, angle); weaponCollider.transform.rotation = Quaternion.Euler(0, 0, 0); } } }