Sword Halo & Arrow to the nearest enemy

This commit is contained in:
2023-10-17 10:55:31 +02:00
parent eb51da13ed
commit ec1084bd10
23 changed files with 1097 additions and 3 deletions
+68
View File
@@ -39,6 +39,7 @@ Transform:
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 4152598028236711869}
- {fileID: 4740856695113674186}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!212 &4824869534615406026
@@ -280,6 +281,73 @@ MonoBehaviour:
laser: {fileID: 4893257253370123985, guid: 95cb513377bf0e248bd66d6f7ddae920, type: 3}
laserOnX: 0
laserOnY: 0
arrow: {fileID: 6280786298679187013, guid: 856a449688373494987701d49b79a7af, type: 3}
--- !u!1001 &993819705047970090
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
serializedVersion: 3
m_TransformParent: {fileID: 3622515600777588477}
m_Modifications:
- target: {fileID: -8230784050683220836, guid: fd7d128113472244d88a3fc511da99a0, type: 3}
propertyPath: initialSwordsCount
value: 1
objectReference: {fileID: 0}
- target: {fileID: 3957911104700806788, guid: fd7d128113472244d88a3fc511da99a0, type: 3}
propertyPath: m_Name
value: Halo
objectReference: {fileID: 0}
- target: {fileID: 5476423095808072928, guid: fd7d128113472244d88a3fc511da99a0, type: 3}
propertyPath: m_LocalPosition.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5476423095808072928, guid: fd7d128113472244d88a3fc511da99a0, type: 3}
propertyPath: m_LocalPosition.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5476423095808072928, guid: fd7d128113472244d88a3fc511da99a0, type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5476423095808072928, guid: fd7d128113472244d88a3fc511da99a0, type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 5476423095808072928, guid: fd7d128113472244d88a3fc511da99a0, type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5476423095808072928, guid: fd7d128113472244d88a3fc511da99a0, type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5476423095808072928, guid: fd7d128113472244d88a3fc511da99a0, type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5476423095808072928, guid: fd7d128113472244d88a3fc511da99a0, type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5476423095808072928, guid: fd7d128113472244d88a3fc511da99a0, type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5476423095808072928, guid: fd7d128113472244d88a3fc511da99a0, type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
m_RemovedComponents: []
m_RemovedGameObjects: []
m_AddedGameObjects: []
m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: fd7d128113472244d88a3fc511da99a0, type: 3}
--- !u!4 &4740856695113674186 stripped
Transform:
m_CorrespondingSourceObject: {fileID: 5476423095808072928, guid: fd7d128113472244d88a3fc511da99a0, type: 3}
m_PrefabInstance: {fileID: 993819705047970090}
m_PrefabAsset: {fileID: 0}
--- !u!1001 &1611953582756194365
PrefabInstance:
m_ObjectHideFlags: 0
+54 -1
View File
@@ -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;
}
}