Forum rules - please read before posting.

Enabling/Disabling hotspot on contact with another hotspot

Hello!

So I have a bunch of cupboards that can all be opened (all of them have a hotspot to interact with).
But there's an enemy that will block some doors by being in front of it. And I want the cupboard hotspot to be disabled as long as the enemy hotspot is overlapping with it.
The player will hit the enemy so it moves away to another cupboard.

To take an example :

There are 6 cupboards

Enemy is in front of cupboard 6. So cupboard from 1 to 5 are interactible but not the 6th one.

Player needs to access number 6. Player interacts with Enemy. Enemy moves to cupboard 5.

Cupboard 6 is now interactible but not cupboard 5 anymore.

So is there an easy way to do this? Maybe a way to check if the hotspot (or the attached object) is overlapping with the cupboards hotspot (or object) and enable/disable said hotspot depending on the result;

Because in the end I'd like to add multiple enemies and the idea I have right now would work with one ennemy, but would be an absolute mess with more

Thank you!

Comments

  • Would this be for a 2D or 3D scene? As it's a visual issue, any screenshot you can share of the scene will help explain the situation.

    If you're using "Mouse Over" Hotspot detection, when two Colliders overlap, the one closest to the camera. One option would be to resize the enemy's collider so that they cover the Hotspot of the cupboard behind them.

    Otherwise, you're probably best off relying on a custom script that brute-forces the enabled state by comparing distance to a fixed position, i.e.:

    using UnityEngine;
    
    public class CupboardEnemy : MonoBehaviour
    {
    
        public Transform[] enemies;
        public float distanceThreshold = 1f;
        public AC.Hotspot hotspot;
    
        void Update ()
        {
            if (AreEnemiesNear ())
            {
                hotspot.TurnOff ();
            }
            else
            {
                hotspot.TurnOn ();
            }
        }
    
    
        bool AreEnemiesNear ()
        {
            foreach (Transform enemy in enemies)
            {
                if ((enemy.position - transform.position).sqrMagnitude < distanceThreshold)
                {
                    return true;
                }
            }
            return false;
        }
    
    }
    
  • Thank you!

    I'm in 2D, and offsetting the Enemy hotspot worked well! I think I'll stick to that right now, But I might come back to this code, because I think it'll fit pretty well with the minigame I have in mind for this!
    Thanks a lot!

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.