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
1
Question by xSpectrum · Sep 21, 2011 at 10:36 PM · c#positioncubenear

Checki f Player is within area? C#

I want to have a cube in my world. When the player is near the cube, I want to display some text instructing the player what to do. The only way that I can seem to accomplish this is by:

     void OnGUI()
     {
 if(GameObject.FindWithTag("Player").transform.position.x <= someAmountNearCube && GameObject.FindWithTag("Player").transform.position.x >= someAmount)
 {
 //Draw Text Here
 }}
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by aldonaletto · Sep 21, 2011 at 10:49 PM

You have several ways to do that. You can check the distance from the cube and do whatever you want when the player is below certain distance, or you can create a cubic trigger and use the OnTriggerEnter to fire your message. Anyway, using FindWithTag (or other Find flavors) is by far the worse - it takes an appreciable amount of time to check all objects, thus you'd better to use any Find routine only at Start (the docs recommend this).
The cubic trigger has the better resolution, since it has the same shape as the cube, and is fast. Just change the cube collider dimensions to reach the range you want and set isTrigger to tell it's a trigger, not a regular collider. You can then add a code like this to the cube script:

bool guiOn = false;

void OnTriggerEnter(Collider obj){ // turn message on when player is inside the trigger if (obj.tag == "Player") guiOn = true; }

void OnTriggerExit(Collider obj){ // turn message off when player left the trigger if (obj.tag == "Player") guiOn = false; }

void OnGUI(){ if (guiOn){ // only show message if guiOn is true // draw text here } }

Comment
Add comment · Show 3 · 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 xSpectrum · Sep 21, 2011 at 11:30 PM 0
Share

Thank you, but how would I tell Unity that I want the "OnTriggerEnter" to be linked with my cube?

avatar image syclamoth · Sep 21, 2011 at 11:45 PM 1
Share

It says in the post- OnTriggerEnter gets called if you set your collider to be a trigger! That's what happens when you set a collider to be a trigger.

avatar image aldonaletto · Sep 22, 2011 at 02:22 AM 0
Share

This script must be assigned to the cube: when the trigger is entered, the scripts attached to the trigger object will have their OnTriggerxxx functions called.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Help! How to change Real numbers to Natural numbers on Distance Counter script (Noob question) 0 Answers

How to Maintain Y Position? C# 1 Answer

addition of vector3 1 Answer

Positioning a GameObject so I't wont be inside another object 1 Answer

Distribute terrain in zones 3 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