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 murkantilism · Mar 23, 2013 at 01:27 AM · collisioninstantiatetriggercollision detectionkinematic

Collision between two instances of the same object

Hello! I'm writing code for a SimCity-type game. I have classes for each building you can place, and a script that detects if collisions between buildings occur. If a collision occurs, it prevents the user from placing the building (because buildings can't go on top of each other!).

I'm having trouble getting this collision script working though, could someone give me some advice on how to write the logic for this script and how to implement it?

To clarify, I'm not tryin to get someone to do the work for me >_> I already wrote the script and posted a question on UA, but wasn't able to fix the code I had. The issues I was having are detailed there, but I'm looking to just start over from scratch with some fresh advice/guidance.

Thank you kindly!

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

Answer by sparkzbarca · Mar 23, 2013 at 09:35 AM

not too hard

create empty addcomponent collider istrigger = true

keep this object ALWAYS ON just move it around with the mouse its going to be a clone of the movemnt of the building when you spawn it and allow movement, except it always exists and always moves

canspawnstuff = true

ontriggerenter

canspawnstuff = false

ontriggerstay

canspawnstuff = false

ontriggerexit

canspawnnstuff = true

make sure you have the ontriggerstray you dont want to have 2 buildings inside your spawn radius

you move one out it makes it possible to spawn stuff but there is still an object int ehre this way you reset to false if another object is still in there

Comment
Add comment · Show 5 · 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 sparkzbarca · Mar 23, 2013 at 09:36 AM 0
Share

basically you'll have an empty collider roa$$anonymous$$g around checking for collisions (you can resize colliders, just do a collider = spawnobject.collider for different buildings)

but sinces its an empty game object and colliders are invisible its never seen

avatar image murkantilism · Mar 24, 2013 at 10:18 PM 0
Share

Hmmm, I like this idea in theory, but I think there might be a problem. When the user goes to build a building, the game object is instantiated at the mouse position, and follows the mouse around until the user clicks.

So if I have the collision detector roams around with the mouse, once the user selects a building to build, it will constantly collide with the detector.

avatar image murkantilism · Mar 24, 2013 at 10:21 PM 0
Share

Although, I guess I could use layers to avoid this problem. Have "Built" and "Unbuilt" layers, with buildings being in the Unbuilt layer by default, and moved to Built once the user clicks, and the detector could check only in the Built layer.

Thanks for the advice! I'll give this a try.

avatar image sparkzbarca · Mar 25, 2013 at 12:51 PM 0
Share

the object shouldnt spawn initially with the collider on

go into the prefab. turn the collider off.

on mouse click spawn the building at the location

and now since its actually in game turn it on.

Problem solved :)

mark as answerd

avatar image murkantilism · Mar 25, 2013 at 01:12 PM 0
Share

No, that still doesn't solve the problem. Only turning the collider on when a mouse click occurs will detect collisions for less than a second.

Unless you meant turning the collider on permanently On$$anonymous$$ouseClick, which would make me unable to place a second building unless I did an OnTriggerExit call. Which is a pretty convoluted way of doing it; I tried the solution you helped me come up with (using OnTriggerStay/Enter/Exit and checking which Layer the building is in). It took some debugging but I got it to work perfectly.

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

11 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

Related Questions

Collision between two instances of the same object 1 Answer

How to detect different parts in sprite? 2 Answers

Kinematic RigidBody does not collide with Wall using MovePosition 2 Answers

How do I use colliders and/or triggers to end the game? 1 Answer

Colliding with multiple objects 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