Exemplo n.º 1
0
        //https://docs.unity3d.com/ScriptReference/Physics.Raycast.html?_ga=2.69651992.960042701.1515280206-1316818202.1483563338
        public float checkRayFlipped(IGravityClient gameObject, Vector3 positionRay, float distance,
                                     float yAngle, float zAngle, out RaycastHit hit, Color color, int layersToIgnore = 0)
        {
            float   result   = float.MaxValue;
            Vector3 startRay = new Vector2(positionRay.x, positionRay.y);
            Vector3 endRay   = new Vector2(positionRay.x + distance, positionRay.y);
            Vector3 dir      = Vector3.left;

            Vector3 eulerVector = new Vector3(0.0f, 0.0f, 0.0f);

            startRay = Quaternion.Euler(eulerVector) * (startRay);
            endRay   = Quaternion.Euler(eulerVector) * (endRay);
            dir      = Quaternion.Euler(eulerVector) * (dir);

            eulerVector.z = zAngle;
            startRay      = Quaternion.Euler(eulerVector) * (startRay);
            endRay        = Quaternion.Euler(eulerVector) * (endRay);
            dir           = Quaternion.Euler(eulerVector) * (dir);

            startRay += gameObject.getPosition();
            endRay   += gameObject.getPosition();

            int layerMask = ~(1 << gameObject.getLayer() | layersToIgnore);

            if (yAngle > 0.0f)
            {
                dir = Vector3.back;
            }
            if (yAngle < 0.0f)
            {
                dir = Vector3.forward;
            }
            if (Physics.Raycast(startRay, dir, out hit, 1000000.0f, layerMask))
            {
                result = hit.distance;
            }

            Debug.DrawRay(startRay, dir, color, 0.2f, false);

            return(result);
        }
Exemplo n.º 2
0
        public virtual float checkRay(IGravityClient gameObject, Vector3 positionRay, float distance, float yAngle, float zAngle, out RaycastHit2D hit, Color color, int layersToIgnore = 0)
        {
            float   result   = float.MaxValue;
            Vector3 startRay = new Vector2(positionRay.x, positionRay.y);
            Vector3 endRay   = new Vector2(positionRay.x + distance, positionRay.y);
            Vector2 dir      = Vector2.right;

            Vector3 eulerVector = new Vector3(0.0f, 0.0f, 0.0f);

            startRay = Quaternion.Euler(eulerVector) * (startRay);
            endRay   = Quaternion.Euler(eulerVector) * (endRay);
            dir      = Quaternion.Euler(eulerVector) * (dir);

            eulerVector.y = yAngle;
            eulerVector.z = zAngle;
            startRay      = Quaternion.Euler(eulerVector) * (startRay);
            endRay        = Quaternion.Euler(eulerVector) * (endRay);
            dir           = Quaternion.Euler(eulerVector) * (dir);

            startRay += gameObject.getPosition();
            endRay   += gameObject.getPosition();

            int layerMask = layersToIgnore;

            hit = Physics2D.Raycast(startRay, dir, 1000000.0f, layerMask);

            if (hit.collider != null)
            {
                result = hit.distance;
                Debug.DrawLine(startRay, hit.point, color);
            }

            else
            {
                Debug.DrawLine(startRay, endRay, color);
            }

            return(result);
        }