• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by abang_adit · Jan 13, 2014 at 10:12 AM · 2dcolliderdragpuzzle

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)

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mohanrao164 · Jan 13, 2014 at 10:25 AM 0
Share

did u add rigidbody to your gameobject.if it is not added u cant detect the collisions.

avatar image abang_adit · Jan 13, 2014 at 10:28 AM 0
Share

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 ?

avatar image Benproductions1 · Jan 13, 2014 at 10:34 AM 0
Share

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.

avatar image mohanrao164 · Jan 13, 2014 at 10:35 AM 0
Share

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

avatar image mohanrao164 · Jan 13, 2014 at 10:39 AM 0
Share

replace if statement like this if (coll.gameObject.tag == "kentang_kolom")

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Dragging one object, but when it overlaps another it sticks also 0 Answers

How to slow down a Sphere smoothly in a world controlled by force and stop it? 2 Answers

the player is flying off the ramp (collider) 1 Answer

Drag object by Touch quickly 1 Answer

Collision with renderer.enabled? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges