Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Vallyrie · May 11, 2020 at 08:20 AM · 2d gameoverlapspawn pointsspawn-when-click

Spawning something if it is not within another object?

Hi everyone. I am creating a tower defense game in which the player can drag a tower from the purchase bar and then lift the left mouse button to spawn the chosen tower at the mouse position. I have all of that working, so now I am trying to prevent the tower from being spawned on top of other objects like other towers or the purchase bar. I know the fix is probably fairly simple with the Physics2D.overlap, but after following the "Bits&Bobs Unity Tutorial - Overlap Sphere" and looking through other forum posts, I still wasn't sure what to do to accomplish this. (My programming knowledge is very limited). Any help is appreciated!

Here is my code for the current spawn system:

public GameObject towerActive;

 void Start()
 {

 }

 void Update()
 {
     Vector2 cursorPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     transform.position = new Vector2(cursorPosition.x, cursorPosition.y);

     if (Input.GetMouseButtonUp(0))
     {
         Instantiate(towerActive, cursorPosition, Quaternion.identity);
         Destroy(gameObject);
     }
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ADiSiN · May 12, 2020 at 02:06 AM

Hi!

So, basically, what you want to do is check if you have any object at your mouse position when instantiating. For that I recommend to use Raycasting, but keep in mind that it only detect objects with Collider component - without it the object will not be detected.

Below an example how it works in 3D. You basically shoot ray from camera to mouse position and it returns true if hits something.

 RaycastHit hit;
 
 if(Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity))
     Debug.Log("We did hit something and we don't want to be able instantiate");

I am not too familiar with 2D, but the code below should gave similar behaviour and work properly.

 RaycastHit2D hit2D;
 
 Ray ray = cam.ScreenPointToRay(Input.mousePosition);
 hit2D = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity);
 
 if(hit2D.collider != null)
     Debug.Log("We did hit something and we don't want to be able instantiate");

The cam is the reference to the Camera: public Camera cam;

Let me know if it helps.


Here you can find additional information about Raycasting: https://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

Comment
Add comment · Show 2 · 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 Vallyrie · May 12, 2020 at 03:58 AM 0
Share

Hey, thanks for answering! I'll look into this and give it a try in the morning!

avatar image Vallyrie · May 12, 2020 at 03:01 PM 0
Share

This worked great, thank you! I had to use Camera.main ins$$anonymous$$d of a public Camera variable because the inspector wouldn't let me drag in the main camera for some reason. I also had to switch the != to == because it was giving me the opposite effect. Other than that, this was perfect. Also, thanks for the links. I've been intimidated by raycasts, but they're actually not that bad now that someone showed me how to write them correctly.

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

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

Can't get game objects to instantiate at random positions without overlap - please help! 3 Answers

2D Area of Effect Script - OverlapCircle not working 1 Answer

Physics2D.OverlapCircleAll returning colliders outside of the area 0 Answers

Create GameObject On Click Not Working 0 Answers

Random objects created and flying through scene 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