Question about triggers

Hey guys, i am making a puzzle game like portal to my friend’s website (we are making multiple games to post). I have a scene in which there is a chest with some spheres, and 3 rooms. I want the player to use the drag function to pickup and drag one sphere to each room. Each room has a box collider that is a trigger. I want to use a script that will detect if the player has put a sphere in each room, and then open a door after it. How can i do that? I can’t see any way to do this actually and i know it’s simple… Could someone help me? I would be very grateful for that.

I’m seeing two questions here, how to implement a drag function and how to detect object in trigger colliers.
I’m not sure about the input of your game, but assuming its top down and mouse controlled, you could change the spheres velocity to be equal to that of the vector between the mouse and the sphere (so sphere rigid body.velocity = mouse.position - sphere.position) and control when u let go based on the mouse input.

As for detecting a when a sphere has been placed in a room, you can use the OnTriggerEnter() function or OnTriggerStay() function to return when an object has interacted with the trigger. If there’s other objects in the scene that can be moved you could use OnTriggerEnter(Collider ) and run an if statement to check the tag of that collider.
If its the sphere, you could send a message or change a variable in another scrip which manages all the rooms to detect when all sphere are inside.

Example Code

Script 1 - Trigger Script

public LevelManagerScript levelManagerScript
void OnTriggerEnter(Collider object)
{
if(object.transform.tag == "sphere")
levelManagerScript.AddSphereCount();
}

Script 2 - LevelManagerScript

public int sphereCount = 0;
public void AddSphereCount()
{
sphereCount++;
if (sphereCount == 3)
//  win level
}

Hope this helped.

Hello!

Working with triggers is actually very easy. I suppose that for each trigger you have an empty gameobject with a collider attached to it. The first step is to add a script to every one of those gameobjects, so 3 in your case. (3 different scripts)

Inside the class of that script, we’ll need to check whether or not an object has entered the trigger zone. We can achieve this by using OnTriggerEnter and OnTriggerStay. First we have to add a script onto our door gameobject, I suggest you use this asset I created. And we’ll need to declare some variables in this door script:

    public bool1;
    public bool2;
    public bool3;

Then we move on to the actual 3 triggerscripts, note that you’ll have to use bool1, bool2 and bool3 for triggerscript 1,2 and 3 respectively. Also add the empty gameobjects that you use as triggerboxes as children to your door gameobject because the underlying code depends on that.

Door door = transform.parent.gameObject.GetComponent<Door>();

void OnTriggerEnter(Collider other)
      {
        if(other.tag == player)
        {
          door.bool1 = true;
        }
    
      }
    
      void OnTriggerStay(Collider other)
      {
        if(other.tag == player)
        {
          door.bool1 = true;
        }
      }

So now we have a unique triggerscript on every trigger gameobject that set the Booleans bool1, bool2 and bool3 to true in the door script of their parent when there is an object colliding with them. Lastly we’ll have to do a bit of code in the door script:

 if(bool1 == true && bool2 == true && bool3 = true)
   {
     Open();
   }

This is a simplistic version of what you have to do but hopefully it helps you a bit :slight_smile: