- Home /
Detect inside Collision 2D
Dear All,
My Name is Adit, this is my first time i create a game, and i decide to use unity,
i want to create a simple puzzle games, the main idea is there is a puzzle pieces and there is a coloumn puzzle and user need to drag the puzzle pieces into the right puzzle coloumn
after wasting much time finally i can drag my puzzle pieces, but i don't know how to detect collision inside my puzzle coloumn
here i attach my source
private var screenPoint: Vector3;
private var offset: Vector3;
private var curScreenPoint : Vector3;
private var curPosition : Vector3;
private var state : String;
private var mousePosition : Vector3;
private var v : Vector2;
private var col : Collider2D[];
var otherScale: Vector3;
function Start () {
}
function Update () {
var DragPiece : GameObject;
if(state == "STATE_DRAG_PUZZLE_PIECE"){
}
}
function OnMouseDown () {
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
Screen.showCursor = false;
state = "STATE_DRAG_PUZZLE_DOWN";
}
function OnMouseDrag() {
curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
state = "STATE_DRAG_PUZZLE_PIECE";
}
function OnMouseUp(){
Screen.showCursor = true;
state = "STATE_DRAG_PUZZLE_UP";
}
function OnTriggerEnter(col:Collider){
otherScale = col.transform.lossyScale;
print("scale tetangga :"+otherScale);
}
function OnCollisionEnter2D(coll: Collision2D)
{
if(coll.gameObject.tag.Equals("kentang_kolom")) {
Destroy(coll.gameObject);
print("nabrak kolom kentang");
}
}
any idea or comment would be very helpfull for me thanks, best regards,
aditya eka putra (@abang_adit)
did u add rigidbody to your gameobject.if it is not added u cant detect the collisions.
hey mohanrao164, thanks for comment i did, i add a rigidbody2d and box collider2d to my gameobject, can u help me how to detect collision inside my puzzle coloumn ?
If you puzzle is in a grid format you can simply use a little bit of maths to calculate which tile a click is on.
in the box collider there is istrigger if it is active your collision will not work .can u check that once if it is active from the above code ontrigger event is called else if not active on collisionenter2d is called
replace if statement like this if (coll.gameObject.tag == "kentang_kolom")
Your answer
![](https://koobas.hobune.stream/wayback/20220613130243im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Dragging one object, but when it overlaps another it sticks also 0 Answers
How can a character not move out of box collider's bounds? 1 Answer
Top Down 2d (PC): Gameobjects' Colliders intermeshing 1 Answer
Remove Drag/Friction 1 Answer
How do I prevent characters from being able to land on each others heads? 1 Answer