mirror of
https://github.com/DerTyp7/fps-citybuild-unity.git
synced 2025-10-29 20:22:08 +01:00
Interactable Class
This commit is contained in:
18
Assets/Scripts/Interactable.cs
Normal file
18
Assets/Scripts/Interactable.cs
Normal 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();
|
||||
|
||||
}
|
||||
11
Assets/Scripts/Interactable.cs.meta
Normal file
11
Assets/Scripts/Interactable.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6b05f8f10a81c3944b365e107a239f5f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
35
Assets/Scripts/LightSwitch.cs
Normal file
35
Assets/Scripts/LightSwitch.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/LightSwitch.cs.meta
Normal file
11
Assets/Scripts/LightSwitch.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 881fede46ab99194e94dc21cd5bd202f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
61
Assets/Scripts/PlayerInteraction.cs
Normal file
61
Assets/Scripts/PlayerInteraction.cs
Normal 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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/PlayerInteraction.cs.meta
Normal file
11
Assets/Scripts/PlayerInteraction.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 49d5033db90749f43a12d57f82fcf8be
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user