How can I find if the player is facing a target and than to check also the distance from the target?

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()
    {
       
    }
}

It sounds like you are close however.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • some value isn’t being set properly (or being set in all cases)

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

1 Like

Also make sure to readibly define important events such as when a target enters and leaves the player’s range using one purpose functions and comments. It’ll massively help in debugging and extensibility.