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
2
Question by alexanderflink · Dec 25, 2013 at 08:11 PM · 2dcollisionrigidbodytriggerkinematic

Collision between two kinematic rigidbody triggers

Hi! I've scoured the forums for a solution to my problem, but to no avail. I'm making a 2D game where you place various physics driven objects. The player is supposed to not be able to place these objects in certain places. To achieve this, i've created empty game objects with kinematic rigidbodies and trigger colliders to mark the places where the player can't place the objects.

When the player drags the objects with the mouse, they are changed into kinematic rigidbodies to stop them from having physics, and their colliders are set to triggers, so that they won't affect any other objects until they are placed. However, this causes them to not respond to collision with the "Can't place here" triggers.

Any suggestions on how to fix this?

Here's the code i've written for the dragging and placing so far (JavaScript):

 #pragma strict
 private var tran: Transform;
 var isDragging: boolean = false;
 private var mousePos: Ray;
 private var canPlace: boolean = true;
 private var spriteRenderer: SpriteRenderer;
 function Start () {
     tran = transform;
     spriteRenderer = GetComponent(SpriteRenderer);
 }
 
 function Update () {
     if (isDragging) {
         //get mousePosition
         mousePos = Camera.main.ScreenPointToRay(Input.mousePosition);
         tran.position = Vector3(mousePos.origin.x,mousePos.origin.y,tran.position.z);
         //rotate object
         if (Input.GetButtonDown("RotateRight")) {
             tran.Rotate(Vector3(0,0,45));    
         } else if (Input.GetButtonDown("RotateLeft")) {
             tran.Rotate(Vector3(0,0,-45));
         }        
         //disable physics
         collider2D.isTrigger = true;
         rigidbody2D.isKinematic = true;
     } else {
         collider2D.isTrigger = false;
         rigidbody2D.isKinematic = false;
     }
 }
 function OnMouseDown () {
     if (canPlace) {
         isDragging = false;
     }
 }
 function OnTriggerStay2D (col: Collider2D) {
     if (isDragging) {
         if (spriteRenderer.color != Color.red) {
             spriteRenderer.color = Color.red;
         }
         canPlace = false;
     }
 }
 function OnTriggerExit2D () {
     Debug.Log("Exit");
     canPlace = true;
     spriteRenderer.color = Color.white;
 }

EDIT: The page on Colliders clearly state that this type of collision should yield a trigger event, as seen in the chart below. alt text

proof.png (28.7 kB)
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
Best Answer

Answer by iwaldrop · Dec 25, 2013 at 09:20 PM

The other answer, that's seems like more of a comment/counter-question, touched on what I'd do to solve the problem. You should have your 'can't place here' colliders on a seperate layer that everything interacts with, and another layer 'object placement' (or something more meaningful for your project) that only collides with that layer. Now, when you're moving your object, set it's layer to that 'object placement' layer, and don't set is kinematic or toggle to trigger. It may require fixing the rotation so that it doesn't start spinning any time it hits one of these 'can't place here' colliders.

Obviously, when you're finished with the placement phase, you should set the layer back to whatever it was to begin with, and allow rotation again.

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 alexanderflink · Dec 25, 2013 at 09:24 PM 0
Share

Yeah, I was thinking about the layer approach... It seemed a bit complicated, but I guess it's the only / right way to do it. Thanks!

avatar image
0

Answer by lukos1986 · Dec 25, 2013 at 08:25 PM

Might be silly but check your ProjectSettings/Physics Matrix if both object layers are checked there.

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 alexanderflink · Dec 25, 2013 at 08:28 PM 1
Share

Everything is checked in the physics matrix... :/

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

20 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

Related Questions

Collider2D/RigidBody2D not working 1 Answer

Collision between two moving objects (Kinematic)? 2 Answers

How to detect in grid of objects if one is destroyed while beside adjacent objects without rigidbody? 1 Answer

CPU cost of using multiple rigidbodies 0 Answers

2D motion/collision: physics, or direct translation? 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