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

[SOLVED] onmousedown distance

Discussion in 'Scripting' started by AmbrielUwU, Aug 19, 2016.

  1. AmbrielUwU

    AmbrielUwU

    Joined:
    Aug 14, 2016
    Posts:
    18
    I have decided to just simplify this based on a huge lack of information anywhere regarding what I wanted, and I am using void onmousedown instead so you just click it (which isnt gonna help me when I try and make this work on gamepad but whatever, I cant help myself on this one...)

    SO NOW, New problem. I need some way of making it so that I have to be close to an object for this to work instead of just simply pointing at it.

    CODE:


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class RockClickDescription : MonoBehaviour {
    6.     public AudioClip whatisthis;
    7.     public AudioSource au_description;
    8.     public float distance;
    9.  
    10.  
    11.  
    12.     void Start() {
    13.        
    14.         au_description = (AudioSource) gameObject.AddComponent<AudioSource>();
    15.         AudioClip whatisthis;
    16.         whatisthis = (AudioClip)Resources.Load("voices/whatisthis");
    17.         au_description.clip = whatisthis;
    18.         au_description.loop = false;
    19.     }
    20.  
    21.     void Update ()
    22.     {
    23.     }
    24.     void OnMouseDown() {
    25.        
    26.                 au_description.Play ();
    27.  
    28.             }
    29.         }
    30.    
     
    Last edited: Aug 20, 2016
  2. AmbrielUwU

    AmbrielUwU

    Joined:
    Aug 14, 2016
    Posts:
    18
    bumping because of title change, sorry, I am about 9 hours into this and had to substitute doing this as a keypress for using onmousedown which is already problematic and just need to get some sort of distance going on here, the closer the better. And so far nowhere can I find anything less then incredibly vague information which doesnt help me. I need some sort of direction for this.
     
  3. lloydsummers

    lloydsummers

    Joined:
    May 17, 2013
    Posts:
    343
    There is a lazy way,

    Warning, untested code. But assuming you have a GameObject named Player, and you want to find the distance between it and the press...

    Code (CSharp):
    1. void OnMouseDown() {
    2.   if (CheckAssignPlayer()) {
    3.     if (Math.Abs(Vector3.distance(this.transform.position, playerObject.position)) > distance)) {
    4.       au_description.Play();
    5.     }
    6.     else
    7.     {
    8.       // Couldn't reach it
    9.     }
    10.   }
    11.   else
    12.   {
    13.     // Player is destroyed, maybe the dude/dudette died and the game object is missing
    14.   }
    15. }
    16.  
    17. private Transform playerObject;
    18. private float distance = 1f;
    19.  
    20. // Check if playerObject is null, and if so, find by name, tag or whatever you want to do.
    21. bool CheckAssignPlayer() {
    22.   if (playerObject == null)
    23.     playerObject = GameObject.Find("Player").transform;
    24.   return (playerObject != null);
    25. }
    26.  
     
    Last edited: Aug 20, 2016
    Kiwasi likes this.
  4. AmbrielUwU

    AmbrielUwU

    Joined:
    Aug 14, 2016
    Posts:
    18
    I'm just using FPS controller and as my main camera, there is no physical object that it is attached to. also I have this code attached to the individual object which triggers the individual sound.
     
  5. lloydsummers

    lloydsummers

    Joined:
    May 17, 2013
    Posts:
    343
    Thats OK, you don't need a physical object.

    Everything from empty gameobjects, to cameras, has a transform (which has a world position). The object this script is attached to, triggers when that object is pressed, and also has a transform.

    So, you can use Vector3.distance(obj1.transform.position, obj2.transform.position) to find out how far apart two transforms are in terms of world space distance.

    You just need to decide on the two points of reference.
     
    Kiwasi likes this.
  6. lloydsummers

    lloydsummers

    Joined:
    May 17, 2013
    Posts:
    343
    Here, just use this...

    Code (CSharp):
    1. void OnMouseDown() {
    2.   if (Math.Abs(Vector3.distance(this,transform.position, Camera.main.transform.position)) > 1f) {
    3.       au_description.Play();
    4.     }
    5. }
     
  7. AmbrielUwU

    AmbrielUwU

    Joined:
    Aug 14, 2016
    Posts:
    18
    haha I appreciate the help, but it changed the code to
    Code (CSharp):
    1. if (Mathf.Abs(Vector3.Distance(this,transform.position, Camera.main.transform.position)) < 1f) {
    2.                 au_description.Play ();
    3.  
    and then had told me No overload for method Distance takes 3 arguments
     
  8. AmbrielUwU

    AmbrielUwU

    Joined:
    Aug 14, 2016
    Posts:
    18
    I changed the comma after this to a period and it resolved the error, but in game nothing happens no matter how close or how far.

    This is a pretty annoying little venture I am on, I would just move on, but eventually I am going to have to click on a door and have it open when I am not miles away from it.
     
  9. AmbrielUwU

    AmbrielUwU

    Joined:
    Aug 14, 2016
    Posts:
    18
    Okay I messed up with the > I had it backwords, sorry. Now it works. It sucks sometimes because I learn through application, so without any usable tutorials it is hard to put the pieces together when they are this foreign to me.

    Now I am hoping that I could use this same idea to open doors and otherwise interact with objects through the game, it is a little annoying having a single input have multiple functions, but before I was trying to have all this work via kepress and that just wasn't happening. SO thank you a lot for helping me out with this, being about a week into code and the engine itself makes things a little rough somtimes.
     
  10. lloydsummers

    lloydsummers

    Joined:
    May 17, 2013
    Posts:
    343
    No sweat! We all start at the beginning.