Search Unity

How detect when gameobject in vector3

Discussion in 'Scripting' started by gevorg89, May 30, 2016.

  1. gevorg89

    gevorg89

    Joined:
    Mar 9, 2016
    Posts:
    1
    In server side I need detect when Player camera look at gameobject and distance to object less then 50 units
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    So when the camera is looking at the gameObject and the player is less than 50 units away?
    Well, you didn't exactly define, what looking at is:
    1. Camera is looking exactly at the gameObject, which is not obscured by anything
    a simple raycast will solve this one:
    Pseudo code
    Code (CSharp):
    1. hit : will be the hitInfo
    2. if (Raycast(cameraPos, cameraDirection, out hit, 50)) { //I might have messed up the order
    3.    if (hit.gameObject.name == yourGameObjectsName) {
    4.       //Do stuff
    5.    }
    6. }
    2. Camera is looking exactly at the gameObject, but it can be obscured by an other object
    same as before, but set it up, so it will ignore every layer, except for the one with the gameObject
    3. The gameObject is in the view of the camera, but it's not obscured
    a simple void OnBecameVisible (or something like that) should do the trick
    4. The gameObject is in the view of the camera, and it might be obscured by an other object
    this one might be the hardest, but it might be possible, by setting up a secondary camera, which only sees the object, and then calling the function I wrote down before, but I never tested this one out
     
    gevorg89 likes this.