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 Grady · Jun 28, 2011 at 04:52 AM · guiraycastingdistancedetectionrange

How to perform some code if you are close to an object

Hey guys, I am trying to make a script that when you are close to an object, it prints some GUI to the screen.

But what I am having trouble with is figuring a way to check if you are in a certain range of an object..... I know you can do this somehow with raycasting, but I'm not sure how to do it.....

Any help would be greatly appreciated!!!!

-Grady

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
1

Answer by crazyKnight · Jun 28, 2011 at 05:09 AM

the most easy way to approach this will be to make a cube around the object(the one on which you need to print a gui)make it a trigger the size of the cube will depend upon the range you want .

then apply the following code on the cube

 function OnTriggerEnter(info : Collider)
 {
      if(info.gameObject.tag == "your object tag")
     {
          // do your required work 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 Grady · Jun 28, 2011 at 05:58 AM 0
Share

yeah, but how would i execute the code when i'm a certain distance away from an object?????

avatar image Graham-Dunnett ♦♦ · Jun 28, 2011 at 09:40 AM 0
Share

Suppose you want to execute the code when you are 100 units away from the object. In that case make the cube 200 units wide/tall/whatever. Or use a sphere with a radius of 100 units.

avatar image Graham-Dunnett ♦♦ · Jun 28, 2011 at 09:42 AM 0
Share

Or, if you don't like that approach, you have two vectors, the position of the object in the scene and the position of your player. Vector3.Distance() will tell you the distance between these two positions, and then you can do your code if it's less than some value in units.

avatar image
0

Answer by Sirex · Jun 28, 2011 at 12:01 PM

This is a modified library function that i have written for these things. Call it from an update function in some script and it should work. Hope this is around what you were looking for!

 def FindObjectWithTag(newPosition as Vector3, tag as string, objectRadius as double):
                 ColliderList = Physics.OverlapSphere(newPosition, objectRadius)    
                 resultObject as duck = null
                 for listObject in ColliderList:
                     if listObject.tag == tag:
                          return true
                          //resultObject = listObject 
                 //return resultObject.gameObject
                 return false
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 demize2010 · Jun 28, 2011 at 12:07 PM 0
Share

I'd recommend looking at SphereCastAll() and testing against returned coliders for their tags. You should get better performance from this I think.

avatar image demize2010 · Jun 28, 2011 at 12:08 PM 0
Share

Sorry the link is: http://unity3d.com/support/documentation/ScriptReference/Physics.SphereCastAll.html

avatar image Grady · Jun 28, 2011 at 11:14 PM 0
Share

so it would send out a sphere that when it collides with anything, then you can get it to run some code.. I also had a raycasting script which i was using but it works, but i'm not sure if anyone would be able to improve it... here it is:

var rayCastLength = 100000;

function Update () {

 var hit : RaycastHit;
 
 if(Physics.Raycast(transform.position, transform.forward, hit, rayCastLength))
 {
 if(hit.collider.gameObject == "GA$$anonymous$$EOBJECTTAG")
 {
     Application.LoadLevel(0);
 }
 }

}

I hope you can read it with out it being colour coded.... thanks

-Grady

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

6 People are following this question.

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

Related Questions

What is the most efficient way of detecting an object? Using raycast, rect (screen space) or utilizing a capsule collider? 1 Answer

Raycasts and new Gui buttons 0 Answers

Detect if player is in range? 2 Answers

IDragHandler goes through buttons 0 Answers

How to get a game object to lerp as a triggered effect? 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