Ejemplo n.º 1
0
 void OnDisable()
 {
     // delete  line object on exit
     l.Destroy();
 }
Ejemplo n.º 2
0
    public double[] GetSensorData()
    {
        //frontCenterHitDetection = Physics2D.Linecast(frontCenterSensorStart.position, frontCenterSensorLength.position, 1 << LayerMask.NameToLayer("InnerBoundary"));
        //frontRightHitDetection = Physics2D.Linecast(frontRightAngleSensorStart.position, frontRightAngleSensorLength.position, 1 << LayerMask.NameToLayer("InnerBoundary"));
        //frontLeftHitDetection = Physics2D.Linecast(frontLeftAngleSensorStart.position, frontLeftAngleSensorLength.position, 1 << LayerMask.NameToLayer("InnerBoundary"));
        //backCenterHitDetection = Physics2D.Linecast(backCenterSensorStart.position, backCenterSensorLength.position, 1 << LayerMask.NameToLayer("InnerBoundary"));
        //backRightHitDetection = Physics2D.Linecast(backRightAngleSensorStart.position, backRightAngleSensorLength.position, 1 << LayerMask.NameToLayer("InnerBoundary"));
        //backLeftHitDetection = Physics2D.Linecast(backLeftAngleSensorStart.position, backLeftAngleSensorLength.position, 1 << LayerMask.NameToLayer("InnerBoundary"));
        double[] sensorData = new double[6];
        float    fraction   = 0; //Percentage of distance to the nearest wall;

        Alpha = (byte)(GetComponent <Respawn>().isAlive ? 92 : 24);
        Blue  = 0;

        //Front Center Hit Detection
        frontCenterDistanceHitDetector = Physics2D.Linecast(frontCenterSensorStart.position, frontCenterSensorStart.position + frontCenterSensorStart.up * sensorLength, 1 << LayerMask.NameToLayer("InnerBoundary"));
        fraction = frontCenterDistanceHitDetector.collider != null ? frontCenterDistanceHitDetector.fraction : 1.0f;    //On hit, fraction is based on RayCast. But if no hit occured, it is 100%, meaning no obsticale along the distance of sensor.
        Green    = (byte)(fraction * 255);
        Red      = (byte)(255 - Green);
        if (levelManager.isDebugMode)
        {
            frontCenterLineDrawer.DrawLineInGameView(frontCenterSensorStart.position, frontCenterSensorStart.position + frontCenterSensorStart.up * sensorLength, new Color32(Red, Green, Blue, Alpha));
        }
        else
        {
            frontCenterLineDrawer.Destroy();
        }
        //if (frontCenterHitDetection)
        //{
        //    Debug.Log(frontCenterDistanceHitDetector.fraction);
        //}
        sensorData[0] = Functions.Map(fraction, 0.0, 1.0, -1.0, 1.0);

        //Front Right Angle Hit Detection
        frontRightDistanceHitDetector = Physics2D.Linecast(frontRightAngleSensorStart.position, frontRightAngleSensorStart.position + frontRightAngleSensorStart.up * sensorLength, 1 << LayerMask.NameToLayer("InnerBoundary"));
        fraction = frontRightDistanceHitDetector.collider != null ? frontRightDistanceHitDetector.fraction : 1.0f;
        Green    = (byte)(fraction * 255);
        Red      = (byte)(255 - Green);
        if (levelManager.isDebugMode)
        {
            frontRightLineDrawer.DrawLineInGameView(frontRightAngleSensorStart.position, frontRightAngleSensorStart.position + frontRightAngleSensorStart.up * sensorLength, new Color32(Red, Green, Blue, Alpha));
        }
        else
        {
            frontRightLineDrawer.Destroy();
        }
        //if (frontRightHitDetection)
        //{
        //    //Debug.Log(frontRightDistanceHitDetector.fraction);
        //}
        sensorData[1] = Functions.Map(fraction, 0.0, 1.0, -1.0, 1.0);

        //Front Left Angle Hit Detection
        frontLeftDistanceHitDetector = Physics2D.Linecast(frontLeftAngleSensorStart.position, frontLeftAngleSensorStart.position + frontLeftAngleSensorStart.up * sensorLength, 1 << LayerMask.NameToLayer("InnerBoundary"));
        fraction = frontLeftDistanceHitDetector.collider != null ? frontLeftDistanceHitDetector.fraction : 1.0f;
        Green    = (byte)(fraction * 255);
        Red      = (byte)(255 - Green);
        if (levelManager.isDebugMode)
        {
            frontLeftLineDrawer.DrawLineInGameView(frontLeftAngleSensorStart.position, frontLeftAngleSensorStart.position + frontLeftAngleSensorStart.up * sensorLength, new Color32(Red, Green, Blue, Alpha));
        }
        else
        {
            frontLeftLineDrawer.Destroy();
        }
        //if (frontLeftHitDetection)
        //{
        //    Debug.Log(frontLeftDistanceHitDetector.fraction);
        //}
        sensorData[2] = Functions.Map(fraction, 0.0, 1.0, -1.0, 1.0);

        //Back Center Hit Detection
        backCenterDistanceHitDetector = Physics2D.Linecast(backCenterSensorStart.position, backCenterSensorStart.position + backCenterSensorStart.up * sensorLength, 1 << LayerMask.NameToLayer("InnerBoundary"));
        fraction = backCenterDistanceHitDetector.collider != null ? backCenterDistanceHitDetector.fraction : 1.0f;
        Green    = (byte)(fraction * 255);
        Red      = (byte)(255 - Green);
        if (levelManager.isDebugMode)
        {
            backCenterLineDrawer.DrawLineInGameView(backCenterSensorStart.position, backCenterSensorStart.position + backCenterSensorStart.up * sensorLength, new Color32(Red, Green, Blue, Alpha));
        }
        else
        {
            backCenterLineDrawer.Destroy();
        }
        //if (backCenterHitDetection)
        //{
        //    Debug.Log(backCenterDistanceHitDetector.fraction);
        //}
        sensorData[3] = Functions.Map(fraction, 0.0, 1.0, -1.0, 1.0);

        //Back Right Angle Hit Detection
        backRightDistanceHitDetector = Physics2D.Linecast(backRightAngleSensorStart.position, backRightAngleSensorStart.position + backRightAngleSensorStart.up * sensorLength, 1 << LayerMask.NameToLayer("InnerBoundary"));
        fraction = backRightDistanceHitDetector.collider != null ? backRightDistanceHitDetector.fraction : 1.0f;
        Green    = (byte)(fraction * 255);
        Red      = (byte)(255 - Green);
        if (levelManager.isDebugMode)
        {
            backRightLineDrawer.DrawLineInGameView(backRightAngleSensorStart.position, backRightAngleSensorStart.position + backRightAngleSensorStart.up * sensorLength, new Color32(Red, Green, Blue, Alpha));
        }
        else
        {
            backRightLineDrawer.Destroy();
        }
        //if (backRightHitDetection)
        //{
        //    Debug.Log(backRightDistanceHitDetector.fraction);
        //}
        sensorData[4] = Functions.Map(fraction, 0.0, 1.0, -1.0, 1.0);

        //Back Left Angle Hit Detection
        backLeftDistanceHitDetector = Physics2D.Linecast(backLeftAngleSensorStart.position, backLeftAngleSensorStart.position + backLeftAngleSensorStart.up * sensorLength, 1 << LayerMask.NameToLayer("InnerBoundary"));
        fraction = backLeftDistanceHitDetector.collider != null ? backLeftDistanceHitDetector.fraction : 1.0f;
        Green    = (byte)(fraction * 255);
        Red      = (byte)(255 - Green);
        if (levelManager.isDebugMode)
        {
            backLeftLineDrawer.DrawLineInGameView(backLeftAngleSensorStart.position, backLeftAngleSensorStart.position + backLeftAngleSensorStart.up * sensorLength, new Color32(Red, Green, Blue, Alpha));
        }
        else
        {
            backLeftLineDrawer.Destroy();
        }
        //if (backLeftDistanceHitDetector)
        //{
        //    Debug.Log(backLeftDistanceHitDetector.fraction);
        //}
        sensorData[5] = Functions.Map(fraction, 0.0, 1.0, -1.0, 1.0);

        return(sensorData);
    }