• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Lolifius · Apr 30, 2014 at 08:35 AM · 2dcollider

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 !

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image

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++;
     }
 }


Comment
Pyrian

People who like this

1 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lolifius · May 01, 2014 at 05:34 PM 0
Share

Hi ! Thanks about this. How do you make the function overslap work in this 2d case? I tried without succes ...

avatar image

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.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

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 ...

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image korbul · May 02, 2014 at 11:47 AM 0
Share

I updated my answer above.

avatar image

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"

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Noob_Vulcan · May 02, 2014 at 12:20 PM 0
Share

there is 2d raycast also ..But i never used that so cant tell u abt that

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[Tilemap] Can you spawn a Prefab (with colliter) together with a tile when placing it? 0 Answers

Why to set maximum and minimum for transform.position of an object? 1 Answer

Problems detecting collisions while changing transform 0 Answers

How to make rigidbody bounce off the mouse? (2D) 2 Answers

Retrieve 2d primitives from 3d colliders 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges