Beispiel #1
0
        protected void LookAtNearestEnemy(List <GameObject> enemies)
        {
            if (enemies.Count < 1)
            {
                return;
            }

            float minDistance       = DirectionAndDistanceCalculator.CalculateDistance(player.transform.Get2DPosition(), enemies[0].transform.Get2DPosition());
            int   nearestEnemyIndex = 0;

            for (int i = 1; i < enemies.Count; i++)
            {
                if (DirectionAndDistanceCalculator.CalculateDistance(player.transform.Get2DPosition(), enemies[i].transform.Get2DPosition()) < minDistance)
                {
                    minDistance       = DirectionAndDistanceCalculator.CalculateDistance(player.transform.Get2DPosition(), enemies[i].transform.Get2DPosition());
                    nearestEnemyIndex = i;
                }
            }

            Vector2 enemyPos = enemies[nearestEnemyIndex].transform.Get2DPosition();

            if ((player.transform.Get2DPosition().IsAtLeftOf(enemyPos) && !player.IsFacingRight) || (!player.transform.Get2DPosition().IsAtLeftOf(enemyPos) && player.IsFacingRight))
            {
                player.Flip();
            }
        }
Beispiel #2
0
        // todo : random next patrol point
        protected void Patrol()
        {
            if (movementHandler.PathEmpty())
            {
                PathRequestManager.RequestPath(enemy.transform.position, enemy.PatrolWayPoints[nextWaypointIndex].Get2DPosition(), enemy.gameObject, enemy.UnwalkableLayer, OnPathFound);
            }
            if (patrolWaitTimer > 0)
            {
                patrolWaitTimer -= Time.deltaTime;
                return;
            }

            FlipIfNeeded();
            enemy.Anim.SetBool(enemy.WalkingAnimationBool, true);
            movementHandler.MoveAlongPath();
            if (DirectionAndDistanceCalculator.CalculateDistance(enemy.transform.Get2DPosition(), enemy.PatrolWayPoints[nextWaypointIndex].Get2DPosition()) < GameManager.Instance.aiReachingPrecision)
            {
                nextWaypointIndex = (nextWaypointIndex + 1) % enemy.PatrolWayPoints.Length;
                PathRequestManager.RequestPath(enemy.transform.position, enemy.PatrolWayPoints[nextWaypointIndex].Get2DPosition(), enemy.gameObject, enemy.UnwalkableLayer, OnPathFound);
                patrolWaitTimer = enemy.PatrolDelay;
                enemy.Anim.SetBool(enemy.WalkingAnimationBool, false);
            }
        }
Beispiel #3
0
 protected bool PlayerInChasingRange()
 {
     return(DirectionAndDistanceCalculator.CalculateDistance(enemy.transform.Get2DPosition(), enemy.Player.Get2DPosition()) <= enemy.chasingRange);
 }
    public bool CheckObstacles(GameObject go, Vector2 startPos, Vector2 targetPos, LayerMask unwalkableLayer, out Vector2 obstaclePosition)
    {
        BoxCollider2D boxCollider = go.GetComponent <BoxCollider2D>();
        Vector2       unitScale   = new Vector2(Mathf.Abs(go.transform.localScale.x), Mathf.Abs(go.transform.localScale.y));
        RaycastHit2D  hit;
        Vector2       rayOrigin;
        Vector2       boxColliderCenter = new Vector2(startPos.x - boxCollider.offset.x * unitScale.x, startPos.y + boxCollider.offset.y * unitScale.y);
        float         xGap = (boxCollider.size.x * unitScale.x) / 2;
        float         yGap = (boxCollider.size.y * unitScale.y) / 2;
        Vector2       rayCastingCenter;
        Vector2       direction = DirectionAndDistanceCalculator.CalculateDirection(startPos, targetPos);
        float         distance  = DirectionAndDistanceCalculator.CalculateDistance(startPos, targetPos);

        if (direction.x == 0)    // walking vertically
        {
            if (direction.y > 0) // going up
            {
                rayCastingCenter = boxColliderCenter + new Vector2(0, yGap);
            }
            else // going down
            {
                rayCastingCenter = boxColliderCenter - new Vector2(0, yGap);
            }

            for (int i = -1; i < 2; i++)
            {
                rayOrigin = rayCastingCenter + new Vector2(i * xGap, 0);
                hit       = Physics2D.Raycast(rayOrigin, direction, distance, unwalkableLayer);
                Debug.DrawLine(rayOrigin, rayOrigin + direction);
                if (hit.collider != null)
                {
                    //Debug.Log(go.name + " is gonna hit an obstacle !");
                    obstaclePosition = hit.point;
                    return(true);
                }
            }
        }
        else
        {
            if (direction.x > 0)
            {
                rayCastingCenter = boxColliderCenter + new Vector2(xGap, 0);
            }
            else
            {
                rayCastingCenter = boxColliderCenter - new Vector2(xGap, 0);
            }
            for (int i = -1; i < 2; i++)
            {
                rayOrigin = rayCastingCenter + new Vector2(0, i * yGap);
                hit       = Physics2D.Raycast(rayOrigin, direction, distance, unwalkableLayer);
                Debug.DrawLine(rayOrigin, rayOrigin + direction);
                if (hit.collider != null)
                {
                    //Debug.Log(go.name + " is gonna hit an obstacle !");
                    obstaclePosition = hit.point;
                    return(true);
                }
            }

            if (direction.y != 0)
            {
                Vector2 lastRayOrigin = boxColliderCenter + new Vector2(-xGap * Math.Sign(direction.x), yGap * Math.Sign(direction.y));
                hit = Physics2D.Raycast(lastRayOrigin, direction, distance, unwalkableLayer);
                Debug.DrawLine(lastRayOrigin, lastRayOrigin + direction);
                if (hit.collider != null)
                {
                    //Debug.Log(go.name + " is gonna hit an obstacle !");
                    obstaclePosition = hit.point;
                    return(true);
                }
            }
        }

        obstaclePosition = new Vector2(short.MinValue, short.MinValue);
        return(false);
    }