In the sword script, we are calling a blank GameObject to be our slash animation spawn point. I am getting an error message from unity that said GameObject’s position cant be translated into the Instantiate command under the Attack method (CS1503 cannot convert from UnityEngine.GameObject to UnityEngine.Vector3). Changing the GameObject to a Vector3 at the top of the script is one workaround, but I wonder if I am missing something?
public class Sword : MonoBehaviour
{
private PlayerControls playerControls;
private Animator myAnimator;
private PlayerController playerController;
private ActiveWeapon activeWeapon;
[SerializeField] private GameObject slashAnimPrefab;
[SerializeField] private GameObject slashAnimSpawnPoint;
private GameObject slashAnim;
private void Awake()
{
playerController = GetComponentInParent<PlayerController>();
activeWeapon = GetComponentInParent<ActiveWeapon>();
playerControls = new PlayerControls();
myAnimator = GetComponent<Animator>();
}
private void OnEnable()
{
playerControls.Enable();
}
private void Start()
{
playerControls.Combat.Attack.started += _ => Attack();
}
private void Update()
{
MouseFollowWithOffset();
}
private void Attack()
{
myAnimator.SetTrigger("Attack");
slashAnim = Instantiate(slashAnimPrefab, slashAnimSpawnPoint, Quaternion.identity);
slashAnim.transform.parent = this.transform.parent;
}
private void MouseFollowWithOffset()
{
Vector3 mousePos = Input.mousePosition;
Vector3 playerScreenPoint = Camera.main.WorldToScreenPoint(playerController.transform.position);
float angle = Mathf.Atan2(mousePos.x, mousePos.y) * Mathf.Rad2Deg;
if (mousePos.x < playerScreenPoint.x)
{
activeWeapon.transform.rotation = Quaternion.Euler(0, -180, angle);
}
else
{
activeWeapon.transform.rotation = Quaternion.Euler(0, 0, angle);