Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a game object called game object 1 on screen. It holds an image called red . when i drag an image called "animal" on a game object 1 collision happening fine and destroys the "animal" in same layer. Then increment the score. But when i drag an image called "animal" on a game object 2 I want to decrement my score (with out collision). I know different layer collision is not happening. enter image description here

 void OnCollisionEnter2D(Collision2D col)
    {

            if(Input.GetMouseButtonUp(0))
            {
        if (col.gameObject.name == "Semi arid" )
        { 
            //obj1.rigidbody.isKinematic = false;
                scoreCalculate.currentScore+=10 ;

                Destroy(gameObject);
                //Debug.Log ("Scored1............"+scoreCalculate.currentScore);
                Instantiate(obj1, ObjectSpawnPosition1, Quaternion.identity);
                Instantiate(green, ObjectSpawnPosition1, Quaternion.identity);
                obj1.renderer.enabled = true;
                green.renderer.enabled = true;}
share|improve this question

2 Answers 2

Instead of collision detection, you can use raycasting. When an object starts to be dragged, perform raytest inside the Update by filtering unwanted objects.

RaycastHit info;
int layerMask = (1 << LayerMask.NameToLayer("GO1Layer") | 1<<LayerMask.NameToLayer("GO2Layer"));
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out info, 100f, layerMask))
{
    if (info.collider.name == "GO1")
        //statements
    else if (info.collider.name == "GO2")
        //statements
    else
        //statements
 }

Put this into a mouse listener script. Check whether an object is getting dragged and set the layermask to ignore the dragged object.

share|improve this answer
    
how ? when i drag this game object "animal" upon the "gameobject 2" how it decrement the score. –  Deepika C P Jul 31 '14 at 10:40
    
I meant always. Try sending raycasts from the mouse origin to the corresponding scene area while some object is dragged. In this way, you can filter whatever you want without colliding objects. –  Alican Jul 31 '14 at 10:42
    
can you provide me a sample code wht u meant? –  Deepika C P Jul 31 '14 at 10:56

To disable collision between specific layers you should go to in

Edit->Project Settings->Physics

and customize the collision matrix, which specifies for each layer combination if they can collide or not.

See documentation, hope it helps.

If it isn't happening yet, then reasons could be just because you use OnCollisionEnter2D instead of OnTriggerEnter2D() or because you move the transform instead of the Rigidbody2d.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.