private void engaged() { enemyDistance = getDistance(this.gameObject, current_target); rigidBody.velocity = ((current_target.transform.position - this.transform.position) / enemyDistance) * speed; if (enemyDistance < attackRadius) { attackScript.attack(attackTime, this.gameObject); } }
// Update is called once per frame void Update() { roomID = new Vector2Int((Mathf.FloorToInt(this.transform.position.x) / 10) - 20, (Mathf.FloorToInt(this.transform.position.y) / 10) - 19); if (awareOfPlayer && !dazed) { playerPos = new Vector2(player.transform.position.x, player.transform.position.y); toPlayer = playerPos - new Vector2(this.transform.position.x, this.transform.position.y); // vv future stuff vv // Conditional movement // once near enough to the player to attack, then attack!!! // asleep enemies will be woken on contact (movement by player collision) // ^^ future stuff ^^ mag = Mathf.Sqrt(toPlayer.x * toPlayer.x + toPlayer.y * toPlayer.y); // magnitude of the vector toPlayerUnit = toPlayer / mag; if (health <= (maxHealth / 10)) { rigidBody.velocity = -toPlayerUnit; } else if (Mathf.Abs(toPlayer.x) > stopDistance || Mathf.Abs(toPlayer.y) > stopDistance) { rigidBody.velocity = toPlayerUnit; } else { attackScript.attack(attackTime, this.gameObject); } hitBoxTransform.localPosition = new Vector3(0, 0, 0); } else { if (playerScript.roomID == roomID) { awareOfPlayer = true; } } if (health <= 0) { //DIEEEEEEEE (death animation) Debug.Log("Ded m8"); Destroy(this.gameObject); } attackTime -= Time.deltaTime; }
void FixedUpdate() { if (Input.GetKey(KeyCode.R)) { mainCamera.orthographicSize = 15f; } else { mainCamera.orthographicSize = 2.5f; } // Get RoomID of player roomID = new Vector2Int((Mathf.FloorToInt(this.transform.position.x) / 10) - 20, (Mathf.FloorToInt(this.transform.position.y) / 10) - 19); // Animation control //animAttacking = playerAnim.GetBool("isAttacking"); animRolling = playerAnim.GetBool("isRolling"); //Gets axis values for movement float moveHor = Input.GetAxis("Horizontal"); float moveVer = Input.GetAxis("Vertical"); // Flip the character based off of movement Vector3 newScale = transform.localScale; if (Input.GetAxisRaw("Horizontal") != 0) { newScale.x = Input.GetAxisRaw("Horizontal") * -1; } transform.localScale = newScale; /*if (Input.GetAxis ("Horizontal") != 0) { * transform.localScale = new Vector3 (Mathf.Round(Input.GetAxisRaw ("Horizontal")), 0f, 0f); * }*/ //apply movement as velocity (keeps collisions going with no acceleration) if (canMove) { movement = new Vector2(moveHor, moveVer); playerAnim.SetFloat("speed", movement.magnitude); } rigidBody.velocity = movement * moveSpeed; //Attack if (Input.GetAxisRaw("Attack") == 1) { attackScript.attack(attackTime, this.gameObject); } //Roll if (canRoll && !attacking && Input.GetAxisRaw("Roll") == 1 && animRolling == false && stamina > rollStamina && rigidBody.velocity.magnitude > 0) // fix co-routine { playerRoll(); rigidBody.AddForce(rigidBody.velocity * rollMod, ForceMode2D.Impulse); } if (playerHealth <= 0) { die(); } //Set UI Text staminaText.text = "Stamina: " + stamina.ToString(); healthText.text = "Health: " + playerHealth.ToString(); coinText.text = "Coins: " + coins.ToString(); essenceText.text = "Essence: " + essences.ToString(); attackTime -= Time.deltaTime; }