Forum rules - please read before posting.

Swap "Menu Position Type" at runtime.

edited December 2020 in Technical Q&A

Hello Chris,
I am trying to solve a problem related to the player occluding a hotspot menu.

This is my scenario:

The player walks to an interactable hotspot, the hotspot icon appears at the hotspot centre (or at the Centre Point Override),
but the character is positioned right in front of it, so the hotspot menu is not entirely visible (or not at all).

When this happens, I would like the hotspot menu to switch to "Above Player".

To do so, I could cast a ray between the menu and the camera as the menu is spawned (using an Action List) and see if the player is occluding it). In that case, I would like to switch the menu Position Type to "Above Player", however, this doesn't seems possible as the menu appear type is only set once on Awake.

Another issue would be to switch back to "Above Hotspot" once the player is not occluding the menu anymore, which makes me think I could cast a ray continuously (at a certain interval to save performance) when a given hotspot is active.

So, long story short, how can I switch to a different Menu Position Type at runtime?

Thanks!

Comments

  • Update, I have found a custom action that does something similar to what I need.

    https://adventure-creator.fandom.com/wiki/Change_a_menu's_"Appear_type"_at_runtime

    I have modified it to change the PositionType and it works as expected.

    So my next question would be, how is the "above player" position calculated?
    Can it be overridden or should I instead use a Manual Position Type?

    What I want to do is move the menu to the side rather than above the player, to prevent the menu being too close to the top edge of the screen.

    Ideally, the menu should move to the closest position to the hotspot centre (or centre override), while sliding around the character's sprite renderer, while the character is inside the hotspot.

  • If you're using custom scripting to determine the intended position, it'd be best to rely on event hooks rather than Actions. The OnMenuTurnOn event is triggered whenever the menu is turned on, where you can perform your raycast as well as change the Position type in one go:

    using UnityEngine;
    using AC;
    
    public class RespositionMenuExample : MonoBehaviour
    {
    
        void OnEnable () { EventManager.OnMenuTurnOn += OnMenuTurnOn; }
        void OnDisable() { EventManager.OnMenuTurnOn -= OnMenuTurnOn; }
    
        void OnMenuTurnOn (Menu menu, bool isInstant)
        {
            if (menu.title == "MyMenu")
            {
                // Determine correct position type, and then:
                menu.positionType = AC_PositionType.AbovePlayer; // If using AC
                menu.uiPositionType = UIPositionType.AbovePlayer; // If using Unity UI
            }
        }
    
    }
    

    The "Appear type" can be similarly changed, but this is different from "Position type". "Appear type" determines when a Menu turns on. Also note that Unity UI menus rely on uiPositionType, rather than positionType, for their "Position type" setting.

    By default, the "Above Player" position setting is calculated by working out where the top of the Player's collider is - which is typically a safe approximation of where their head lies. You can, however, override this by assigning a Speech menu placement child Transform in their Inspector.

    If you want full control over the Menu's position, though, setting it to Manual may be best. In this mode, you can position a Menu to a specific set of screen-space co-ordinates with the SetCentre function.

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.