예제 #1
0
    // Casts three rays to a point, returns all the hits or misses
    void rayCastToPoint(Vector2 point, ref List <Vector3> vertices)
    {
        RaycastHit2D[] target = new RaycastHit2D[1];
        // Raycast fro the center to the point, and to the left and to the right
        Vector2 direction = point - new Vector2(lightCenter.x, lightCenter.y);
        Vector2 left      = rotatePointbyRadians(direction, 0.00001f);
        Vector2 right     = rotatePointbyRadians(direction, -0.00001f);

        // Aim for point
        if (colliderCircle.Raycast(direction, layerfilter, target, lightRadius) == 0)
        {
            // if no collision, push in a point on the radius
            direction.Normalize();
            vertices.Add(new Vector3((direction.x * lightRadius) + lightCenter.x, (direction.y * lightRadius) + lightCenter.y, lightCenter.z));
        }
        else
        {
            // if collision, push in the collision point
            vertices.Add(new Vector3(target[0].point.x, target[0].point.y, lightCenter.z));
        }
        // Aim left
        if (colliderCircle.Raycast(left, layerfilter, target, lightRadius) == 0)
        {
            left.Normalize();
            vertices.Add(new Vector3((left.x * lightRadius) + lightCenter.x, (left.y * lightRadius) + lightCenter.y, lightCenter.z));
        }
        else
        {
            vertices.Add(new Vector3(target[0].point.x, target[0].point.y, lightCenter.z));
        }
        // Aim Right
        if (colliderCircle.Raycast(right, layerfilter, target, lightRadius) == 0)
        {
            right.Normalize();
            vertices.Add(new Vector3((right.x * lightRadius) + lightCenter.x, (right.y * lightRadius) + lightCenter.y, lightCenter.z));
        }
        else
        {
            vertices.Add(new Vector3(target[0].point.x, target[0].point.y, lightCenter.z));
        }
    }
예제 #2
0
    void Update()
    {
        if (running)
        {
            transform.position += direction * Time.deltaTime * speed;
        }

        RaycastHit2D[] hits = new RaycastHit2D[10];
        if (collider.Raycast(direction, hits, 0.2f) > 0)
        {
            direction = Vector2.Reflect(direction, hits[0].normal);
        }
    }
예제 #3
0
    private RaycastHit2D[] interloperHit = new RaycastHit2D[1];                         // BUGFIX: for UpdateCarriedItem checks
    private void UpdateCarriedItem()
    {
        if (!isCarryingItem || path == null || path.Length <= 0)
        {
            return;
        }

        Vector3 carryDir = (path [path.Length - 1] - transform.position).normalized;
        Vector3 carryPos = transform.position + carryDir * carryItemDistance;

        carriedItem.transform.position = new Vector3(carryPos.x, carryPos.y, 0.0f);

        // BUGFIX: a box will sometimes get pushed between a robot and its carried item, causing the robot to
        // rocket backwards due to the default unity collision correction
        circleCollider.Raycast(carryDir, interloperHit, carryItemDistance, groundedResetMask);
        if (interloperHit[0].collider != null)
        {
            Throwable hitItem = interloperHit[0].collider.GetComponent <Throwable> ();
            if (hitItem != null && (hitItem is Box) && hitItem != carriedItem)
            {
                DropItem();
            }
        }
    }
예제 #4
0
    private void View()
    {
        //获得在ViewRange的物体
        List <Collider2D> targets = new List <Collider2D>();
        ContactFilter2D   vf      = new ContactFilter2D().NoFilter(); //过滤器

        vf.SetLayerMask(ViewLayer);
        vf.useLayerMask = true;
        vf.useTriggers  = true;
        //Debug.Log(LayerMask.LayerToName(vm));
        ViewRange.OverlapCollider(vf, targets);

        //Debug.Log(ViewRange.OverlapCollider(vf, targets));

        //遍历
        foreach (Collider2D c in targets)
        {
            if (c == null)
            {
                continue;
            }
            SensoryVisual sv;
            //检测到SensoryVisual脚本的才是可视对象
            if ((sv = c.gameObject.GetComponent <SensoryVisual>()) == null)
            {
                //Debug.Log(c.gameObject.name + "不可视");
                continue;
            }

            Vector2 thisPos   = this.transform.position; //自己的位置
            Vector2 targetPos = c.transform.position;    //检测目标的位置
            Vector2 dir       = this.transform.up;       //自己的方向
            Vector2 targetDir = targetPos - thisPos;     //检测目标的相对位置
            ////坐标cos公式
            //float cos = Vector2.Dot(targetDir, dir) / targetDir.magnitude / dir.magnitude;
            float angle = Vector3.Angle(dir, targetDir) / 180 * Mathf.PI;                   //相对角度角度制

            //Debug.Log(angle);

            float viewAngleRangeArc = ViewAngleRange / 180f * Mathf.PI;                     //探测范围弧度制



            //超出角度判定
            if (angle > viewAngleRangeArc / 2f || angle < -viewAngleRangeArc / 2f)
            {
                //Debug.Log(c.gameObject.name + "超出角度判定");
                continue;
            }


            float checkRange    = Mathf.Asin(sv.VisualRange.radius / targetDir.magnitude) * 2;             //需要检测的角度范围
            int   checkTimes    = (int)(checkRange / (ViewCheckDegrees / 180 * Mathf.PI)) + 1;             //计算检测次数
            float checkRange_r  = ViewCheckOffset * checkRange;                                            //方便边缘检测
            float anglePerCheck = checkRange_r / (checkTimes + 1);                                         //每次增加的角度
            float targetAngle   = Mathf.Atan2(targetDir.y, targetDir.x);                                   //目标方向的绝对角
            float startAngle    = targetAngle - checkRange / 2f + checkRange * (1 - ViewCheckOffset) / 2f; //起始角度
            float currentAngle  = startAngle;                                                              //当前检测的角度

            //检测
            for (int i = 0; i < checkTimes + 2; ++i)
            {
                //射线超出角度剔除
                if (currentAngle - targetAngle > viewAngleRangeArc / 2f || angle < -viewAngleRangeArc / 2f)
                {
                    continue;
                }
                //计算射线的方向向量
                Vector3    v0  = new Vector3(1f, 0f, 0f);
                Quaternion rot = new Quaternion();
                rot.eulerAngles = new Vector3(0, 0, currentAngle / Mathf.PI * 180);
                Vector2 v1 = rot * v0;          //方向向量
                currentAngle += anglePerCheck;  //递增角度

                //进行射线判断
                RaycastHit2D[] hits   = new RaycastHit2D[10];
                int            length = ViewRange.Raycast(v1, vf, hits, ViewRange.radius);
                //什么都没碰到
                if (length <= 0)
                {
                    Debug.DrawLine(thisPos, thisPos + v1 * ViewRange.radius, Color.red);
                    continue;
                }

                //第一个命中
                if (hits[0].collider.gameObject.GetComponent <SensoryVisual>() == sv)
                {
                    memory.UpdateViewRecord(hits[0].collider.gameObject);
                    Debug.DrawLine(thisPos, hits[0].point, Color.green);
                }
                //没命中
                else
                {
                    Debug.DrawLine(thisPos, hits[0].point, Color.red);
                }
            }
        }
    }