Forum rules - please read before posting.

Multiple Hotspots on same object don't work as expected (first-person mode, contect sensitive)

Hi,

I have a prefab that includes a script to open/close a door so I added a hotspot on the door which works.
However, as the interaction source is not per interaction but per hotspot I cannot create an examine interaction that is not a script action of the use action script.

What I want to do:

  • use = call script method
  • examine = show dialog

Therefore I added a second hotspot for the dialog. But nothing happens on right-click. When I disable the first "use" hotspot the second hotspot should actually lead to the label being shown, but it's not.

I found out that this happens with any object. Interaction method is set to "context sensitive" as I want to use the default interactions: left click = use, right click = examine therefore I don't want to have a specific menu for it.

Any help? Didn't find a solution here.

Comments

  • edited December 2021

    Welcome to the community, @tricoos.

    When Hotspot detection relies on raycasting (as is the case with the default "Mouse Over" option), then each Hotspot must be associated with a unique Collider. I'd advise combining your two separate Hotspots into a single Hotspot again.

    To get around the "Interaction Source" issue, there are two workarounds:

    1) Assign an ActionList for the "Use" interaction that contains either the Object: Send message or Object: Call event Actions to then call your custom script function

    2) Have your custom script hook into the OnHotspotInteract custom event to have it trigger its own function internally when the Player uses the Hotspot. The Hotspot does not need to have an ActionList assigned for this to work. Since you're already relying on a custom script, I'd recommend this approach.

    Custom events are a means to run custom code when AC performs a common task, such as interacting with a Hotspot, or showing a line of speech. A tutorial on their usage can be found here, but here's an example that you should be able to incorporate into your existing script:

    using UnityEngine;
    using AC;
    
    public class HotspotEventExample : MonoBehaviour
    {
    
        private void OnEnable () { EventManager.OnHotspotInteract += OnHotspotInteract; }
        private void OnDisable () { EventManager.OnHotspotInteract -= OnHotspotInteract; }
    
        private void OnHotspotInteract (Hotspot hotspot, AC.Button button)
        {
            if (hotspot == GetComponent<Hotspot> () && hotspot.GetButtonInteractionType (button) == HotspotInteractionType.Use)
            {
                OnUse ();
            }
        }
    
        private void OnUse ()
        {
            // Custom script method
            Debug.Log ("The Hotspot " + this + " had its Use interaction run", this);
        }
    
    }
    

    Attaching this to the Hotspot in the prefab will cause the OnUse function to be run whenever the Hotspot's Use interaction is triggered.

  • edited December 2021

    Thanks, Chris!

    I tried the second suggestion, but it doesn't work. Actually I have come so far as that Visual Studio does not show any errors for the script and neither does the Unity console but Unity still complains that that script is not valid.

    I simply added a second script to the door object and used your script above and added a single line:

    gameObject.GetComponent<LC_Door>().InteractWithThisDoor();

    The script is called LC_Door and the method is public - it's the same method I previously called directly through the Unity UI.

    private void OnUse()
        {
            // Custom script method
            Debug.Log("The Hotspot " + this + " had its Use interaction run", this);
    
            gameObject.GetComponent<LC_Door>().InteractWithThisDoor();
        }
    

    Or am I doing something wrong? I found the above solution on how to call another script method online - maybe it's wrong? Still not getting why Unity complains but the console does not show any error related to the script.

    Just to make it clear: I did your your complete script above, but only added the one line.

  • The change you've made looks fine to me, so far as the function call is valid. In what way it Unity complaining, if not a console error? Did you name this new script file HotspotEventExample.cs?

    The original script without your addition should log a message in the Console - have you tried running it without your change to see if this shows?

  • Thanks, Chris, that worked so far! The script name was wrong.

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.