/** * Idle state * * In this state, the Friendly will wait to spot a player, and then it will go to its attack state. * Patroling Friendlys will resume to patrol after the player is out of reach */ private void Idle() { // Will set 'playerSpotted' to true if spotted IsPlayerInRange(); if (playerSpotted) { _state = FriendlyState.wait; } }
/** * Patrol script for Friendly, * will walk untill the collidingWithWall linecast hits a collider, then walk the other way * or (if checked) will detect if the Friendly is to hit the edge of a platform */ private void Patrol() { GetComponent <Rigidbody2D>().velocity = new Vector2(walkSpeed, GetComponent <Rigidbody2D>().velocity.y); FaceDirectionOfWalking(); collidingWithWall = Physics2D.Linecast( new Vector2((this.transform.position.x + collideDistance), (this.transform.position.y - (GetComponent <SpriteRenderer>().bounds.size.y / 4))), new Vector2((this.transform.position.x + collideDistance), (this.transform.position.y + (GetComponent <SpriteRenderer>().bounds.size.y / 2))), ~( (1 << this.gameObject.layer) + (1 << LayerMask.NameToLayer("UI")) + (1 << LayerMask.NameToLayer("EnemeyProjectile")) + (1 << LayerMask.NameToLayer("PlayerProjectile")) + (1 << LayerMask.NameToLayer("Foreground")) ) // Collide with all layers, except itself ); if (edgeDetection) { collidingWithGround = Physics2D.Linecast( new Vector2(this.transform.position.x, this.transform.position.y), new Vector2((this.transform.position.x + collideDistance), (this.transform.position.y - (GetComponent <SpriteRenderer>().bounds.size.y))), ~( (1 << this.gameObject.layer) + (1 << LayerMask.NameToLayer("UI")) + (1 << LayerMask.NameToLayer("EnemeyProjectile")) + (1 << LayerMask.NameToLayer("PlayerProjectile")) + (1 << LayerMask.NameToLayer("Foreground")) ) // Collide with all layers, except itself ); } else { collidingWithGround = true; } if (collidingWithWall || !collidingWithGround) { // Debug.Log(this.name + " hit a wall, now walking the other way."); walkSpeed *= -1; collideDistance *= -1; } if (!isBlinded) { // Will set 'playerSpotted' to true if spotted IsPlayerInRange(); if (playerSpotted) { _state = FriendlyState.wait; } } }
/** * Friendly will stop to face the player. * * StopPatrolDelay is to prevent the friendly patrol to stop moving * every time the player enters its spotrange. * This is because in some occations it would cause the friendly patrol * to 'creep up' to players. Hopefully it looks a bit more natural now. */ private void Wait() { if (type == FriendlyType.patrol) { GetComponent <Rigidbody2D>().velocity = new Vector2(0, 0); } FacePlayer(); // Will set 'playerSpotted' to true if spotted IsPlayerInRange(); if (!playerSpotted) { _state = FriendlyState.idle; if (type == FriendlyType.patrol) { if (!delayCoroutineStarted) { StartCoroutine(StopPatrolDelay()); } } } }