- Home /
Collider2D.IsTouchingLayers() delayed reaction
I have a simple Player prefab and a simple Item prefab that both have a Collider2D which should block the Player's movement.
Currently: Player has a CircleCollider2D (isTrigger = false) and a RigidBody2D (isKinematic = true)
Update() {
//...
Move(x, y)
}
Move(float x, float y) {
//...
//store current position
currentPosition = transform.position
//calculate new position
newPosition = new Vector3(x, y, 0);
//move object to newPosition
transform.position = newPosition;
//Check for collisions
if (gameObject.GetComponent<CircleCollider2D>().IsTouchingLayers(layer)){//"layer" is set in Unity Editor
print("Collision");
//If collision, undo move
transform.position = currentPosition;
}
}
Item has a CircleCollider2D (isTrigger = true)
Problem is, When the Player moves to a point where the Collider2Ds of each object overlap it does not pass the above IsTouchingLayers() check. However, when I move the player back to the original position where the Collider2Ds now no longer overlap, the above check is true. It seems that the IsTouchingLayers() check is delayed by a period of time/frame/etc.
I have edited the Update() function to include the same IsTouchingLayers() check, which is successfully true while the two Collider2Ds are overlapping, but I really need and want the check to be done before the player movement actually takes place.
Any assistance greatly appreciated! Thanks
Answer by Tepei · Jun 07, 2015 at 11:49 AM
You could use Physics2D.OverlapCircle
But maybe OnTriggerEnter2D will trigger faster..
Are you sure it's not the print function that delay something ?