The script is attached to the player object.
When I click on object that is tagged as Interactable object I want to make some action for example to open a door to move something to change something.
In this case I have two objects that tagged is Interactable.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DetectInteractable : MonoBehaviour
{
public Camera cam;
public float distanceToSee;
public string objectHit;
public bool interactableObject = false;
public Transform parentToSearch;
public static bool detected = false;
private RaycastHit whatObjectHit;
private bool clickForDescription = false;
private int layerMask = 1 << 8;
private GameObject objectWasHit;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
clickForDescription = true;
if (whatObjectHit.collider != null)
ExecuteActions(whatObjectHit.collider.gameObject);
}
Debug.DrawRay(cam.transform.position, cam.transform.forward * distanceToSee, Color.magenta);
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out whatObjectHit, distanceToSee, layerMask))
{
objectWasHit = whatObjectHit.collider.gameObject;
detected = true;
objectHit = whatObjectHit.collider.gameObject.name;
interactableObject = true;
print("Hit ! " + whatObjectHit.collider.gameObject.name);
}
else
{
detected = false;
clickForDescription = false;
print("Not Hit !");
}
}
private void ExecuteActions(GameObject go)
{
ItemAction iaa = go.GetComponent<ItemAction>();
//foreach (ItemAction ia in go.GetComponentsInChildren<ItemAction>())
//{
if (iaa != null)
iaa.ItemMove();
//}
}
private void OnGUI()
{
if (clickForDescription == true)
{
ProcessOnGUI(parentToSearch);
}
}
void ProcessOnGUI(Transform parent, int level = 0)
{
foreach (Transform child in parent)
{
if (child.GetComponent<ItemInformation>() != null)
{
ItemInformation iteminformation = child.GetComponent<ItemInformation>();
if (child.name == objectHit)
{
var centeredStyle = GUI.skin.GetStyle("Label");
centeredStyle.alignment = TextAnchor.UpperCenter;
GUI.Box(new Rect(
Screen.width / 2 - 50 + 20 * level, // <== INDENTATION
Screen.height / 2 - 25, 100, 50),
iteminformation.description, centeredStyle);
}
}
// Process next deeper level
ProcessOnGUI(child, level + 1);
}
}
public class ViewableObject : MonoBehaviour
{
public string displayText;
public bool isInteractable;
}
}
The part with the ItemInformation was each each object that attached to it the ItemInformation I addeedd to it another text:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemInformation : MonoBehaviour
{
[TextArea]
public string description;
}
So If I click on a window for example it will describe the window with some text and if I click on a door then it will show some text about the door.
Now I want to make something like this with the same idea but for actions.
For example if I click on a door open the door but if I click on a window do something else with/to the window.
So I added this part on them mouse click:
if (whatObjectHit.collider != null)
ExecuteActions(whatObjectHit.collider.gameObject);
Then:
private void ExecuteActions(GameObject go)
{
ItemAction iaa = go.GetComponent<ItemAction>();
//foreach (ItemAction ia in go.GetComponentsInChildren<ItemAction>())
//{
if (iaa != null)
iaa.ItemMove();
//}
}
And then the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemAction : MonoBehaviour
{
public virtual void ItemMove()
{
}
public virtual void ActivateWindowCamera()
{
}
}
And then I have for example a script for the ItemMove.
But what I don’t understand is how to identify when I click on a object what action(method) to call to: ItemMove or maybe ActivateWindowCamera ? And the script ItemAction I can’t attach it to any object I want to have action since it have some methods in it.
In other words it’s not working like the ItemInformation idea. I’m not sure how to make the action part.