• 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 Kamiyama · Oct 06, 2013 at 02:27 AM · sensornot-workingnear

Why won't my character register as near?

 #pragma strict
 var player : Transform;
 var distance = 7;
 private var playerCloseEnough = false;
 
 function Start () {
 
 }
 
 function Update () {
 if(Vector3.Distance(transform.position, player.position) < distance){
         playerCloseEnough = true;
     }
     else{
         playerCloseEnough = false;
 }
 if (playerCloseEnough == true){
         print("It works!");
         }
 else
         {
             print("It doesn't work!:(");
             }
 }

This is attached to a cube that is supposed to detect when the player is near. So far It doesn't register as I always get "It doesn't work!". Any idea's?

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
0

Answer by TrickyHandz · Oct 06, 2013 at 02:37 AM

Your script is written fine and it works. I just tried it out in a new project with a cube and a sphere, where the script was on the cube and the sphere assign as the player. As soon as I moved my sphere inside the range it worked as expected.

I would recommend changing your "It doesn't work" code to printing the distance so you can see how far away you actually are. Like this:

 print(Vector3.Distance(transform.position, player.position));
Comment
Add comment · Show 4 · 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 Kamiyama · Oct 06, 2013 at 02:44 AM 0
Share

When I try that- it looks as if Unity takes the distance from when the game starts and then doesn't update and continue checking the distance. It's very odd. I'm kinda at a loss.

avatar image TrickyHandz · Oct 06, 2013 at 02:48 AM 0
Share

That is rather strange. As I said your code works right out of the box for me. Try it in a new empty scene to see if something else is causing an issue. If it works in a new scene with just two objects the culprit is elsewhere.

avatar image Kamiyama · Oct 06, 2013 at 02:51 AM 0
Share

Actually I take that back. I started the player in a different spot and it gave me the same number...86.9916. So that's a problem. I'll try an empty scene and hopefully it'll work.

avatar image Kamiyama · Oct 06, 2013 at 03:00 AM 0
Share

So I did try it and it this point- it still doesn't work for me. This time it gives me a number in the 70's. Really at a loss here. I'm not sure what could be causing the problem. EDIT: So apparently I have to drag the player controller from the hierarchy for it to work. So it's solved then.

avatar image
0

Answer by darkhog · Oct 06, 2013 at 03:23 AM

Why so much ifs? First of all, you could simplify first if as

 playerCloseEnough = (Vector3.Distance(transform.position, player.position) < distance

But even that's not needed, as you can just do:

 if ((Vector3.Distance(transform.position, player.position) < distance){
        print("It works!");
        }
 else
        {
          print("It doesn't work!:(");
          }
 }

This way you'll avoid unnecessary ifs which may make your game run faster.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

17 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

Related Questions

Move Objects which are far away. 0 Answers

Using PhidgetInterfaceKit 8/8/8 with unity3d. 0 Answers

Humanoid with custom motion (like exercise) 0 Answers

UNITY Scripting - True and False not working! 3 Answers

Compiler errors when opening unity 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges