- Home /
2D Function analyzing if there is a Gameobject where you click ?
Hi everybody !
I'm making a little game with objects appearing when I click. I have this code actually making things work : everytime I use the mouseclick a Gameobject is created where I click on the 2d game I created.
//Changed to an array of objects assign as many as you have in inspector
var boxes : GameObject[];
var boxCounter : int;
function Update()
{
if(Input.GetMouseButtonDown(0))//Checks to see if left mouse button was clicked.
{
CreateBox();
}
}
function CreateBox()
{
var mousePos : Vector2 = Input.mousePosition;
//Depth you want the center of the object to be is z which I used zero
var boxPos : Vector3 = camera.ScreenToWorldPoint(mousePos.x, mousePos.y, 0);
//I used the perfab box's rotation here but you can enter what you'd like as a euler using Quaternion.Euler(x,y,z)
Instantiate(boxes[boxCounter], boxPos, boxes[boxCounter].transform.rotation);
//This will increment if there are more boxes or reset to 0 if it is the last one.
if(boxCounter == boxes.length-1)
{
boxCounter = 0;
}else{
boxCounter ++;
}
}
I want to add a function to avoid the creation of the object if there is already another one where I clicked. I don't know if I can do a simple function like If onclick there is a gameobject nothing happened; or if I have to do something like when I click create a gameobject, if collision cancel it, if not go for the Createbox function. I don't know exactly how to do that, any idea?
Thanks in advance !
Answer by korbul · Apr 30, 2014 at 09:40 AM
I suggest using Physics.OverlapSphere with the OnClick method.
This way you can set the radius to decide on how far from other boxes you can place the new box.
EDIT:
I noticed you are using unity 2d, so a better function would be Physics2D.OverlapCircle.
Bellow is a tested example of how I made this work.
var boxes: GameObject[];
var boxCounter: int;
function Start() {
boxCounter = 0;
}
// Update is called once per frame
function Update() {
if (Input.GetMouseButtonDown(0)) //Checks to see if left mouse button was clicked.
{
CreateBox();
}
}
function CreateBox() {
var mousePos: Vector2 = Input.mousePosition;
//I used 10 for the depth here because camera is at -10. This means all boxes will be created at Z = 0
var boxPos: Vector3 = Camera.main.ScreenToWorldPoint(Vector3(mousePos.x, mousePos.y, 10));
//OverlapCircle helps us know if there is a collider in a specific circle
//So we overlap a circle where we want to place the new box
var collider = Physics2D.OverlapCircle(Vector2(boxPos.x, boxPos.y), 0.5f);
//If no other colliders (boxes) exist at the new position, create our new box
if (collider == null) {
Instantiate(boxes[boxCounter], boxPos, boxes[boxCounter].transform.rotation);
}
//This will increment if there are more boxes or reset to 0 if it is the last one.
if (boxCounter == boxes.length - 1) {
boxCounter = 0;
} else {
boxCounter++;
}
}
Hi ! Thanks about this. How do you make the function overslap work in this 2d case? I tried without succes ...
Answer by KiraSensei · Apr 30, 2014 at 08:38 AM
I would suggest you to handle the problem like this :
OnClick -> make a raycast (see doc)
if the raycast hits a collider (this supposes that your created game objects have one), do nothing
else you create your game object.
Instantiate and destroy are far more heavier than a ray cast.
Answer by Lolifius · May 01, 2014 at 02:38 PM
Hi ! Thanks about this. How do you make the function overslap work in tis case? I tried without succes ...
Answer by Noob_Vulcan · May 02, 2014 at 12:20 PM
if (Input.GetMouseButtonDown(0)) { //when user touches the screen
RaycastHit hit = new RaycastHit ();
if (Physics.Raycast (ray, out hit)) {
//if it is a box
if (hit.transform.CompareTag ("Box")) {
//then do nothing }
else
CreateBox()
}
}
Just put this code in your update function ..and you are good to go
Note* Set you box tag as "Box"
there is 2d raycast also ..But i never used that so cant tell u abt that