Interactable Class

This commit is contained in:
DerTyp187
2021-09-23 20:09:12 +02:00
parent c268d0c489
commit 904b534695
331 changed files with 59287 additions and 7 deletions

View File

@@ -0,0 +1,18 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class Interactable : MonoBehaviour
{
public enum InteractionType //Interaction Types (Enum hei<65>t enumeration also Aufz<66>hlung)
{
Click,
Hold
}
public InteractionType interactionType;
public abstract string GetDescription();
public abstract void Interact();
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6b05f8f10a81c3944b365e107a239f5f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,35 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LightSwitch : Interactable
{
public Light m_Light;
public bool isOn;
private void Start()
{
UpdateLight();
}
private void UpdateLight()
{
m_Light.enabled = isOn;
}
public override string GetDescription()
{
if (isOn)
{
return "Press [E] to turn <color=red>off</color> the light.";
}
return "Press [E] to turn <color=green>on</color> the light.";
}
public override void Interact()
{
isOn = !isOn;
Debug.Log("Click Light");
UpdateLight();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 881fede46ab99194e94dc21cd5bd202f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,61 @@
using UnityEngine;
public class PlayerInteraction : MonoBehaviour
{
public float interactionDistance;
public TMPro.TextMeshProUGUI interactionText;
Camera cam;
void Start()
{
cam = Camera.main;
}
// Update is called once per frame
void Update()
{
Ray ray = cam.ScreenPointToRay(new Vector3(Screen.width / 2f, Screen.height / 2f, 0f));
RaycastHit hit;
bool successfulHit = false;
if (Physics.Raycast(ray, out hit, interactionDistance))
{
Interactable interactable = hit.collider.GetComponent<Interactable>();
if (interactable != null)
{
HandleInteraction(interactable);
interactionText.text = interactable.GetDescription();
successfulHit = true;
}
}
// if we miss, hide the UI
if (!successfulHit)
{
interactionText.text = "";
}
}
void HandleInteraction(Interactable interactable)
{
switch (interactable.interactionType)
{
case Interactable.InteractionType.Click:
// interaction type is click and we clicked the button -> interact
if (Input.GetButtonDown("Interact"))
{
Debug.Log("INTERACT");
interactable.Interact();
}
break;
default:
throw new System.Exception("Unsupported type of interactable.");
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 49d5033db90749f43a12d57f82fcf8be
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: