/** Update is called once per frame. * First, check LoS on penguin for disguise purposes. * Second, chase penguin if needed. * Otherwise, wander randomly. * Finally, update sprite and/or animation. **/ void Update() { if (transform.FindChild("HumanSprite").GetComponent <SpriteRenderer>().isVisible) { RaycastHit2D hit = Physics2D.Raycast(transform.position, facing_right ? Vector2.right : Vector2.left, Mathf.Infinity, 1 << LayerMask.NameToLayer("Player")); if (hit.collider) { // We can see the player. PlatformerPlayerMovement player = hit.collider.GetComponent <PlatformerPlayerMovement>(); if (player.sliding || player.stealing || player.jumping) { // Is that...? disguise.DecreaseDisguise(); } chasing = disguise.disguise < 0; } } if (chasing) { destination = player.transform.position.x; body.velocity = new Vector2(destination > transform.position.x ? run_speed : -run_speed, body.velocity.y); } else { // Head towards goal. body.velocity = new Vector2(destination > transform.position.x ? speed : -speed, body.velocity.y); if (AtGoal() || (!override_destination && Time.time > reconsider_goal_time)) { // If we're here, stop. body.velocity = new Vector2(); // If destination is not overriden, select a new destination. if (!override_destination) { destination = transform.position.x + (Random.value * 10 - 5); reconsider_goal_time = Time.time + 10; } } } if (body.velocity.x > 0) { facing_right = true; } if (body.velocity.x < 0) { facing_right = false; } GetComponentInChildren <SpriteRenderer> ().flipX = !facing_right; }
void OnTriggerStay2D(Collider2D c) { if (collision_enabled && c.gameObject.tag == "Player") { if (c.gameObject.GetComponent <Rigidbody2D> ().velocity.magnitude > 0.1) { disguise.disguise -= bump_penalty * Time.deltaTime; disguise.DecreaseDisguise(); } if (movement.chasing) { // TODO Remove fish. // TODO Go to next day. GameObject.Find("GameManager").GetComponent <GameManagerScript>().NextDay(); } } }