• 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 basil4j · Sep 14, 2013 at 04:26 AM · c#

Best way to check distance between player and objects

Hi. I need a number of objects in each of my scenes to do something when the player is within a certain distance. Would it be best to attach a script to each object which checks the distance to the player, or attach a script to the player which loops and checks the distance to each object?

Target platform is android if it matters. Thanks in advance!

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 AndyMartin458 · Sep 14, 2013 at 04:29 PM 0
Share

It is about the same complexity because you will end up with the same number of distance checks with either method. $$anonymous$$aybe it's easier though to place a small script on each object to run this check. Otherwise the player will have to keep track of every object somehow.

1 Reply

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

Answer by clunk47 · Sep 14, 2013 at 05:40 AM

http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Distance.html

EXAMPLE:

 using UnityEngine;
 using System;
 using System.Collections;
 
 public class Example : MonoBehaviour 
 {
     [SerializeField]
     private float range = 10.0f;
 
     private Transform t;
     private Transform player;
 
     private void Awake()
     {
         t = this.transform;
         player = GameObject.FindGameObjectWithTag("Player").transform;
     }
 
     private void Update()
     {
         if (player)
             print(player.name + " is " + Distance().ToString() + " units from " + t.name);
 
         else
             print("Player not found!");
     }
 
     private float Distance()
     {
         return Vector3.Distance(t.position, player.position);
     }
 }
 
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 Christopher_Mtn · Jan 09, 2016 at 04:27 AM 0
Share

Wait, if I wanted to put something that I wanted my t object to do if the player was close, where would I put it? Sorry, I'm pretty new to program$$anonymous$$g!

avatar image MastersOfUs Christopher_Mtn · Jan 09, 2016 at 10:06 AM 0
Share

Put it in the object you want to perform an action.

avatar image Christopher_Mtn MastersOfUs · Jan 09, 2016 at 07:16 PM 0
Share

Ok yeah, I've put it on my plane object and I want it so that if the player is close to the plane they can press E to get in. So then, where in the script would I put if__ bool planeenter = true?

And then using that boolean I could check if they are close enough or not.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Flip over an object (smooth transition) 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer


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