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.