This script is attached to the player :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FIMSpace.FLook;
using TMPro;
public class TargetInteraction : MonoBehaviour
{
public List<InteractableItem> targets = new List<InteractableItem>();
public TextMeshProUGUI text;
public GameObject descriptionTextImage;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
foreach(var target in targets)
{
if(InteractableItem.InteractableMode.ActionWithoutThrow == target.interactableMode)
{
Vector3 lookAt = target.transform.position - transform.position;
lookAt.y = 0f;
if (lookAt.magnitude > target.distance) continue;
float dotProduct = Vector3.Dot(new Vector3(transform.forward.x, 0f, transform.forward.z).normalized, lookAt.normalized);
float lookWeight = Mathf.Clamp(dotProduct, 0f, 1f);
if (lookWeight > 0.5f)
{
var distance = Vector3.Distance(transform.position, target.transform.position);
if (distance <= target.distance)
{
descriptionTextImage.SetActive(true);
text.text = target.description;
}
else
{
text.text = "";
descriptionTextImage.SetActive(false);
}
}
}
}
}
}
The goal is to detect first if the player is facing the target possible with a range when facing the target than when facing the target to calculate the distance and to show/not show the text depending on facing and the target and the distance.
In this case the target distance is 1.7f but it’s not working as expected.
The text show when getting close enough to the target but than the text is keep showing never turned off and the part with the Vector3.dot is not working no matter if I’m facing or not the target the text is keep showing all the time.
This script is attached to each object that I want to make as target :
And depending on the target mode for example if it’s description only show text if it’s action or action without throw do something else in the TargetInteraction script.
I want to decide this way what action the player should take and do when interacting with a target.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System;
public class InteractableItem : MonoBehaviour
{
public string currentMode;
private InteractableMode currentInteractableMode;
private string currentDesription;
private void Awake()
{
}
public enum InteractableMode
{
Description,
Action,
ActionWithoutThrow
};
public InteractableMode interactableMode = InteractableMode.Description;
public float distance;
[TextArea(1, 10)]
public string description = "";
public bool IsAnyAction()
{
return interactableMode == InteractableMode.ActionWithoutThrow || interactableMode == InteractableMode.Action;
}
public bool IsActionWithoutThrow()
{
return interactableMode == InteractableMode.ActionWithoutThrow;
}
private void Start()
{
currentMode = GetComponent<InteractableItem>().interactableMode.ToString();
currentInteractableMode = GetComponent<InteractableItem>().interactableMode;
currentDesription = GetComponent<InteractableItem>().description;
}
private void Update()
{
}
}