Forum rules - please read before posting.

Detect hotspot when in front of it & NPC didn't follow the NavMesh

Hello everyone,

My game is set with Direct input with keyboard only and hotspot detection method with player vicinity nearest only. I have two questions:

1) Is there any way to detect hotspots when the player is in front of the hotspot only? Because my current configuration also works when the player is facing in a different position.

2) I set the NPC to follow the player, but after tens of seconds or a minute or two, the NPC and also the player didn't follow the NavMesh and could walk outside the NavMesh. In the case of the player, I set collider 2D to prohibit the player from walking outside, but the colliders didn't work when this problem occurred.

Regards,
Lrei

Comments

  • Is there any way to detect hotspots when the player is in front of the hotspot only?

    If you're meaning to limit detection based on the Player's direction, you can adjust the size/position of the Player's "Hotspot detector" collider to be more front-facing.

    If you're instead looking to limit interaction to only a certain position in front of a given Hotspot, you can assign an "Interacive boundary" to the Hotspot, which is like a "Detector" collider for a Hotspot itself. These can be generated from the Scene Manager. Once assigned to a Hotspot, that Hotspot will only be interactive if the Player is within it's boundary.

    I set the NPC to follow the player, but after tens of seconds or a minute or two, the NPC and also the player didn't follow the NavMesh and could walk outside the NavMesh.

    Is anything else going on during this time frame, or does the issue still occur if you leave the game alone for two minutes and then move around?

    What is your AC version, and can you share screenshots of the issue? In particular, what is the state of the wall colliders and Player Inspector before/after the issue occurs? Presumably the Player has a Rigidbody2D component, but this should always prevent collision through walls unless a collider is turned into a Trigger.

  • Adjusting the size/position of the player's hotspot detector can't bring the effect that I want.
    I want before running the hotspot interaction, check some condition, is there any code that could do this?

    About the NPC follow issue, I have updated AC last version and it works correctly. Thanks.

  • Adjusting the size/position of the player's hotspot detector can't bring the effect that I want.

    If you set the Settings Manager's "Hotspot detection method" to "Custom Script", Hotspots can be selected manually be calling:

    AC.KickStarter.playerInteraction.SetActiveHotspot (myHotspot);
    

    I'm not clear on the exact behaviour you're looking for, though. Can you share screenshots to illustrate the problem?

  • https://drive.google.com/file/d/1_O2OWRtw0jFMOHRqOmMN2YISRMgZaJkH/view?usp=sharing

    https://drive.google.com/file/d/1qUmFHN46Uu98CXxXrcJ83IjmhE07aXpT/view?usp=sharing

    The hotspot interaction works in both two screenshots, but I only want to work with the first screenshot.

    So I try to use raycasting to cast a ray in the player look's direction to check if the object that the ray collides with is the hotspot.
    But with the code that you said, I can't get myHotspot to execute that code.

    Is there any other way to solve it?

  • I can't get myHotspot to execute that code.

    If you can share your code, I can try to see the issue.

    But custom code may not be necessary, going by the screenshots.

    Where is the Player's origin, and where is the Hotspot Detector collider?

    You need to make sure that your Player's origin is by their feet - and set your sprite Pivots to Bottom to make sure they appear correctly. But this is true for any 2D character.

    For the Hotspot Detector: so long as your Animator above it in the Hierarchy, you can update the detector's position in your animations. So when facing downwards, it has a lower Y position value; when facing up, it has a higher Y, etc.

  • edited May 2021
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
            Sprite sprite = child.GetComponent<SpriteRenderer>().sprite;
    
            string name = sprite.name;
    
            if (name.Contains("front"))
            {
                lookDirection = new Vector2(0, -1);
            }
            else if (name.Contains("back"))
            {
                lookDirection = new Vector2(0, 1);
            }
            else if (name.Contains("right"))
            {
                lookDirection = new Vector2(1, 0);
            }
            else if (name.Contains("left"))
            {
                lookDirection = new Vector2(-1, 0);
            }
    
            RaycastHit2D hit = Physics2D.Raycast(rigidbody2d.position + Vector2.up * 0.2f, lookDirection, 1.5f);
            if (hit.collider != null && KickStarter.player.hotspotDetector.NearestHotspot)
            {
                //check if it is the hotspot nearby and then run that hotspot interaction
            }
        }
    }
    

    Both the Player's origin and Hotspot Detector collider are set to the bottom.

    I don't know if my code is right, it could have errors.

    So when facing downwards, it has a lower Y position value; when facing up, it has a higher Y, etc.

    How I can do that? With code?

  • The code doesn't need to involve input or the default Hotspot Detector - it's about setting AC's record of the active Hotspot manually:

    void Update()
    {
        Sprite sprite = child.GetComponent<SpriteRenderer>().sprite;
    
        string name = sprite.name;
    
        if (name.Contains("front"))
        {
            lookDirection = new Vector2(0, -1);
        }
        else if (name.Contains("back"))
        {
            lookDirection = new Vector2(0, 1);
        }
        else if (name.Contains("right"))
        {
            lookDirection = new Vector2(1, 0);
        }
        else if (name.Contains("left"))
        {
            lookDirection = new Vector2(-1, 0);
        }
    
        RaycastHit2D hit = Physics2D.Raycast(rigidbody2d.position + Vector2.up * 0.2f, lookDirection, 1.5f);
        if (hit.collider != null)
        {
            Hotspot hotspot = hit.collider.GetComponent <Hotspot>();
            KickStarter.playerInteraction.SetActiveHotspot (hotspot);
        }
    }
    

    How I can do that? With code?

    Code is possible, but I was suggesting animation. If your Hotspot Detector is a child of your Animator, you can animate its position within your regular character sprite animations, so that its position changes depending on which facing animation the character is playing.

  • I understood, thank you.

    I have another issue about NPC follow, although NPC follows the navmesh, NPC doesn't follow the navmesh holes. I have attached some screenshots about the NPC inspector and navmesh. As you can see in the last screenshot, I set three holes in the position of the chairs, but the NPC could cross them.

    https://drive.google.com/file/d/130p0X8I6fZGVHdaUW-LtxdFFs9_uTWAs/view?usp=sharing
    https://drive.google.com/file/d/1va_fkJn-8-6yArWqglxcqAo9zmV7nR24/view?usp=sharing
    https://drive.google.com/file/d/13AAKVjA_sCS5ay0tNt09lcnDGpIK9C_Q/view?usp=sharing
    https://drive.google.com/file/d/12r5SK6OLyfjDNug4-7GqspCBk1Tl9wgJ/view?usp=sharing

    I also have a question about the play speech. If I set 2 dialogue actions consecutively, the dialogue box will disappear when the first dialogue finishes and appear when the second dialogue starts, but I want the dialogue box to remain until all dialogue finish. Can this be done?

  • although NPC follows the navmesh, NPC doesn't follow the navmesh holes.

    Your screenshots don't show an NPC component, but does show a custom Enemy Controller script - are you still using the NPC component to move the NPC?

    I see also that their Circle Collider 2D has a non-zero offset - is this because their pivot point is not around their feet? For pathfinding to work correctly, this should be the case - a character's origin should be by their feet, with sprite pivots at this position to have them appear correctly on screen.

    Your NavMesh screenshot does not show the holes defined in the Inspector - is this because you're baking them? Try not baking, but also bear in mind that they must be fully inside the main NavMesh - the one on the right looks like it's poking out the side.

    If I set 2 dialogue actions consecutively, the dialogue box will disappear when the first dialogue finishes and appear when the second dialogue starts, but I want the dialogue box to remain until all dialogue finish.

    What is your AC version? I will also need to see images of your Speech Manager, the two speech Actions, and your Subtitles menu's properties.

  • edited May 2021

    are you still using the NPC component to move the NPC?

    The Enemy Controller script is only to detect the collision with the player and calls actionlist. I am still using the NPC component. The below link is the NPC component.

    https://drive.google.com/file/d/1zVqzfGq6qGtB9if9jbFYUuVOJYDyyQkX/view?usp=sharing

    is this because their pivot point is not around their feet?

    The pivot point is around the NPC feet. Circle Collider 2D has a non-zero offset is because of the size of the NPC image.

    you're baking them?

    Yes, and I tried not baking and set again the right holes inside the main NavMesh, but it still didn't work.

    What is your AC version? I will also need to see images of your Speech Manager, the two speech Actions, and your Subtitles menu's properties.

    1.73

    Speech Manager:
    https://drive.google.com/file/d/1UXBnxleNQtIoEYW_ysRzBdT1Lt5jsLzL/view?usp=sharing
    https://drive.google.com/file/d/16G0KtwoCG5Mq_71y561ZKj7WvV6Nxmyp/view?usp=sharing

    Subtitles menu's properties:
    https://drive.google.com/file/d/1uXyHpiWC3tHB5nK8wn8S541RpswuO2mY/view?usp=sharing
    https://drive.google.com/file/d/1KmxluOahnVz2GeZmuUYDb7Eevo0Jl_zF/view?usp=sharing
    https://drive.google.com/file/d/1wnO0HJuaHViTlO6PPKXJn4zxvGQkanFc/view?usp=sharing

    speech Actions:
    https://drive.google.com/file/d/159XM2RUPM21igr-PEAgqv03qZCx6BDqB/view?usp=sharing

  • The pivot point is around the NPC feet. Circle Collider 2D has a non-zero offset is because of the size of the NPC image.

    The image with the Inspector is too cropped to see which objects in the Scene view correspond to what. Which of the three circles is the NPC's root?

    How large is the NPC compared with the NavMesh?

    Subtitles menu's properties:

    The Duplicate for each line? property is enabled, which means that each speech line will generate its own instance of the menu. The first line will fade out while the second fades in. Try unchecking this option.

  • The image with the Inspector is too cropped to see which objects in the Scene view correspond to what. Which of the three circles is the NPC's root? How large is the NPC compared with the NavMesh?

    https://drive.google.com/file/d/1Hh2P3ZNch6HusCTqwFprnwE3WZKtIi4z/view?usp=sharing

    The circle of the feet is the NPC's collider. The other two are the audio source and audio clips.

    NPC is smaller than the size of NavMesh

    Try unchecking this option.

    Got it, thanks!

  • PM me your scene file and your NPC prefab - I'll take a look directly.

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.