Sword Halo & Arrow to the nearest enemy
This commit is contained in:
@@ -1,19 +1,22 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent (typeof(PlayerMovements))]
|
||||
public class PlayerWeapons : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private GameObject laser;
|
||||
[Header("If all are false: only on 1 side")]
|
||||
[SerializeField] private bool laserOnX;
|
||||
[SerializeField] private bool laserOnY;
|
||||
|
||||
[SerializeField] private GameObject arrow;
|
||||
|
||||
private PlayerMovements _pm;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_pm = GetComponent<PlayerMovements>();
|
||||
InvokeRepeating(nameof(ShootBeam), 5f, 5f);
|
||||
InvokeRepeating(nameof(ShootArrow), 2f, 2f);
|
||||
}
|
||||
|
||||
private void ShootBeam()
|
||||
@@ -32,4 +35,54 @@ public class PlayerWeapons : MonoBehaviour
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void ShootArrow()
|
||||
{
|
||||
// Get all enemies in given radius
|
||||
Collider2D[] hits = Physics2D.OverlapCircleAll(transform.position, 20f, LayerMask.GetMask("Enemy"));
|
||||
|
||||
if (hits.Length <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
GameObject target = null;
|
||||
float shortestDistance = Mathf.Infinity;
|
||||
|
||||
for(int i = 0; i < hits.Length; i++)
|
||||
{
|
||||
float distance = Vector3.Distance(transform.position, hits[i].transform.position);
|
||||
if (distance < shortestDistance)
|
||||
{
|
||||
shortestDistance = distance;
|
||||
target = hits[i].gameObject;
|
||||
}
|
||||
}
|
||||
|
||||
// Shoot arrow
|
||||
if (target != null)
|
||||
{
|
||||
GameObject spawnedArrow = Instantiate(arrow, transform.position, Quaternion.identity);
|
||||
spawnedArrow.GetComponent<Arrow>().target = target;
|
||||
}
|
||||
}
|
||||
|
||||
GameObject GetClosestEnemy(GameObject[] enemies)
|
||||
{
|
||||
GameObject bestTarget = null;
|
||||
float closestDistanceSqr = Mathf.Infinity;
|
||||
Vector3 currentPosition = transform.position;
|
||||
foreach (GameObject potentialTarget in enemies)
|
||||
{
|
||||
Vector3 directionToTarget = potentialTarget.transform.position - currentPosition;
|
||||
float dSqrToTarget = directionToTarget.sqrMagnitude;
|
||||
if (dSqrToTarget < closestDistanceSqr)
|
||||
{
|
||||
closestDistanceSqr = dSqrToTarget;
|
||||
bestTarget = potentialTarget;
|
||||
}
|
||||
}
|
||||
|
||||
return bestTarget;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user