-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddForceToNearbyShips.cs
More file actions
44 lines (38 loc) · 1.14 KB
/
AddForceToNearbyShips.cs
File metadata and controls
44 lines (38 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace _Game
{
public class AddForceToNearbyShips : MonoBehaviour
{
public float Force = 100f;
public float MaxDistance = 100f;
public ForceMode Mode = ForceMode.Impulse;
public float DelaySecs = 0;
Collider[] nearbyColliders = new Collider[40];
int nearbyCollidersTotal = 0;
void Start()
{
if (!GameManager.IsServer)
return;
StartCoroutine(StartCo());
}
IEnumerator StartCo() {
if (DelaySecs > 0)
yield return new WaitForSecondsRealtime(DelaySecs);
nearbyCollidersTotal = Physics.OverlapSphereNonAlloc(transform.position, MaxDistance, nearbyColliders, GameManager.Instance.SpaceshipsLayerMask);
if (nearbyCollidersTotal > 0)
{
for(int i=0; i < nearbyCollidersTotal; i++) {
var collider = nearbyColliders[i];
var ship = collider.GetComponentInParent<Ship>();
if (ship)
{
var direction = (ship.transform.position - transform.position);
ship.AddForce(direction.normalized * (Force / (direction.magnitude/(MaxDistance/3))), Mode);
}
};
}
}
}
}