-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathObjectInteraction.cs
More file actions
40 lines (32 loc) · 1.04 KB
/
ObjectInteraction.cs
File metadata and controls
40 lines (32 loc) · 1.04 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
using UnityEngine;
using UnityEngine.Events;
public class ObjectInteraction : MonoBehaviour {
private static Camera mainCam;
private static float interactDist = 5;
public UnityEvent OnInteract;
/// <summary>
/// Call the interact from the centre of the screen
/// </summary>
public static void Interact() {
Interact(new Vector2(.5f, .5f));
}
/// <summary>
/// Call the interact from the position of the mouse on the screen
/// </summary>
public static void InteractMouse() {
if (mainCam == null)
mainCam = Camera.main;
Interact(mainCam.ScreenToViewportPoint(Input.mousePosition));
}
/// <summary>
/// Call the interact from wherever tf u want in normalized viewport coords
/// </summary>
public static void Interact(Vector2 normalizedPos) {
if (mainCam == null)
mainCam = Camera.main;
Ray ray = mainCam.ViewportPointToRay(normalizedPos);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, interactDist))
hit.transform.GetComponent<ObjectInteraction>()?.OnInteract.Invoke();
}
}