Problem with Raycast and colliders on skeleton

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:

using UnityEngine;
using System.Collections;

public class test : MonoBehaviour {
	public GameObject Character;
	private GameObject o = null;
	private RaycastHit c = new RaycastHit();

	// Use this for initialization
	void Start () {
		CreateColliders(Character);
	}

	// Update is called once per frame
	void Update () {
		Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
		Debug.DrawLine(r.GetPoint(Camera.main.nearClipPlane), r.GetPoint(Camera.main.farClipPlane), new Color(1.0f, 1.0f, 1.0f, 0.5f));

		if (Input.GetButtonDown("Fire1")) {
			r = Camera.main.ScreenPointToRay(Input.mousePosition);
			if (Physics.Raycast(r, out c))
				o = c.transform.gameObject;
			if (o != null)
				Debug.Log(o);
		}
		
		if (Input.GetButton("Fire1")  o != null) {
			Vector3 p = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, c.point.z - Camera.main.transform.position.z));

			joint j = o.GetComponent<joint>();
			j.transform.LookAt(p, j.transform.TransformDirection(Vector3.up));
			j.transform.Rotate(0, 90, 0);
		}
		
		if (Input.GetButtonUp("Fire1")) {
			o = null;
		}		
	}

	public static void CreateColliders(GameObject Model)
	{
		foreach (joint j in Model.GetComponentsInChildren<joint>()) {
			Transform t = j.transform;
			Transform t0 = t.parent;
			joint j0 = t0.GetComponent<joint>();

			if (j0 != null) {
				CapsuleCollider c = j0.gameObject.AddComponent<CapsuleCollider>();
				float d = Vector3.Distance(t0.position, t.position);
				c.center = new Vector3(-d / 2, c.center.y, c.center.z);
				c.radius = 0.1f;
				c.height = d;
				c.direction = 0;
			}
		}
	}
}

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.

Any ideas? This is a really weird problem. Please try the project and tell me I’m not crazy :slight_smile:

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.

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 :slight_smile:

Is this a bug?

Finally found the way to prove this is an actual bug! Replaced the capsule colliders with sphere colliders and the problem doesn’t occur:

	public static void CreateColliders(GameObject Model)
	{
		foreach (joint j in Model.GetComponentsInChildren<joint>()) {
			Transform t = j.transform;
			Transform t0 = t.parent;
			joint j0 = t0.GetComponent<joint>();

			if (j0 != null) {
/*
				CapsuleCollider c = j0.gameObject.AddComponent<CapsuleCollider>();
				float d = Vector3.Distance(t0.position, t.position);
				c.center = new Vector3(-d / 2, c.center.y, c.center.z);
				c.radius = 0.1f;
				c.height = d;
				c.direction = 0;
*/
				SphereCollider c = j0.gameObject.AddComponent<SphereCollider>();
				float d = Vector3.Distance(t0.position, t.position);
				c.center = new Vector3(-d / 2, c.center.y, c.center.z);
				c.radius = d / 2;
			}
		}
	}

This is the bug report (Case 509438):

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 !

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!

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?

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.

void Update()
{
	collider.enabled = false;
	collider.enabled = true;
}