Esempio n. 1
0
    void PushBack()
    {
        if (spheres != null)
        {
            for (int i = 0; i < spheres.Length; ++i)
            {
                CollisionSphere s = spheres[i];

                if (s != null)
                {
                    foreach (Collider collider in Physics.OverlapSphere(OffsetPosition(s.Offset), bodyRadius, walkable))
                    {
                        Vector3 position     = OffsetPosition(s.Offset);
                        Vector3 contactPoint = Vector3.zero;

                        if (collider is BoxCollider)
                        {
                            contactPoint = RPGCollisions.ClosestPointOn((BoxCollider)collider, position);
                        }
                        else if (collider is SphereCollider)
                        {
                            contactPoint = RPGCollisions.ClosestPointOn((SphereCollider)collider, position);
                        }
                        else if (collider is MeshCollider)
                        {
                            RPGMesh rpgMesh = collider.GetComponent <RPGMesh>();

                            if (rpgMesh != null)
                            {
                                contactPoint = rpgMesh.ClosestPointOn(position, bodyRadius, displayDebugInfo, displayExtendedDebugInfo);
                            }
                            else
                            {
                                // Make last ditch try for convex colliders
                                MeshCollider mc = (MeshCollider)collider;

                                if (mc.convex)
                                {
                                    contactPoint = mc.ClosestPointOnBounds(position);
                                }
                                else
                                {
                                    continue;
                                }
                            }
                        }
                        else if (collider is CapsuleCollider)
                        {
                            Debug.LogWarning("[RPGMotor] CapsuleCollider not supported");
                        }
                        else if (collider is TerrainCollider)
                        {
                        }
                        else if (collider is WheelCollider)
                        {
                            Debug.LogWarning("[RPGMotor] WheelColliders not supported");
                        }
                        else
                        {
                            continue;
                        }

                        if (contactPoint != Vector3.zero)
                        {
                            // If the sphere is feet
                            // We have a ground and we're touching
                            // And the sphere position is above the contact position
                            // We should ignore it
                            if (s.IsFeet && HasGround && CurrentGround.IsTouching && position.y > contactPoint.y)
                            {
                                continue;
                            }

                            // If this is the head sphere
                            // And we're jumping
                            // And we hit the "top" of the sphere, abort jumping
                            if (s.IsHead && IsJumping && contactPoint.y > (position.y + bodyRadius * 0.25f))
                            {
                                JumpDone(null);
                            }

                            // Vector from contact point to position
                            Vector3 v = position - contactPoint;

                            // Draw debug line
                            if (displayDebugInfo)
                            {
                                Debug.DrawLine(position, contactPoint, Color.red);
                            }

                            // Display extend debug info
                            if (displayExtendedDebugInfo)
                            {
                                Debug.Log("[RPGMotor] Contact point " + contactPoint);
                            }

                            // Move object away from collision
                            target.position += Vector3.ClampMagnitude(v, Mathf.Clamp(bodyRadius - v.magnitude, 0, bodyRadius));
                        }
                    }
                }
            }
        }
    }
Esempio n. 2
0
    void PushBack()
    {
        if (spheres != null)
        {
            for (int i = 0; i < spheres.Length; ++i)
            {
                CollisionSphere s = spheres[i];

                if (s != null)
                {
                    foreach (Collider collider in Physics.OverlapSphere(OffsetPosition(s.Offset), bodyRadius, walkable))
                    {
                        Vector3 position     = OffsetPosition(s.Offset);
                        Vector3 contactPoint = Vector3.zero;

                        if (collider is BoxCollider)
                        {
                            contactPoint = RPGCollisions.ClosestPointOn((BoxCollider)collider, position);
                        }
                        else if (collider is SphereCollider)
                        {
                            contactPoint = RPGCollisions.ClosestPointOn((SphereCollider)collider, position);
                        }
                        else if (collider is MeshCollider)
                        {
                            RPGMesh rpgMesh = collider.GetComponent <RPGMesh>();

                            if (rpgMesh != null)
                            {
                                contactPoint = rpgMesh.ClosestPointOn(position, bodyRadius, displayDebugInfo, displayExtendedDebugInfo);
                            }
                            else
                            {
                                MeshCollider mc = (MeshCollider)collider;

                                if (mc.convex)
                                {
                                    contactPoint = mc.ClosestPointOnBounds(position);
                                }
                                else
                                {
                                    continue;
                                }
                            }
                        }
                        else if (collider is CapsuleCollider)
                        {
                        }
                        else if (collider is TerrainCollider)
                        {
                        }
                        else if (collider is WheelCollider)
                        {
                            Debug.LogWarning("[RPGMotor] WheelColliders not supported");
                        }
                        else
                        {
                            continue;
                        }

                        if (contactPoint != Vector3.zero)
                        {
                            //如果这个sphere是脚
                            //我们接触一个地面
                            //并且球的位置在接触位置之上
                            //我们应该忽略它
                            if (s.IsFeet && HasGround && CurrentGround.IsTouching && position.y > contactPoint.y)
                            {
                                continue;
                            }

                            //如果这个球是头
                            //并且跳跃
                            //并且撞到了头,就终止跳跃
                            if (s.IsHead && IsJumping && contactPoint.y > (position.y + bodyRadius * 0.25f))
                            {
                                JumpDone(null);
                            }

                            Vector3 v = position - contactPoint;

                            if (displayDebugInfo)
                            {
                                Debug.DrawLine(position, contactPoint, Color.red);
                            }

                            if (displayExtendedDebugInfo)
                            {
                                //Debug.Log("[RPGMotor] Contact point " + contactPoint);
                            }

                            target.position += Vector3.ClampMagnitude(v, Mathf.Clamp(bodyRadius - v.magnitude, 0, bodyRadius));
                        }
                    }
                }
            }
        }
    }