Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Check if object exists at a certain position

Discussion in 'Scripting' started by landshark, Dec 6, 2012.

  1. landshark

    landshark

    Joined:
    Dec 6, 2012
    Posts:
    2
    hi i am incredibly new to Unity and dont have much experience with java script Im trying to write a code that checks if an object exists below another object. but i do not know a way of checking weather the space directly below is free or not.

    function Update () {

    var TerrainPrefab1:Transform;
    if(||| insert some function i dont know that checks if an object exists at a certain spot||||. != true) {
    var create = Instantiate(TerrainPrefab1,myPosition.position + Vector3(0,-1,0),Quaternion.identity );
    }

    you get the idea, please help
     
  2. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    Try a raycast. raycast can return all kinds of info with RaycastHit.collider. If you don't know much about them, there are a few examples in the doc. It's better than sending a collider to do the job cause sometimes colliders and triggers are funny about running into things instead of things running into them.
     
  3. landshark

    landshark

    Joined:
    Dec 6, 2012
    Posts:
    2
    Thank you very much
    i used Physics.Raycast and it worked beutifully

    function Update () {
    if(Physics.Raycast(transform.position,Vector3(0,-1,0),1) != true) {
    var create = Instantiate(TerrainPrefab1,transform.position + Vector3(0,-1,0),Quaternion.identity );
    }

    works like a charm
     
    Procy2 likes this.