Пример #1
0
    // Update is called once per frame
    void Update()
    {
        base.CheckForDeactivateStateChange();

        EnemyEngine enemyEngine = GetComponent <EnemyEngine>();
        Vector3     target      = enemyEngine.GetTargetPosition();
        float       distance    = 0;

        switch (sensorType)
        {
        case RangeSensorType.Horizontal:
            distance = Mathf.Abs(transform.position.x - target.x);
            break;

        case RangeSensorType.Vertical:
            distance = Mathf.Abs(target.y - transform.position.y);
            break;

        case RangeSensorType.DistanceBased:
            distance = Vector2.Distance(transform.position, target);
            break;
        }

        if (!sensorActive && distance < detectionRange)
        {
            OnSensorActive();
        }

        else if (sensorActive && distance > detectionRange)
        {
            OnSensorExit();
        }
    }
Пример #2
0
    /// <summary>
    /// Rotates the entity to face the current target.
    /// Common to all liner components.
    /// </summary>
    protected void RotateToTarget()
    {
        // Get Angle in Radians
        float AngleRad = Mathf.Atan2(enemyEngine.GetTargetPosition().y - transform.position.y, enemyEngine.GetTargetPosition().x - transform.position.x);
        // Get Angle in Degrees
        float AngleDeg = (180 / Mathf.PI) * AngleRad;

        // Rotate Object

        if (enemyEngine.GetTargetPosition().x > transform.position.x)
        {
            transform.rotation = (Quaternion.Euler(0, 180, -AngleDeg));
        }
        else
        {
            transform.rotation = Quaternion.Euler(0, 0, AngleDeg - 180);
        }
    }