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 19, 2013 at 12:44 AM · collisioninstantiatetriggeroncollisionenterkinematic

Collision between two instances of the same object

Hello! I'm creating a building game where the player should not be able to place buildings on top of each other. Each time a new building is selected and placed, it is instantiated only if there is no building already there.

In order to do this, I attached a BoxCollider & RigidBody component to the building prefabs, and written a script that uses OnCollisionEnter to check for collisions (also attached to the prefabs). I've read through much documentation and forum posts, I simply cannot get the collisions working. Please take a look at my screenshots & code, if you can find where I'm making a mistake I'd be eternally grateful.

1. Screenshot of the farm prefab in the inspector.

2. BuildingCollisions.cs - Checks if any collisions occur (if a building already exists in that spot)

 public class BuildingCollisions : MonoBehaviour{
     void Update(){
         collisionDetection();
     }
     
     public bool collisionDetection(){
         RaycastHit hit;
         if (Physics.Raycast(transform.position, -Vector3.up, out hit, 3)){
             if (hit.collider.gameObject.CompareTag("Building")){
                 Debug.Log("A building is already there!");
                 return true;
             }else{
                 return false;
             }
         }else{
             return false;
         }
     }
 }

3. Farm.cs - Instantiates a farm (only relevant function shown)

     void moveFarm(){
         // The position of the mouse in screen coordinates
         Vector3 screenPos = Input.mousePosition;
         screenPos.z = 225;
         // The position of the mouse in world coordinates
         Vector3 worldPos = myCamera.ScreenToWorldPoint(screenPos);
         
         // Update the farm's position
         if (farmPlacep == false){
             farm.transform.position = worldPos;
         }
         
         // When the user left clicks...
         if (Input.GetMouseButtonDown(0)){
             // Instantiate BuildingCollisions class
             BuildingCollisions getBuildingCollisions;
             getBuildingCollisions = gameObject.AddComponent("BuildingCollisions") as BuildingCollisions;
             Debug.Log("Building already there?: " + getBuildingCollisions.collisionDetection());

          // Only if a building collision isn't occurring, build the farm
             if (getBuildingCollisions.collisionDetection() == false){
                 Instantiate(farm, worldPos, Quaternion.identity);
                 farmPlacep = true;
                 farmSelectedp = false;
             }
         }
     }
Comment
Add comment · Show 9
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 prototype7 · Mar 19, 2013 at 01:36 AM 0
Share

Do you have CharacterController on your player or rigidbody ? if the player is a CharacterController, try use OnControllerColliderHit. OnCollision event are triggered when a rigidbody hits a CharacterController.

avatar image murkantilism · Mar 19, 2013 at 01:42 AM 0
Share

No, there's no player object or character controller. This is a SimCity type game, so the player is placing buildings from a birds-eye view, or "God mode" view.

So wait, OnCollisionEnter & OnCollisionStay are only triggered when one of the objects is a CharacterController?

avatar image robertbu · Mar 19, 2013 at 03:38 AM 1
Share

Given that this is a non-moving game, rather than colliders, use raycasts. You can raycast to the position you are about to place the building and take a look at the hit.

avatar image Tarlius · Mar 19, 2013 at 06:22 AM 1
Share

I think the problem is that you are checking for the result before the physics update. (And if you don't have a rigidbody, that will also be a problem)

@robertbu has the answer. Just raycast to check first, or maintain a separate list of what is in each square

avatar image Tarlius · Mar 19, 2013 at 02:10 PM 1
Share

Yes. They happen between Update()s (depending on framerate, you might not get one between 2 Updates, or you might get many)

But you don't yield to allow a FixedUpdate() to occur between instantiating and checking. I may be wrong, but I believe that will not allow the OnCollissionEnter to run until after you've checked

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by whydoidoit · Mar 19, 2013 at 01:53 PM

Rigidbodies that don't move often go to sleep, which can be a problem. But I'd have thought your underlying requirement is that at a moment in time you want to check if something is at a particular location - you can do that with:

  • Raycast - if your building are laid out on a grid you'd only need one

  • CheckSphere

  • OverlapShere

Comment
Add comment · Show 8 · 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 murkantilism · Mar 19, 2013 at 03:17 PM 0
Share

Ok, thank you very much. I'll be implementing Raycasting later today and see if that works.

avatar image murkantilism · Mar 21, 2013 at 01:20 AM 0
Share

Hello, so I implemented Raycasting and there are still problems. See the original post for the updated code.

The output:

When I place a farm, and then hover over the built farm with another farm the console prints "A building is already there!" as it supposed to. Unfortunately, it still lets me place it on top of the first farm.

Additionally, even though it prints the Building Already There warning, it also prints "Building already there?: false" when it should be true.

avatar image whydoidoit · Mar 21, 2013 at 07:55 AM 0
Share

A raycast starting within a collider won't hit it. Perhaps you should be doing -Vector3.up * 100 and making you raycast 100 long?

avatar image murkantilism · Mar 23, 2013 at 01:18 AM 0
Share

Been wicked busy lately, finally got to try out

 Physics.Raycast(transform.position, -Vector3.up * 100, out hit, 100)

It didn't change anything, same problem. Prints "A building is already there!" but still lets me place it. scratches head any ideas?

avatar image whydoidoit · Mar 23, 2013 at 11:51 AM 0
Share

That should be:

   Physics.Raycast(transform.position -Vector3.up * 100, Vector3.down, out hit, 100)

Perhaps you could edit the question and post the code again if that doesn't help?

Show more comments

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

13 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

Related Questions

Collision between two instances of the same object 1 Answer

Making a turret fire another arrow if an enemy survives the hit 3 Answers

Lightswitch help? 3 Answers

OnCollisionEnter not triggering when two rigidbody collide via Instantiate 1 Answer

How do i destroy only 1 object when 2 instances of the same object collide ? 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