public CapsuleCollider(bool isTrigger, Vector3 center, float radius, float height, CapsuleDirection direction) : base(ColliderType.Capsule, isTrigger, center) { Radius = radius; Height = height; Direction = direction; }
/* * Display viewport controls for messing with individual body parts * */ void ViewportShapeControls(BodyPart part) { GUILayout.Label("Shape Controls:"); GUILayout.BeginVertical(); { part.shapeType = (ShapeType)GUILayout.SelectionGrid((int)part.shapeType, System.Enum.GetNames(typeof(ShapeType)), 4); if (part.shapeType == ShapeType.Capsule) { GUILayout.BeginHorizontal(); { GUILayout.Label("Capsule Direction:", GUILayout.Width(viewportControlsWidth * 0.65f)); CapsuleDirection dir = (CapsuleDirection)GUILayout.SelectionGrid((int)part.capsuleDirection, System.Enum.GetNames(typeof(CapsuleDirection)), 3); if (dir != part.capsuleDirection) { part.FlipCapsule(dir); if (isSymmetrical) { part.oppositePart.FlipCapsule(dir); } } } GUILayout.EndHorizontal(); } } GUILayout.EndVertical(); }
internal static TriggerCallbackController CreateCapsule(Vector3 center, CapsuleDirection direction, Single radius, Single height, Quaternion rotation, Boolean continuous, LayerIndex layer, Boolean debugVisible = false) { var obj = new GameObject("CapsuleCallback"); obj.transform.position = center; obj.transform.rotation = rotation; var cap = obj.AddComponent <CapsuleCollider>(); cap.direction = (Int32)direction; cap.radius = radius; cap.height = height; cap.isTrigger = true; return(Create(obj, cap, continuous, layer, debugVisible)); }
protected virtual void Awake() { Transform = GetComponent <Transform>(); if (null == _collider) { _collider = GetComponent <Collider>(); } if (_collider != null) { _bounds = _collider.bounds; DetectDOColliderType(); if (DOColliderType.Box == _colliderType) { BoxCollider bCollider = _collider as BoxCollider; Radius = bCollider.bounds.extents.magnitude; _direction = CapsuleDirection.None; _height = bCollider.bounds.extents.magnitude; } else if (DOColliderType.Sphere == _colliderType) { SphereCollider sCollider = _collider as SphereCollider; Radius = sCollider.radius; _direction = CapsuleDirection.None; _height = sCollider.radius; } else if (DOColliderType.Capsule == _colliderType) { CapsuleCollider cCollider = _collider as CapsuleCollider; Radius = cCollider.radius; _height = cCollider.height; _direction = (CapsuleDirection)cCollider.direction; } RecalculateScaledValues(); DetectableObjectManager.Instance.Register(this); } else { enabled = false; } }
/* * Adjust shapeSize automatically when the capsule direction changes * */ public void FlipCapsule(CapsuleDirection toDirection) { // early out if there is no flip if (toDirection == capsuleDirection) { return; } // swap around shapeSize components as needed switch (capsuleDirection) { case CapsuleDirection.X: if (toDirection == CapsuleDirection.Y) { shapeSize = new Vector3(shapeSize.y, shapeSize.x, shapeSize.z); } else { shapeSize = new Vector3(shapeSize.z, shapeSize.y, shapeSize.x); } break; case CapsuleDirection.Y: if (toDirection == CapsuleDirection.X) { shapeSize = new Vector3(shapeSize.y, shapeSize.x, shapeSize.z); } else { shapeSize = new Vector3(shapeSize.x, shapeSize.z, shapeSize.y); } break; case CapsuleDirection.Z: if (toDirection == CapsuleDirection.X) { shapeSize = new Vector3(shapeSize.z, shapeSize.y, shapeSize.x); } else { shapeSize = new Vector3(shapeSize.x, shapeSize.z, shapeSize.y); } break; } capsuleDirection = toDirection; }
private Unity_collidersCapsuleCollider.DirectionEnum?GetDirection(CapsuleDirection direction) { switch (direction) { case CapsuleDirection.X: return(Unity_collidersCapsuleCollider.DirectionEnum.x); case CapsuleDirection.Y: return(Unity_collidersCapsuleCollider.DirectionEnum.y); case CapsuleDirection.Z: return(Unity_collidersCapsuleCollider.DirectionEnum.z); case CapsuleDirection.Default: default: return(null); } }
/* * Convert this part's shapeSize into the opposite part's space * */ public void PasteShapeSizeToOpposite(bool applyScale) { Vector3 s = TransformPointToOpposite(shapeSize, applyScale); if (s.sqrMagnitude == 0f) { return; // early out if there is invalid scale } oppositePart.shapeSize = new Vector3(Mathf.Abs(s.x), Mathf.Abs(s.y), Mathf.Abs(s.z)); if (oppositePart.shapeType == ShapeType.Sphere) { oppositePart.shapeSize = Vector3.one * VectorHelpers.MaxValue(oppositePart.shapeSize); } else if (oppositePart.shapeType == ShapeType.Capsule) { CapsuleDirection currentDirection = oppositePart.capsuleDirection; oppositePart.capsuleDirection = capsuleDirection; oppositePart.FlipCapsule(currentDirection); } }
public void SetSize(Vector3 size) { float h = size.x; float r = Mathf.Max(size.y, size.z); int d = 0; if (size.y > h) { h = size.y; r = Mathf.Max(size.x, size.z); d = 1; } if (size.z > h) { h = size.z; r = Mathf.Max(size.x, size.y); d = 2; } height = h; radius = r / 2; direction = (CapsuleDirection)d; }
private void DrawCapsuleImp(Vector3 pos, Vector3 center, Vector3 scale, CapsuleDirection direction, float radius, float height, Color color) { // 参数保护 if (height < 0f) { Debug.LogWarning("Capsule height can not be negative!"); return; } if (radius < 0f) { Debug.LogWarning("Capsule radius can not be negative!"); return; } // 根据朝向找到up 和 高度缩放值 Vector3 up = Vector3.up; // 半径缩放值 float radiusScale = 1f; // 高度缩放值 float heightScale = 1f; switch (direction) { case CapsuleDirection.XAxis: up = Vector3.right; heightScale = Mathf.Abs(scale.x); radiusScale = Mathf.Max(Mathf.Abs(scale.y), Mathf.Abs(scale.z)); break; case CapsuleDirection.YAxis: up = Vector3.up; heightScale = Mathf.Abs(scale.y); radiusScale = Mathf.Max(Mathf.Abs(scale.x), Mathf.Abs(scale.z)); break; case CapsuleDirection.ZAxis: up = Vector3.forward; heightScale = Mathf.Abs(scale.z); radiusScale = Mathf.Max(Mathf.Abs(scale.x), Mathf.Abs(scale.y)); break; } float realRadius = radiusScale * radius; height = height * heightScale; float sideHeight = Mathf.Max(height - 2 * realRadius, 0f); center = new Vector3(center.x * scale.x, center.y * scale.y, center.z * scale.z); // 为了符合Unity的CapsuleCollider的绘制样式,调整位置 pos = pos - up.normalized * (sideHeight * 0.5f + realRadius) + center; Color oldColor = Gizmos.color; Gizmos.color = color; up = up.normalized * realRadius; Vector3 forward = Vector3.Slerp(up, -up, 0.5f); Vector3 right = Vector3.Cross(up, forward).normalized *realRadius; Vector3 start = pos + up; Vector3 end = pos + up.normalized * (sideHeight + realRadius); // 半径圆 DrawCircleImp(start, up, color, realRadius); DrawCircleImp(end, up, color, realRadius); // 边线 Gizmos.DrawLine(start - forward, end - forward); Gizmos.DrawLine(start + right, end + right); Gizmos.DrawLine(start - right, end - right); Gizmos.DrawLine(start + forward, end + forward); Gizmos.DrawLine(start - forward, end - forward); for (int i = 1; i < 26; i++) { // 下部的头 Gizmos.DrawLine(start + Vector3.Slerp(right, -up, (i - 1) / 25f), start + Vector3.Slerp(right, -up, i / 25f)); Gizmos.DrawLine(start + Vector3.Slerp(-right, -up, (i - 1) / 25f), start + Vector3.Slerp(-right, -up, i / 25f)); Gizmos.DrawLine(start + Vector3.Slerp(forward, -up, (i - 1) / 25f), start + Vector3.Slerp(forward, -up, i / 25f)); Gizmos.DrawLine(start + Vector3.Slerp(-forward, -up, (i - 1) / 25f), start + Vector3.Slerp(-forward, -up, i / 25f)); // 上部的头 Gizmos.DrawLine(end + Vector3.Slerp(forward, up, (i - 1) / 25f), end + Vector3.Slerp(forward, up, i / 25f)); Gizmos.DrawLine(end + Vector3.Slerp(-forward, up, (i - 1) / 25f), end + Vector3.Slerp(-forward, up, i / 25f)); Gizmos.DrawLine(end + Vector3.Slerp(right, up, (i - 1) / 25f), end + Vector3.Slerp(right, up, i / 25f)); Gizmos.DrawLine(end + Vector3.Slerp(-right, up, (i - 1) / 25f), end + Vector3.Slerp(-right, up, i / 25f)); } Gizmos.color = oldColor; }
public void DrawCapsule(string capsuleName, Vector3 pos, Vector3 center, Vector3 scale, float radius, float height, CapsuleDirection direction, Color color) { if (!Enable) { return; } Capsule temp; if (Capsules.TryGetValue(capsuleName, out temp)) { temp.Position = pos; temp.Center = center; temp.Scale = scale; temp.Radius = radius; temp.Height = height; temp.Direction = direction; temp.Color = color; } else { temp = new Capsule() { Position = pos, Center = center, Scale = scale, Radius = radius, Height = height, Direction = direction, Color = color }; Capsules.Add(capsuleName, temp); } }
/* * Adjust shapeSize automatically when the capsule direction changes * */ public void FlipCapsule(CapsuleDirection toDirection) { // early out if there is no flip if (toDirection == capsuleDirection) return; // swap around shapeSize components as needed switch (capsuleDirection) { case CapsuleDirection.X: if (toDirection == CapsuleDirection.Y) shapeSize = new Vector3(shapeSize.y, shapeSize.x, shapeSize.z); else shapeSize = new Vector3(shapeSize.z, shapeSize.y, shapeSize.x); break; case CapsuleDirection.Y: if (toDirection == CapsuleDirection.X) shapeSize = new Vector3(shapeSize.y, shapeSize.x, shapeSize.z); else shapeSize = new Vector3(shapeSize.x, shapeSize.z, shapeSize.y); break; case CapsuleDirection.Z: if (toDirection == CapsuleDirection.X) shapeSize = new Vector3(shapeSize.z, shapeSize.y, shapeSize.x); else shapeSize = new Vector3(shapeSize.x, shapeSize.z, shapeSize.y); break; } capsuleDirection = toDirection; }