Hello, I am having an issue with making melee combat work. I am following a tutorial by Brackeys on melee combat, but when I try to compile this code, I get error CS0029: Cannot implicitly convert type ‘UnityEngine.Collider2D’ to ‘UnityEngine.Collider2D’. I am not sure how to fix this. Everything I’ve tried has not worked, and every answer to similar situations I have found I have been unable to properly understand as my knowledge of programming is extremely limited. Thank you for any help you are able to provide.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCombat : MonoBehaviour
{
public Animator animator;
public Transform attackPoint;
public LayerMask enemyLayers;
public float attackRange = 0.5f;
public int attackDamage = 1;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.N))
{
Attack();
}
}
void Attack()
{
//play an attack animation
animator.SetTrigger("Attack");
//detect enemies in range of an attack
Collider2D[] hitEnemies = Physics2D.OverlapCircle(attackPoint.position, attackRange, enemyLayers);
//damage them
foreach(Collider2D enemy in hitEnemies)
{
enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
}
}
public void OnDrawGizmosSelected()
{
if (attackPoint == null)
return;
Gizmos.DrawWireSphere(attackPoint.position, attackRange);
}
I'm in love....
– RosenberryThank you so much for your help, that fixed the errors right away.
– Whitworth