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

Problem with Raycast and colliders on skeleton

Discussion in 'Editor & General Support' started by marcoantap, Dec 24, 2012.

  1. marcoantap

    marcoantap

    Joined:
    Sep 23, 2012
    Posts:
    215
    Hi! I'm building a pose editor, and I have a script that creates colliders for each joint (component) in the skeleton, just like a ragdoll setup. When the user clicks and drags a collider the bone will move. The problem is after moving different limbs some collisions stop working, and sometimes clicking at random positions on the screen will cause a collision. It's like the collider moved to a new location, but the editor shows the collider in the right place.

    I rewrote the script to isolate the problem from the actual editor, but the problem is persisting. Here is the minimal version that works in Unity 4 without errors/warnings:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class test : MonoBehaviour {
    5.     public GameObject Character;
    6.     private GameObject o = null;
    7.     private RaycastHit c = new RaycastHit();
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         CreateColliders(Character);
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update () {
    16.         Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
    17.         Debug.DrawLine(r.GetPoint(Camera.main.nearClipPlane), r.GetPoint(Camera.main.farClipPlane), new Color(1.0f, 1.0f, 1.0f, 0.5f));
    18.  
    19.         if (Input.GetButtonDown("Fire1")) {
    20.             r = Camera.main.ScreenPointToRay(Input.mousePosition);
    21.             if (Physics.Raycast(r, out c))
    22.                 o = c.transform.gameObject;
    23.             if (o != null)
    24.                 Debug.Log(o);
    25.         }
    26.        
    27.         if (Input.GetButton("Fire1")  o != null) {
    28.             Vector3 p = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, c.point.z - Camera.main.transform.position.z));
    29.  
    30.             joint j = o.GetComponent<joint>();
    31.             j.transform.LookAt(p, j.transform.TransformDirection(Vector3.up));
    32.             j.transform.Rotate(0, 90, 0);
    33.         }
    34.        
    35.         if (Input.GetButtonUp("Fire1")) {
    36.             o = null;
    37.         }      
    38.     }
    39.  
    40.     public static void CreateColliders(GameObject Model)
    41.     {
    42.         foreach (joint j in Model.GetComponentsInChildren<joint>()) {
    43.             Transform t = j.transform;
    44.             Transform t0 = t.parent;
    45.             joint j0 = t0.GetComponent<joint>();
    46.  
    47.             if (j0 != null) {
    48.                 CapsuleCollider c = j0.gameObject.AddComponent<CapsuleCollider>();
    49.                 float d = Vector3.Distance(t0.position, t.position);
    50.                 c.center = new Vector3(-d / 2, c.center.y, c.center.z);
    51.                 c.radius = 0.1f;
    52.                 c.height = d;
    53.                 c.direction = 0;
    54.             }
    55.         }
    56.     }
    57. }
    58.  
    Here is the project in case you want to test it. Move the bones around for a while and you will see:

    http://gamelix.com/demos/unity/test.zip (336KB, Unity 4 project)

    I'm using collisions (Raycast, Rigidbody) in other places of the actual application and I haven't had any problems.
     
  2. marcoantap

    marcoantap

    Joined:
    Sep 23, 2012
    Posts:
    215
    Any ideas? This is a really weird problem. Please try the project and tell me I'm not crazy :)
     
  3. WendygoGames

    WendygoGames

    Joined:
    Dec 11, 2012
    Posts:
    53
    Hey marco, got your message.

    So I think the problem is just that the collider gets buried by the model. so it can get clicked anymore.I have not had an issue with spontaneous collisions though. It is just difficult to click the colliders. Maybe try and make them a little larger? Or even maybe add cubes or something that can physically show were the joint is while in Play. That may be easier to visualize the position of the joint and debug any issues. Because It is hard to see the tiny colliders in edit more.
     
  4. marcoantap

    marcoantap

    Joined:
    Sep 23, 2012
    Posts:
    215
    Thank you Straight Rainbow! I just doubled the scale of the colliders and the problem is still there. But if the problem was with the size of colliders you wouldn't be able to move the bones in first place. If you select Male model you can clearly see the colliders and the intersection of the ray from the camera. I'll keep looking for a solution, thanks again anyway :)
     
  5. marcoantap

    marcoantap

    Joined:
    Sep 23, 2012
    Posts:
    215
    Is this a bug?
     
  6. marcoantap

    marcoantap

    Joined:
    Sep 23, 2012
    Posts:
    215
    Finally found the way to prove this is an actual bug! Replaced the capsule colliders with sphere colliders and the problem doesn't occur:

    Code (csharp):
    1.  
    2.     public static void CreateColliders(GameObject Model)
    3.     {
    4.         foreach (joint j in Model.GetComponentsInChildren<joint>()) {
    5.             Transform t = j.transform;
    6.             Transform t0 = t.parent;
    7.             joint j0 = t0.GetComponent<joint>();
    8.  
    9.             if (j0 != null) {
    10. /*
    11.                 CapsuleCollider c = j0.gameObject.AddComponent<CapsuleCollider>();
    12.                 float d = Vector3.Distance(t0.position, t.position);
    13.                 c.center = new Vector3(-d / 2, c.center.y, c.center.z);
    14.                 c.radius = 0.1f;
    15.                 c.height = d;
    16.                 c.direction = 0;
    17. */
    18.                 SphereCollider c = j0.gameObject.AddComponent<SphereCollider>();
    19.                 float d = Vector3.Distance(t0.position, t.position);
    20.                 c.center = new Vector3(-d / 2, c.center.y, c.center.z);
    21.                 c.radius = d / 2;
    22.             }
    23.         }
    24.     }
    25.  
    This is the bug report (Case 509438):

     
    Last edited: Dec 26, 2012
  7. NanoMath

    NanoMath

    Joined:
    Dec 22, 2012
    Posts:
    33
    I've noticed that problem happens after that, when two colliders intersect THROUGH each other. Maybe it will help you localize a problem. Why does this problem happen I don't know. If I find a solution, then I will tell you. Good Luck ! Best Regards !
     
  8. marcoantap

    marcoantap

    Joined:
    Sep 23, 2012
    Posts:
    215
    Well, now I'm using box colliders in the pose editor and those intersect through each other, but there are no problems. Hope the bug is solved in future versions. Thanks!
     
  9. hoesterey

    hoesterey

    Joined:
    Mar 19, 2010
    Posts:
    659
    We just ran into this bug on our project. Took us forever to figure out. Capsule colliders are not updating on skeleton rotation (though they do in the editor). Can this type of bug be fixed?

     
  10. gediriweera

    gediriweera

    Joined:
    Jul 15, 2013
    Posts:
    1
    If anyone else gets stuck on this, there appears to be a simple workaround. Try switching the capsule colliders off and then on again in the Update function. I tried a few things but this was the only one that had any effect on this strange behaviour.

    Code (csharp):
    1.  
    2. void Update()
    3. {
    4.     collider.enabled = false;
    5.     collider.enabled = true;
    6. }
    7.