private void UpdateGameColliders() { int i = 0; bool is_dir_flipped = false; const double flip_cutoff = 10.0; if (Math.Abs(creature_renderer.transform.rotation.eulerAngles.y) > flip_cutoff) { is_dir_flipped = true; } if (parentRBD) { parentRBD.isKinematic = noGravity; } foreach (var cur_body in child_bodies) { var cur_bone = runtime_bones[i]; XnaGeometry.Vector4 spawn_center = (cur_bone.getWorldStartPt() + cur_bone.getWorldEndPt()) * 0.5f; XnaGeometry.Vector4 spawn_dir = (cur_bone.getWorldEndPt() - cur_bone.getWorldStartPt()); spawn_dir.Normalize(); // Get the direction in the renderer's space UnityEngine.Vector3 u_spawn_dir = new UnityEngine.Vector3((float)spawn_dir.X, (float)spawn_dir.Y, (float)0); //u_spawn_dir = TransformToCreatureDir(u_spawn_dir); float setAngle = -(float)Math.Atan2((double)u_spawn_dir.y, (double)u_spawn_dir.x) * Mathf.Rad2Deg; /* * if(is_dir_flipped) * { * setAngle = -setAngle; * } * else { * setAngle = -setAngle; * } */ if (cur_body != null) { cur_body.MoveRotation(setAngle); UnityEngine.Vector3 local_pos = new UnityEngine.Vector3((float)spawn_center.X, (float)spawn_center.Y, (float)0); cur_body.MovePosition(TransformToCreaturePt(local_pos)); } i++; } }
public void CreateGameColliders() { if (creature_renderer == null) { return; } if (transform.childCount > 0) { Debug.Log("Please remove all children game colliders before running CreateGameColliders!"); return; } creature_renderer.InitData(); MeshBone root_bone = GetRootBone(); // Create a base rigid body that actually participates in simulation for the creature_renderer var parent_obj = creature_renderer.gameObject; Rigidbody2D parent_rbd = parent_obj.AddComponent <Rigidbody2D>(); parentRBD = parent_rbd; parent_rbd.isKinematic = false; BoxCollider2D parent_collider = parent_obj.AddComponent <BoxCollider2D>(); parent_collider.size = new UnityEngine.Vector2(simColliderWidth, simColliderHeight); // Create rigid bodies for all the bones List <MeshBone> all_children = root_bone.getAllChildren(); foreach (MeshBone cur_child in all_children) { // Create new GameObject GameObject new_obj = new GameObject(cur_child.key); // Add components XnaGeometry.Vector4 spawn_center = (cur_child.getWorldStartPt() + cur_child.getWorldEndPt()) * 0.5f; float colliderWidth = (float)(cur_child.getWorldRestEndPt() - cur_child.getWorldRestStartPt()).Length(); XnaGeometry.Vector4 spawn_dir = (cur_child.getWorldRestEndPt() - cur_child.getWorldRestStartPt()); spawn_dir.Normalize(); // Get the direction in the renderer's space UnityEngine.Vector3 u_spawn_dir = new UnityEngine.Vector3((float)spawn_dir.X, (float)spawn_dir.Y, (float)0); u_spawn_dir = TransformToCreatureDir(u_spawn_dir); float startAngle = (float)Math.Atan2((double)u_spawn_dir.y, (double)u_spawn_dir.x) * Mathf.Rad2Deg; UnityEngine.Vector3 local_pos = new UnityEngine.Vector3((float)spawn_center.X, (float)spawn_center.Y, (float)0); Rigidbody2D new_rbd = new_obj.AddComponent <Rigidbody2D>(); new_rbd.isKinematic = true; BoxCollider2D new_collider = new_obj.AddComponent <BoxCollider2D>(); new_collider.size = new UnityEngine.Vector2(colliderWidth, colliderHeight); // set the position using the renderer's parent transform new_obj.transform.position = TransformToCreaturePt(local_pos); new_obj.transform.rotation = UnityEngine.Quaternion.AngleAxis(startAngle, new UnityEngine.Vector3(0, 0, 1)); // Make new object child of the parent new_obj.transform.parent = gameObject.transform; } }