Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Proximity script

Discussion in 'Scripting' started by tattyswad, May 21, 2012.

  1. tattyswad

    tattyswad

    Joined:
    Sep 22, 2010
    Posts:
    106
    Hi All,

    I have the following script which works but it only fires once. If i start the camera close up and move away it fires but then when I move close again it doesn't fire. If I start the camera far away and move close it fires but then when I move away it doesn't fire again. I thought the 'function Update' would mean that it ran continuously.

    Can anyone explain this?


    Code (csharp):
    1. var cam : Transform;
    2.  
    3. function Update () {
    4.  
    5.     var dist = Vector3.Distance(cam.position, transform.position);
    6.    
    7.     if (dist<50){
    8.         Debug.Log ("Close up");  
    9.         }
    10.        
    11.     if (dist>50){
    12.         Debug.Log ("Far away");
    13.     }
    14. }
    Many thanks
     
  2. Poya

    Poya

    Joined:
    Jul 3, 2011
    Posts:
    51
    Is this script attached to your camera? Also what's the position in cam?
     
  3. tattyswad

    tattyswad

    Joined:
    Sep 22, 2010
    Posts:
    106
    Hi,

    This script is attached to a box which will hopefully detect when the camera is within range. The cam variable is the main camera in the scene and the cam.position is the position of the camera. Like I say it works once but then seems to become inactive once it has fired.

    I would prefer to leave the script on the object as I will eventually have a lot of objects that will need this script on them which will be triggered when the camera is within range.

    thanks
     
  4. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    Pretty weird, assuming that this is the only script on the boxes. Or are there any other scripts?
     
  5. Zethariel1

    Zethariel1

    Joined:
    Mar 21, 2012
    Posts:
    439
    The only logical thing that could disable a script would be when a component or gameobject goes inactive or is destroyed. Make sure that does not happen. This code is too simple not to work :S
     
  6. tattyswad

    tattyswad

    Joined:
    Sep 22, 2010
    Posts:
    106
    That is the only script in the whole scene! I set up a new scene just to sort out this script. I tried it with OnTriggerEnter too and the same thing happened. It would fire once and then nothing.

    Strange!
     
  7. Poya

    Poya

    Joined:
    Jul 3, 2011
    Posts:
    51
    Something about that cam variable doesn't seem right...just as an experiment try using Camera.mainCamera.transform maybe?
     
  8. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    I guess, it works, but "Collapse" button in Console window is pressed, so you don't see the repeating messages.
     
  9. tattyswad

    tattyswad

    Joined:
    Sep 22, 2010
    Posts:
    106
    alexzzzz!!!!! You're a genius!

    Either that or I'm an idiot..... probably both.

    Thanks everybody.
     
  10. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    Maybe you could also put in some more debug code to print the cam position, transform position and distance to see how values change over time. Also specify the type of the dist variable, just to make sure.

    edit: alexzzzz seems to have the most applicable explanation for the phenomenon ;)