Ejemplo n.º 1
0
// TODO Bounds from mesh
//        Bounds CalculateFromMesh(Mesh mesh)
//        {
//            if (aObj == null)
//            {
//                Debug.LogError("CalculateBoundingBox: object is null");
//                return new Bounds(Vector3.zero, Vector3.one);
//            }
//            Transform myTransform = aObj.transform;
//            Mesh mesh = null;
//            MeshFilter mF = aObj.GetComponent<MeshFilter>();
//                if (mF != null)
//                mesh = mF.mesh;
//                else
//            {
//                SkinnedMeshRenderer sMR = aObj.GetComponent<SkinnedMeshRenderer>();
//                if (sMR != null)
//                    mesh = sMR.sharedMesh;
//            }
//        if (mesh == null)
//        {
//        Debug.LogError("CalculateBoundingBox: no mesh found on the given object");
//        return new Bounds(aObj.transform.position, Vector3.one);
//        }
//        Vector3[] vertices = mesh.vertices;
//        if (vertices.Length <=0)
//        {
//        Debug.LogError("CalculateBoundingBox: mesh doesn't have vertices");
//        return new Bounds(aObj.transform.position, Vector3.one);
//        }
//        Vector3 min, max;
//        min = max = myTransform.TransformPoint(vertices[0]);
//        for (int i = 1; i < vertices.Length; i++)
//        {
//        Vector3 V = myTransform.TransformPoint(vertices[i]);
//            for (int n = 0; n < 3; n++)
//        {
//            if (V[n] > max[n])
//                max[n] = V[n];
//            if (V[n] < min[n])
//                min[n] = V[n];
//        }
//        }
//        Bounds B = new Bounds();
//        B.SetMinMax(min, max);
//        return B;
//        }

        private void CheckSurround(Vector2 wdir)
        {
            var        pos = _collider.transform.position + Vector3.up;
            RaycastHit rayhit;

            for (int i = 1, n = _sides.Length; i < n; i++)
            {
                if (!UnityEngine.Physics.Raycast(pos, _collider.transform.TransformDirection(_sides[i]), out rayhit, 2, _movementMask))
                {
                    continue;
                }


                if (rayhit.distance < 1f)
                {
                    OnWallClose?.Invoke(i);

                    if (i == 2) // forward
                    {
                        CheckWallHeigt(rayhit);
                    }

                    Debug.DrawLine(pos, rayhit.point, Color.red);
                    return;
                }
                else
                {
                    Debug.DrawLine(pos, rayhit.point, Color.blue);
                }
            }

            OnWallClose?.Invoke(0);
        }
        private void CheckSurround(Vector2 wdir)
        {
            Vector2 pos = new Vector2(_collider.transform.position.x, _collider.transform.position.y) + Vector2.up;

            _wall = 0;
            for (int i = 1, n = _sides.Length; i < n; i++)
            {
                var dir    = _collider.transform.TransformDirection(_sides[i]);
                var rayhit = Physics2D.Raycast(pos, new Vector2(dir.x, dir.y), _radius * 5, _movementMask);

                if (rayhit.collider == null)
                {
                    continue;
                }

                _wall         = i;
                _wallDistance = rayhit.distance;
                if (rayhit.distance <= _radius * 1.1f)
                {
                    OnWallClose?.Invoke(i);

                    var heigt = CheckWallHeigt(rayhit);

                    // Set position
                    //_collider.transform.position = new Vector3( rayhit.point.x - _radius * _sides[i].x, Collider.transform.position.y, Collider.transform.position.z );

                    Debug.DrawLine(pos, rayhit.point, Color.red);
                    Debug.DrawRay(pos, Vector2.up * heigt, Color.blue);
                    return;
                }
                else
                {
                    Debug.DrawLine(pos, rayhit.point, Color.blue);
                }
            }

            OnWallClose?.Invoke(0);
        }