Forum rules - please read before posting.

NPC Facing Object/Player, and running ActionList based on that

Hello.
I've got a question regarding NPC/Facing Object (Player). Context: I'd like to have two ActionLists on an NPC. And I'd like them to run depending on whether the NPC is facing the character or not. For example, if the character is facing the player, run the text "I can see you", If the character is not facing the player "I can't see you". I'd like to use this logic for enemies in particular. Nothing extremely complicated; I thought of a few methods, but i'd like to hear your thoughts; perhaps there's an easier way I don't know of yet. As usual, thank you in advance, and have a wonderful week ahead.

https://drive.google.com/file/d/19jI-cfG47nMN7hoqXZ5Y1ld4hzCiMmbX/view?usp=sharing

Comments

  • edited September 2023

    One technique is to attach a Trigger to the NPC that is used to detect the Player's proximity. It doesn't account for walls etc, but an example of this can be found here in the 2.5D tutorial.

    The most robust way to currently achieve this would be to rely on a custom script to detect if the Player is within the NPC's line of sight, and then run one of the two ActionLists accordingly.

    A well-explained function to handle this detection can be found in Unity's Discussions site here:

    https://discussions.unity.com/t/how-to-check-if-player-is-inside-enemy-field-of-view-without-rendertexture/244249/2

    Adapted to run one of two ActionLists based on this:

    using UnityEngine;
    using AC;
    
    public class DetectPlayer : MonoBehaviour
    {
    
        public ActionList canSeeActionList;
        public ActionList cannotSeeActionList;
    
        public float viewRange = 4f;
        public float viewAngle = 40f;
    
        public void RunActionList ()
        {
            if (CanSeePlayer ())
                canSeeActionList.Interact ();
            else
                cannotSeeActionList.Interact ();
        }
    
    
        private bool CanSeePlayer ()
        {
            Transform target = KickStarter.player.transform;
            Vector3 toTarget = target.position - transform.position;
            if (Vector3.Angle (transform.forward, toTarget) <= viewAngle) 
            {
                RaycastHit hit;
                if (Physics.Raycast(transform.position, toTarget, out hit, viewRange))
                {
                    if (hit.transform.root == target)
                        return true;
                }
            }
    
            return false;
        }
    
    }
    

    To use it, attach to the NPC and fill in its Inspector. To have it run, call its RunActionList function - either through custom scripting, or with the Object: Call event or Object: Send message Actions.

  • thank you again! worked like a charm. if unity doesn't implode and we manage to finish the demo this month, we're adding you in the credits and we'll make sure to promtoe Adventure Creator every change we get.

    I have a non related question to this thread. Is it possible to disable custom scripts via an action list? I've added the a trigger when running to the NPC. Now when you run, it triggers a follow Action List. But I'd like to disable it during other actions to make sure it doesn't interrupt anything when the player runs. Thank you again in advance for everything.

  • The Object: Call event Action can be used to set the "Enabled" state of a specific component, but it may be easier to embed your "ignore" condition into the custom script itself.

    For example, if you want to prevent the RunActionList function from having an effect outside of gameplay, add the following to the top:

    if (KickStarter.stateHandler.IsInGameplay ()) return;
    

    You can also have it read the value of a Variable - see the front page of the Scripting Guide for details.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.