// Update is called once per frame void Update() { if (!trapped) { RaycastHit2D seePlayer = Physics2D.Raycast(transform.position, player.transform.position - transform.position, 2.5f); // line of sight looking for player if (seePlayer) { canSeePlayer = seePlayer.transform.gameObject == player; if (canSeePlayer) { float xDiff = transform.position.x - player.transform.position.x; facingRight = xDiff > 0; xDiff = xDiff / Mathf.Abs(xDiff); rb.velocity = new Vector2(xDiff * 4, rb.velocity.y); } } else if (cg.canJump()) { rb.velocity = new Vector2(0, rb.velocity.y); } if (rb.velocity.x > 0) { sr.flipX = false; } else if (rb.velocity.x < 0) { sr.flipX = true; } anim.SetBool("running", cg.canJump() && Mathf.Abs(rb.velocity.x) > 0.01); anim.SetBool("jumping", !cg.canJump()); } }
// Update is called once per frame void Update() { if (health <= 0) { SceneManager.LoadScene("Lose"); } bool canJump = cg.canJump(); if (beingKbd) { time += Time.deltaTime; if (time >= invinTime) { beingKbd = false; } } float horVel = 0; if (pitJumps > 0) { if (Input.GetKeyDown("space")) { pitJumps -= 1; } if (pitJumps == 0) { sr.sprite = normalSprite; } } else { horVel = vel * Input.GetAxis("Horizontal"); if (Input.GetKeyDown("up") && canJump) { sound.clip = jump; sound.Play(); rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse); } anim.SetBool("running", canJump && Input.GetAxis("Horizontal") != 0); anim.SetBool("jumping", !canJump); anim.SetBool("hurt", beingKbd); rb.velocity = new Vector2(horVel, rb.velocity.y); if (horVel < 0) { sr.flipX = true; } else if (horVel > 0) { sr.flipX = false; } } }
void OnTriggerStay2D(Collider2D other) { switch (other.gameObject.tag) { case "cage": case "pit": if (cg.canJump() && monster.GetComponent <Monster>().canSeePlayer) { monster.GetComponent <Rigidbody2D> ().AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse); } break; } }