Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Collider to match sprite shape

Discussion in '2D' started by imperatormk, Feb 22, 2017.

  1. imperatormk

    imperatormk

    Joined:
    Feb 22, 2017
    Posts:
    2
    Hey. I have a spritesheet like attached. What I want to do is create a collider that would automatically change it's size and shape to perfectly fit the frame of the animation that is currently being shown (I am using an animator and .anim file constructed from the separate sprites in the image).

    Thanks!
     

    Attached Files:

  2. kvnchua

    kvnchua

    Joined:
    Apr 24, 2015
    Posts:
    6
    why not use polygonCollider2D? but you cant edit the size of polygon collider in the animation though
     
    imperatormk likes this.
  3. imperatormk

    imperatormk

    Joined:
    Feb 22, 2017
    Posts:
    2
    Hey, that worked perfectly well. Thanks a lot!

    Just in case someone else has the same question: if you were previously using BoxCollider and now switch to PolygonCollider2D, and are using Raycast, here is a working example:

    Code (CSharp):
    1. if (Input.GetMouseButton(0))
    2.     {
    3.         RaycastHit2D hit;
    4.         hit = Physics2D.Raycast(new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint (Input.mousePosition).y), Vector2.zero, 0);
    5.         if (hit.transform == null)
    6.             return;
    7.         if (hit.transform.gameObject.name != item.name)
    8.             return;
    9.  
    10.  
    11.         print (hit.transform.gameObject.name);
    12.     }