Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Bryangs · Dec 03, 2016 at 10:58 PM · c#script.triggersdooropen

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. alt text

alt text

example1.png (15.3 kB)
example2.png (12.6 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by alexanderameye · Dec 04, 2016 at 12:01 AM

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 :)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bryangs · Dec 04, 2016 at 02:41 AM 0
Share

IT WOR$$anonymous$$ED PERFECTLY, THAN$$anonymous$$S!!! $$anonymous$$y only problem was that the Door door = transform.parent.gameObject.GetComponent<Door>(); didn't worked, but i changed it.

I can't express how grateful i am! Thank you kindly =)

avatar image alexanderameye · Dec 04, 2016 at 01:52 PM 0
Share

Glad I could help! If you do use my asset, a review would be greatly appreciated and extremely useful :) Have a nice day, and good luck with your game/website!

avatar image
0

Answer by Paricus · Dec 05, 2016 at 02:14 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

248 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to make a door. Zoning or Load Level with Animation. 1 Answer

Open/Close Door only if player is near the Door 1 Answer

Health Script 2 Answers

i need help jump script c# 1 Answer

Why cant I access the same function twice? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges