Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
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
Add comment
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
1

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
Add comment · 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
0

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
Add comment · 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
0

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
Add comment · 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
0

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
Add comment · 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.

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

Physics raycast hit offset from where it should be 1 Answer

OnTriggerStay2D Breaks When Adding AnimationController 1 Answer

How Can I switch between 2 colliders in my 2D Game? 1 Answer

Sprite does not rotate with a CircleCollider2D 1 Answer

Jumping from a floor to another floor (2D) 2 Answers


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