- Home /
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.
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.
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!
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.