i am doing the unity 2d combat rpg course and i am stuck in the the part where we have to create sword swing both direction…
when i click play the sword gets detached i checked the code no errors…`using UnityEngine;
public class Sword : MonoBehaviour
{
private PlayerControls playerControls;
private Animator myAnimator;
private PlayerController playerController;
private ActiveWeapon activeWeapon;
private void Awake()
{
playerController = GetComponentInParent<PlayerController>();
activeWeapon = GetComponentInParent<ActiveWeapon>();
myAnimator = GetComponent<Animator>();
playerControls = new PlayerControls();
}
private void OnEnable()
{
playerControls.Enable();
}
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
playerControls.Combat.Attack.started += _ => Attack();
}
private void Update()
{
MouseFollowWithOffset();
}
private void Attack()
{
myAnimator.SetTrigger("Attack");
}
private void MouseFollowWithOffset()
{
Vector3 mousePos = Input.mousePosition;
Vector3 playerScreenPoint = Camera.main.WorldToScreenPoint(playerController.transform.position);
if (mousePos.x < playerScreenPoint.x)
{
activeWeapon.transform.rotation = Quaternion.Euler(0, -180, 0);
} else
{
activeWeapon.transform.rotation = Quaternion.Euler(0, 0, 0);
}
}
}`