Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 JoshMBeyer · Jan 28, 2014 at 11:12 PM · gameobjectsmultiplehowrangedetect

How to detect range of multiple gameobjects?

I am trying to create a script to where when the user comes within range of any of the objects, then the user may press "Spacebar" to open. I've searched for answers to similar questions but they don't help. Mostly the code is just "Here it works trust me" so I can't fully understand any of it because there is random spawn code and all that in the middle of what i need. And its not even in C#. Anyway. I need help with this, i've searched the references, answers, and just can't find what I am looking for.

Concept: Player is within range, now we can do something(Place code there).

MyScript:

 using UnityEngine;
 using System.Collections;
 
 public class TriggerDetection : MonoBehaviour
 {
 
 //--------------------------
     //Player's Position.
     public Transform player;
     
     //Set the range.
     int range = 20;
 //--------------------------
 
     void Update()
     {
         //If the player comes within range of us.
         if (Vector3.Distance(transform.position, player) < range)
         {
             //Do something
             Debug.DrawLine(transform.position, player, Color.red);
         }
     }
 }
Comment
Add comment · Show 2
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 JoshMBeyer · Jan 29, 2014 at 02:00 AM 0
Share

Well I was wanting this script to be attatched to the item, and have an option in the inspector to drag on the player. For example if I want to add a chest in game, drag the player into a slot on the inspector and then if the player goes close to that chest he will be able to open it. This setup would make it easy to add in other trigger items such as doors, gates, portals, anything like that you know

avatar image JoshMBeyer · Jan 29, 2014 at 02:09 AM 0
Share

Never$$anonymous$$d, I am an idiot. OnTriggerEnter is exactly what I was trying to do lol. I been up way too long obviously. Thank you

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by getyour411 · Jan 28, 2014 at 11:17 PM

Before you go too far down this road, why aren't you using OnTriggerEnter?

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

18 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

Related Questions

How to detect range? 2 Answers

How to use a script on multiple gameobjects, then change a variable for one of them, not the other. 3 Answers

How would I have a volume bar in multiple places? 1 Answer

Checking if multiple GameObjects do what one has to.... 1 Answer

Problem with multiple gameobjects using same script. 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