Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by Chocolade · May 14, 2017 at 11:17 PM · scripting problemtriggerscript.mesh colliderenter

How can i detect by script when a character enter a hole in object ?

I have a cylinder with a hole. The character can enter the hole. But now i want to trigger something once the character is inside the hold inside the cylinder.

In the screenshot on the left it's the Hierarchy part of the Cylinder. Then the Cylinder part in the scene view. Then the cylinder inspector. And on the right the BezierCircle inspector.

Now the BezierCircle is the hole the character can walk in and out. And the BezierCircle is where i want to detect where the character enter/leave it. Not collide with it but enter it.

The BezierCircle have a collider component Mesh Collider but i unchecked it not to use it since if i will use this collider the character will not be able to enter the hole(BezierCircle).

The question is how by script i can detect when the character is inside/ouside the BezierCircle ?

Cylinder

cylinders006.jpg (272.6 kB)
Comment
Add comment · Show 1
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 RobAnthem · May 14, 2017 at 11:31 PM 1
Share

Use a trigger collider ins$$anonymous$$d of a normal collider, by checking the Is Trigger box.

Then your code will look like this

 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "Player")
     {
         Debug.Log("Player entered the hole");
     }
 }

You could probably use a capsule collider for it, but if you want to use your $$anonymous$$esh collider, you'll need to check the Convex box first.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Davirtuoso · May 16, 2017 at 08:28 AM

The simplest way I can see you doing this is to add a box collider to your object that fills the entrance to your circle in the area you want to detect. If you tick the 'is trigger' box on the collider, it will mean objects can pass through it freely, but you can use it to detect things instead.

If the player has a rigidbody attached (which if you're using the default third-person controller I believe it does), then you can use the 'OnTriggerEnter()' method to run code whenever something with a rigidbody enters the collider area. So for example:

 bool detected = false;
 
 OnTriggerEnter (Collider other)
 {
     if (other.name == "Player")
     {
         detected = true;
     }
 }

 OnTriggerExit (Collider other)
 {
     if (other.name == "Player")
     {
         detected = false;
     }
 }

This is a simple example of detecting something has entered the collider's range and doing something, and then doing something else when it leaves. In this case, OnTriggerEnter will run when an object enters, check what the name of the object is, and if it is Player then it will set detected to true. When an object leaves, OnTriggerExit runs and checks if the thing leaving is named Player. If it is, it sets detected to false.

Whatever script you write that in would then need to be attached to the object that has the collider attached to fill in the doorway. I hope that gives you a good idea of where to start to achieve what it is you need :-)

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

117 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

Related Questions

Why doesn't my NavMeshAgent go to a position on trigger? 1 Answer

Door open script opens all the doors in the scene 3 Answers

How can I return from the method array of Transforms but with or without make instance of the new gameobjects ? 0 Answers

How can I get the SerializedObject and FineProperty also the node tree childs of conversations ? 0 Answers

One Script Referred by Another Script used by Multiple Objects 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