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. Dismiss Notice

OnMouseDown within certain distance?

Discussion in 'Scripting' started by radler470, Jan 16, 2015.

  1. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    Right now I am using OnMouseDown() to make some things happen, but is there a way to do this where the user (first person controller) has to be within a certain distance in order for this to work?

    I am still very new to C# and to Unity, so if you could be as specific as possible, it would be greatly appreciated. Thank you!
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Code (csharp):
    1.  
    2. if ((transform.position - player.position).magnitude < requiredDistance)
    3. {
    4.     DoGreatThings();
    5. }
    6.  
     
    parviz likes this.
  3. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    Super new to this, so bear with me. It's not recognizing a "player" name, I imagine that's probably an issue with the way I have my FPC set up?

    Also, for required distance - what is that based on? I just put in an arbitrary number, but I'm not sure how the scale works.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class Serial1 : MonoBehaviour {
    4.     [SerializeField]
    5.     private SerialDoer Dd;
    6.    
    7.     void OnMouseDown () {
    8.         if ((transform.position - player.position).magnitude < 3) {
    9.                         Dd.Switch (true);
    10.                 }
    11.         }
    12.    
    13. }
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You need to declare player somewhere. I don't know what you're using so I just left it ambiguous.

    Distance is measured in "units" which can be whatever you want them to be (within limitations of the physics engine). The typical starting point (and the one we still use) is one unit is one meter. So your code would fire if the player was 3 meters from the thing.
     
    radler470 likes this.
  5. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    What would that look like in the code?
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I have no idea what you're using or what you're trying to do honestly, so I can't answer that.
     
  7. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    I have a First Person Controller, and the script works when you click on the object where the script is applied, I'm just not sure how to call it in the script. So sorry, I'm a total dolt when it comes to scripting. >.<
     
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    There are a lot of different ways. The easiest one (if not the most efficient) is to create a variable called player and assign it via the Inspector

    Code (csharp):
    1.  
    2. public class Serial1 : MonoBehaviour
    3. {
    4.     [SerializeField()]
    5.     Transform player;
    6.  
    7.     void OnMouseDown() {.....}
    8. }
    9.  
     
    radler470 likes this.
  9. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    Got it working now, thank you so much for your help!