Search Unity

How to make unique collider from box collider?

Discussion in 'Scripting' started by Slyrfecso1, Sep 24, 2015.

  1. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    Hi,

    I'd like to know how can I change a box collider of game object to unique shape collider.
    It is very important, because I can stop the pass through with collider.
    On the picture with yellow colour you can see the default box collider and
    with blue what I need.

    Any help will be helpful.


     
  2. Michael-N3rkmind

    Michael-N3rkmind

    Joined:
    Aug 16, 2015
    Posts:
    24
    What about using a MeshCollider instead? :)
     
  3. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Compound colliders are your friend. Just add multiple box colliders. Or make a simplified mesh and use it as a mesh collider.
     
  4. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    Thanks, nice ideas. I have tried compound collider plugin and it is good solution, when the object has one mesh.
    The best way for me is mesh collider. I have the all 3D models, than I can make simply mesh for these.
    I attached one and you can see it on the picture. The problem is, if I use convex and trigger, then the mesh is changing to box.
    How can I detect mesh collider maybe with collision enter?




    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. class drag_mouse : MonoBehaviour
    5. {
    6.     private bool Triggered = false;
    7.     private bool dragging = false;
    8.     private Vector3 distance;
    9.     private    Vector3    scanPos;
    10.  
    11.  
    12.    
    13.    
    14.     void OnMouseDown()
    15.     {
    16.         distance = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y,Camera.main.WorldToScreenPoint(transform.position).z)) - transform.position;
    17.  
    18.         dragging = true;
    19.  
    20.  
    21.  
    22.     }
    23.    
    24.     void OnMouseUp()
    25.     {
    26.         dragging = false;
    27.  
    28.         if (Triggered)
    29.         {
    30.             transform.position = scanPos;
    31.             Debug.Log(name + " Vissza az érintkezési ponthoz");
    32.            
    33.         }
    34.  
    35.  
    36.     }
    37.    
    38.     void Update()
    39.     {
    40.  
    41.             gameObject.name = gameObject.name.Replace("(Clone)","").Trim();
    42.         if (dragging)
    43.         {
    44.  
    45.             if(Input.GetKeyDown(KeyCode.A))
    46.                 transform.Rotate(0, 90, 0);
    47.            
    48.             if(Input.GetKeyDown(KeyCode.C))
    49.                 Destroy (gameObject);
    50.            
    51.             if(Input.GetKeyDown(KeyCode.D))
    52.                 Instantiate (gameObject);
    53.                
    54.  
    55.  
    56.             Vector3 distance_to_screen = Camera.main.WorldToScreenPoint(transform.position);
    57.             Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen.z ));
    58.             transform.position = new Vector3( pos_move.x - distance.x , transform.position.y, pos_move.z -distance.z );
    59.  
    60.  
    61.  
    62.  
    63.         }
    64.     }
    65.    
    66.  
    67.     void OnTriggerEnter (Collider myTrigger)
    68.     {
    69.         if(myTrigger.gameObject.tag == "Player" || myTrigger.gameObject.tag == "Falak")
    70.         {
    71.             dragging = false;
    72.             scanPos = gameObject.transform.position;
    73.             Triggered = true;
    74.             Debug.Log(name + " Triggered");
    75.  
    76.         }
    77.     }
    78.    
    79.    
    80.     void OnTriggerExit (Collider myTrigger)
    81.     {
    82.         if(myTrigger.gameObject.tag == "Player" || myTrigger.gameObject.tag == "Falak")
    83.         {
    84.             Debug.Log(name + " Snap feloldása Drag alatt");
    85.             Triggered = false;
    86.         }
    87.     }
    88.    
    89.  
    90.  
    91.     // This is idea for mesh collider
    92.     void OnCollisionEnter(Collision collision)
    93.    
    94.     {
    95.         if(collision.gameObject.tag == "Player" || collision.gameObject.tag == "Falak")
    96.         {
    97.             dragging = false;
    98.             scanPos = gameObject.transform.position;
    99.             Triggered = true;
    100.             Debug.Log(name + " Collided");
    101.            
    102.         }
    103.     }
    104.  
    105.  
    106.  
    107.    
    108.    
    109.    
    110. }
    111.  
    112.  
    113.  
    114.  
     
  5. Dennis_eA

    Dennis_eA

    Joined:
    Jan 17, 2011
    Posts:
    380
    Convex does Not change this into a box collider !
    However, it would be (and is) expected behavior if the Mesh Collider you show there looks like a box (if convex), because the mesh collider is then generated in a way like you wrap something in plastic wrap..

    ------

    http://docs.unity3d.com/Manual/class-MeshCollider.html

    That does not solve your second problem?