-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteractObject.cs
More file actions
54 lines (47 loc) · 1.63 KB
/
Copy pathInteractObject.cs
File metadata and controls
54 lines (47 loc) · 1.63 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
45
46
47
48
49
50
51
52
53
54
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InteractObject : MonoBehaviour
{
public string interactMessage;
public float interactionDistance = 5f;
private Transform player;
private PlayerMovement playerMovement;
private PlayerCam playerCam;
[HideInInspector] public bool interactedOn = false;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
playerMovement = player.gameObject.GetComponent<PlayerMovement>();
playerCam = player.gameObject.GetComponentInChildren<PlayerCam>();
}
void Update()
{
// Handle Interact UI Render
if (!interactedOn && (Vector3.Distance(playerCam.transform.position, transform.position) <= interactionDistance) && LookingAtObject())
{
FindAnyObjectByType<InteractUI>().InteractWith(interactMessage);
interactedOn = true;
}
else if (interactedOn && (Vector3.Distance(playerCam.transform.position, transform.position) >= interactionDistance || !LookingAtObject()))
{
FindAnyObjectByType<InteractUI>().InteractStop();
interactedOn = false;
}
}
private bool LookingAtObject()
{
RaycastHit hit;
if (Physics.Raycast(playerCam.transform.position, playerCam.transform.forward, out hit, 5f))
{
if (hit.collider != null && hit.collider.gameObject != null)
{
if (hit.collider.gameObject == gameObject)
{
return true;
}
}
}
return false;
}
}