Search Unity

Detecting two objects in same place

Discussion in 'Scripting' started by xKoyaanisqatsi, Sep 23, 2018.

  1. xKoyaanisqatsi

    xKoyaanisqatsi

    Joined:
    Jun 11, 2018
    Posts:
    2
    Hi!
    I need an idea how to create detecting script to let me know if both gameobjects i have are in same area.
    I know i can do it by using Colliders, but the point is one of the gameobjects is 2D and one is 3D.
    I need to check if player(2dObject) is in same place as car(3d) object. If it is i want play death animation.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    You can use Vector3.Distance() to compare the positions of the objects each frame, when they get close enough, call whatever function(s) you want.
     
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,433
    Usually this means that one of the objects is in screen space and the other object is in world space, or some other similar difference of coordinate systems. One common trick is to simply have a non-rendering object which represents one of the objects in the other coordinate space. For example, wherever the 3D car moves, calculate where that is in the 2D screen space, and move the invisible "2D car in screen space" object to that spot in the 2D world. Call it a proxy. Then you can attach colliders which can interact appropriately, or measure distances in the same coordinate space easily.
     
    DaemonicDreams likes this.