using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerInteraction : MonoBehaviour
{
[Header("Raycasting")]
[SerializeField] float Reach = 3f;
Interactable currentInteractable;
[Header("Keys")]
[SerializeField] KeyCode Interact = KeyCode.E;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
CheckInteraction();
if (Input.GetKeyDown(Interact) && currentInteractable != null)
{
currentInteractable.Interact();
}
}
void CheckInteraction()
{
RaycastHit hit;
Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
if (Physics.Raycast(ray, out hit, Reach))
{
if (hit.collider.tag = "Interactable")
{
Interactable newInteractable = hit.collider.GetComponent<Interactable>();
if (currentInteractable && newInteractable != currentInteractable)
{
currentInteractable.disOutline();
}
if (newInteractable.enabled)
{
SetNewCurrentInteractable(newInteractable);
}
else
{
DisableCurrentInteractable();
}
}
else
{
DisableCurrentInteractable();
}
}
else
{
DisableCurrentInteractable();
}
}
void SetNewCurrentInteractable(Interactable newInteractable)
{
currentInteractable = newInteractable;
currentInteractable.Outline();
}
void DisableCurrentInteractable()
{
if (currentInteractable)
{
currentInteractable.disOutline();
currentInteractable = null;
}
}
}
To explain, I have been using a tutorial to be able to make an interaction system. Problem is, I keep seeing them use a variable called “Interactable”, but Unity wont seem to identify this variable. I might just be really dumb, but I can’t fix this by myself. I might need to watch a more in depht tutorial that actually explains everything. And I’m pretty sure this isn’t even the best way to do it. I would still like to know why this doesnt work to help me improve my coding skills. Thank you!