Пример #1
0
    private void Update()
    {
        if (!isSetUp)
        {
            return;
        }

        if (!isOn)
        {
            return;
        }

        laser.startWidth = Random.Range(laser_width - max_laser_offset, laser_width + max_laser_offset);;
        laser.endWidth   = laser.startWidth;

        laser.startColor = new Color(laser_color.r + Random.Range(-max_color_offset, max_color_offset),
                                     laser_color.g + Random.Range(-max_color_offset, max_color_offset),
                                     laser_color.b + Random.Range(-max_color_offset, max_color_offset));
        laser.endColor = laser.startColor;

        hit2D = Physics2D.Raycast(transform.position + offset_scalar * laser_dir, laser_dir, 5f, 1 << hittable_layer | 1 << editable_layer);
        if (hit2D.collider == null)
        {
            //short circuit if endpt of laser is the same as previous frame
            if (laser.GetPosition(1) == transform.position + max_dist * laser_dir)
            {
                return;
            }

            //otherwise set endpoint of laser to position + max distance * direction
            laser.SetPosition(1, transform.position + max_dist * laser_dir);

            //If a new port is found or no port is found, do something
            if (port != null)
            {
                ////Debug.Log("HELLO");

                port.Deactivate();
                port = null;
            }
        }
        else
        {
            ////short circuit if endpt of laser is the same as previous frame
            //if ((Vector2)laser.GetPosition(1) == hit2D.point)
            //{
            //    return;
            //}

            //otherwise set endpoint of laser to hit_point of raycast
            laser.SetPosition(1, hit2D.point);

            //If a new port is found or no port is found, do something
            if (hit2D.collider.GetComponent <Port>() != port)
            {
                ////Debug.Log("HELLO");
                //Deactivate current Port if existing
                if (port != null)
                {
                    ////Debug.Log(transform.name + " is deactivating " + port.transform.name);
                    port.Deactivate();
                }

                //Activate new Port if possible
                if ((port = hit2D.collider.GetComponent <Port>()) != null)
                {
                    ////Debug.Log(transform.name + " is activating " + port.transform.name);
                    port.Activate(laser_color);
                }
            }
        }
    }