Forum rules - please read before posting.

Hotspot start button for mobile

edited June 2020 in Technical Q&A

Hi @ChrisIceBox
Sorry to ask too many questions. Need help again.
Video of the problem: https://streamable.com/dc0r0v
Hot spots occasionally overlap with the joystick and hot spot interaction comes into play. To deal with this problem, I tried to make a button that appears on the screen when the player approaches the hot spot. So hot spots will only work when the hot spot interaction button is pressed. But I couldn't do it exactly.

I encountered a topic like this in the forum : https://adventurecreator.org/forum/discussion/7440/hotspot-use-button-on-gui

The codes you wrote have been deleted by the site:
Link: https://pasteall.org/938123/csharp

Can you help me?

Thanks in advance...

Comments

  • edited June 2020

    I don't have the old script to hand, but this should do it:

    using UnityEngine;
    using UnityEngine.UI;
    using AC;
    
    public class AutoSelectCentreHotspot : MonoBehaviour
    {
    
        public float rayLength = 3f;
        public UnityEngine.UI.Button button;
        private Hotspot hitHotspot = null;
    
        void Update ()
        {
            if (KickStarter.stateHandler.IsInGameplay ())
            {
                Ray ray = Camera.main.ScreenPointToRay (new Vector2 (ACScreen.width / 2f, ACScreen.height / 2f));
                RaycastHit hit;
    
                if (Physics.Raycast (ray, out hit, rayLength, 1 << LayerMask.NameToLayer (KickStarter.settingsManager.hotspotLayer)))
                {
                    Hotspot rayHotspot = hit.collider.gameObject.GetComponent <Hotspot>();
                    if (rayHotspot != null)
                    {
                        hitHotspot = rayHotspot;
                        KickStarter.playerInteraction.SetActiveHotspot (rayHotspot);
                        button.gameObject.SetActive (true);
                    }
                    else
                    {
                        KickStarter.playerInteraction.DeselectHotspot ();
                        button.gameObject.SetActive (false);
                    }
                }
            }
            else
            {
                button.gameObject.SetActive (false);
            }
        }
    
        public void UseInteraction ()
        {
            hitHotspot.RunUseInteraction ();
        }
    
    }
    

    Set your Interaction method to Custom Script, and make a Unity UI to store the "use" Button. Don't link this to AC, but instead attach the script above to the Canvas root, and assign the Button field in it's Inspector. Then set the Button's OnClick event to this component's "UseInteraction" function.

    The Button should then show/hide automatically when a Hotspot is in the centre of the screen, and clicking/tapping it should then run the interaction.

  • Hi @ChrisIceBox
    I did everything you said. But I got an error in the codes.
    Picture One: https://imgur.com/EZatZII

    I solved the error I received with the suggestion of visual studio.
    Picture Two: https://imgur.com/72NOHyM

    But the code doesn't work.
    Picture Three: https://imgur.com/l8Oa2dA

    Picture Four: https://imgur.com/I6Yzi44

    Picture Five: https://imgur.com/ny19UO8

  • In the OnClick event box, you need to first drag in the Canvas root (where the component is), and then find the component in its hierarchy - not assign the script file itself.

  • Sorry, it's my fault.

    Aside from the Button conflict, it was also disabling the root canvas - not the Button itself.

    I've updated the post above with the corrected code - give it a try now.

  • Apologies again - I think I removed something when editing last time.

    Updated once more. The "Hotspot detection method" will need to be kept as "Custom Script" as well, but this is working on my end.

  • Works great except for a minor problem.

    The "use" button does not disappear after moving away from the hot spot. He works once again while away from the hot spot.

    Video: https://streamable.com/n0xlp4

    Also, when we turn our back to the hot spot, the "use" button does not disappear.

    Video 2: https://streamable.com/q2mxzm

    Sorry for bothering you...

  • It's no bother. Try this:

    using UnityEngine;
    using UnityEngine.UI;
    using AC;
    
    public class AutoSelectCentreHotspot : MonoBehaviour
    {
    
        public float rayLength = 3f;
        public UnityEngine.UI.Button button;
        private Hotspot hitHotspot = null;
    
        void Update ()
        {
            if (KickStarter.stateHandler.IsInGameplay ())
            {
                Ray ray = Camera.main.ScreenPointToRay (new Vector2 (ACScreen.width / 2f, ACScreen.height / 2f));
                RaycastHit hit;
    
                if (Physics.Raycast (ray, out hit, rayLength, 1 << LayerMask.NameToLayer (KickStarter.settingsManager.hotspotLayer)))
                {
                    Hotspot rayHotspot = hit.collider.gameObject.GetComponent <Hotspot>();
                    if (rayHotspot != null)
                    {
                        hitHotspot = rayHotspot;
                        KickStarter.playerInteraction.SetActiveHotspot (rayHotspot);
                        button.gameObject.SetActive (true);
                    }
                    else
                    {
                        KickStarter.playerInteraction.DeselectHotspot ();
                        button.gameObject.SetActive (false);
                    }
                }
                else
                {
                    button.gameObject.SetActive (false);
                }
            }
            else
            {
                button.gameObject.SetActive (false);
            }
        }
    
        public void UseInteraction ()
        {
            hitHotspot.RunUseInteraction ();
        }
    
    }
    
  • It works very well. I am grateful to you for your help @ChrisIceBox

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.