-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtil.cs
81 lines (65 loc) · 2.73 KB
/
Util.cs
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
Util.cs
=======
Description: Utility functions
*/
using System;
using System.Linq;
using UnityEngine;
namespace EasyCompany
{
internal static class Util
{
// Find the nearest Component to the player of a given type matching a specified function
// Every game object should be a Component
public static T FindNearestComponentOfTypeWhere<T>(Func<T, bool> whereFn) where T : Component
{
T result = null;
float minDist = float.MaxValue;
UnityEngine.Vector3 playerPos = GameNetworkManager.Instance.localPlayerController.gameplayCamera.transform.position;
foreach (T obj in UnityEngine.Object.FindObjectsOfType<T>().Where(whereFn))
{
float dist = UnityEngine.Vector3.Distance(playerPos, obj.transform.position);
// If closer, pick this object as new nearest object
if (dist < minDist)
{
minDist = dist;
result = obj;
}
}
return result;
}
// Find nearest Component to the player of a given type
public static T FindNearestComponentOfType<T>() where T : Component
{
return FindNearestComponentOfTypeWhere<T>(c => true);
}
// Raycast from the player's position/camera rotation, stopping if a collision is found within maxDist units.
// If a collision was found, returns true and sets hit to the location of the collision.
public static bool RaycastFromPlayer(out UnityEngine.Vector3 hit, float maxDist = Mathf.Infinity)
{
// Ensure we can get player camera transform
if (GameNetworkManager.Instance?.localPlayerController?.gameplayCamera == null)
{
Main.log.LogWarning("RaycastFromPlayer unable to get gameplayCamera!");
hit = new UnityEngine.Vector3();
return false;
}
Transform camera = GameNetworkManager.Instance.localPlayerController.gameplayCamera.transform;
// Perform raycasting
// HACK: Shift camera position up when raycasting to give better result
var ray = new Ray(camera.position + Vector3.up * 0.5f, camera.forward);
RaycastHit rayHit;
// NOTE: LayerMask taken from Turret.CheckForPlayersInLineOfSight()
if (Physics.Raycast(ray, out rayHit, maxDist, 1051400, QueryTriggerInteraction.Ignore))
{
// Collision detected
hit = rayHit.point;
return true;
}
// No collision
hit = UnityEngine.Vector3.zero;
return false;
}
}
}