Search Unity

Spawning a object on touch position when area is free.

Discussion in 'Scripting' started by irjason, Mar 5, 2015.

  1. irjason

    irjason

    Joined:
    Sep 15, 2012
    Posts:
    42
    Currently im trying to make it so i can Instatiate a object on the screen where i click on the screen that i have manage do do. Now im wondering how would i go about saying "If this object is in the way .. or if this gui button is being pressed don't create a object) some kind of collision in code that detects this. Its hard to explain but i hope you get what i mean.


    Code (CSharp):
    1. public void AddSquare(){
    2.         //Find the vector 3 of the postion in the world of the screen for finding mouse input location
    3.         Vector3 a = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x,Input.mousePosition.y,10));
    4.         //Creates a instantiate of the game prefab Square and applys the location from above to its location and sets rotation to default.
    5.         SquareInstance = (GameObject) Instantiate (Square, a, Quaternion.identity);
    6.         //Increment increase
    7.         increase++;
    8.         //add's the instance and increments each time by increase to the list
    9.         squares.Add (new Squares (increase, "Square" + " " +increase, SquareInstance));
    10.         //Sorts the items by there ID
    11.         squares.Sort ();
    12.      
    13.         foreach (Squares square in squares) {
    14.             //Prints a list in to the console of the ID and name of a object in list.
    15.             print ("ID: " + square.id + " Name: " + square.name);
    16.         }
    17.     }
    Code (CSharp):
    1.         if (Input.touchCount==1){
    2.  
    3. //Do some kinda of collision checking for gui buttons in the way or the ground is in the way? howww?
    4.  
    5.             //Excutes Add Square
    6.             AddSquare();
    7.         }
    Would it be somthing like

    Code (CSharp):
    1.         if (Input.touchCount==1){
    2.             if (!collider.CompareTag("GUI") && !collider.CompareTag ("Ground")){
    3.             //Excutes Add Square
    4.             AddSquare();
    5.             }
    And add the tags to the gui and ground?
     
    Last edited: Mar 5, 2015
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Look up Raycasts.
     
  3. honprovet

    honprovet

    Joined:
    Mar 4, 2014
    Posts:
    23
    Ive been thinking about how to check for colisions before placing buildings as well. So i'll use you post, if im allowed to.

    Doing 4 vertical top to bottom raycasts in the position of each corner of where the building would be placed, based on mouse position, and checking for map is a good solution? Is there a better way?
     
  4. irjason

    irjason

    Joined:
    Sep 15, 2012
    Posts:
    42
    I should of also mentioned it was a 2D game but will look in to raycasting now and try and understand how it works.
     
  5. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    How are you placing the objects and are they square?