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
1
Question by Moohasha · Jan 22, 2012 at 10:05 PM · raycastcollision detectionspherecastintersection

Advice for Detecting Intersections With Object Being Placed

I'm working on a tower defense game and rather than limiting the player to only placing towers on specific, predefined spots, I want to let him place them anywhere on the ground that is open. However, towers should not intersect each other or other world objects such as buildings or walls.

I'm wondering what the best way to implement this would be. I have a script on my camera that does a ray cast every frame to get the point on the terrain under the cursor, that's simple enough. Now I want to know if the selected tower type will fit there, or if there is another obstacle nearby.

Should I attach a script to the tower that's being placed on the ground, and in that script have an OnCollisionEnter() callback that simply tells the camera that the tower has collided with something and can't be placed there? Or is there a way I can do a "cube cast", kind of like how I can do a Physics.Raycast or Physics.SphereCast and handle the collision detection in the same script on the camera that I check for the ground point under the cursor?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by KrisCadle · Jan 23, 2012 at 12:54 AM

then use the collider on your prefab to disable placement ability and activate your red outline... you can have any function you want with a trigger on your prefab - plus adjust the size of the collider so it gives you the right buffer zone etc...

 function OnTriggerEnter (other : Collider) {
     Destroy(other.gameObject);
     //or whatever else you want to do
 }
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 KrisCadle · Jan 22, 2012 at 10:54 PM

if your prefab has a collider then maybe you could just have it destroy itself if it is touching anything like a building or tree etc (use tags)... It would look like nothing happened but you can instantiate a GUI label (for a couple seconds) that says "try again" etc...

That way you could only place a tower when it is not touching anything else. Also the only code you have would be running on your prefab and not running in your update on your camera...

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 Moohasha · Jan 22, 2012 at 11:36 PM 0
Share

Hey $$anonymous$$ris, thanks for the suggestion. I don't want to destroy the object if it intersects something else, just highlight it red or something to show that it can't be placed there. The object will follow the mouse cursor until it is placed in a valid position, or the user hits Escape to cancel placing it.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Using Trigonometry for Collision Detection 0 Answers

Problem in spherecast . 1 Answer

Test If Two Colliders Overlap 0 Answers

Prevent explosion through walls 0 Answers

How many ways to stop player from going through walls? 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