// Update is called once per frame void Update() { playerInView = view.InView(transform) && view.InDistance(transform, xDistanceMinus, yDistanceMinus); // If player in view and not dead, stop moving and face player if (playerHP.CurrentHP > 0 && playerInView) { // If move while facing is false, stop moving and set animations to go to idle if (!moveWhileFacing) { anim.SetBool("StopMove", true); // Need to set a trigger for animations to go into idle animation state movement.StopMoving(); } // Activate any attached shooting behaviour while in view if (shootBehaviourScript != null && !shootBehaviourScript.enabled) { shootBehaviourScript.enabled = true; } //Swap facing x direction to the player if ((transform.localScale.x > 0 && transform.position.x < player.transform.position.x) || (transform.localScale.x < 0 && transform.position.x > player.transform.position.x)) { transform.localScale = new Vector2(-transform.localScale.x, transform.localScale.y); } } else { if (!moveWhileFacing) { anim.SetBool("StopMove", false); } // If not shooting, resume moving, if mid shoot animation, don't resume moving if (!moveWhileFacing && !anim.GetCurrentAnimatorStateInfo(0).IsName("Shoot")) { movement.ResumeMoving(); } // Deactivate any attached shooting behaviour until player comes back into view if (shootBehaviourScript != null && shootBehaviourScript.enabled) { shootBehaviourScript.enabled = false; } } }
void Update() { // Start toggling on hit shoot behaviour on and off once player comes into view playerInView = view.InView(transform); if (!startingOnHit && !onHitShoot && playerInView && !wasInView && Time.time > lastTrigger) { lastTrigger = Time.time + Random.Range(0, maxDelay); } wasInView = playerInView; if (!startingOnHit && playerInView && Time.time > lastTrigger) { startingOnHit = true; lastTrigger = Time.time + maxDelay; anim.Play("StartOnHitShoot"); } }
// Update is called once per frame void Update() { // The first time player comes into view (or the first time this object comes into player's camera view), reset lastShot by a random delay if (!playerInView && view.InView(transform) && view.InDistance(transform)) { // Avoid rapid/irregular shots by going repeatedly going in and out of view if (Time.time > lastShot) { lastShot = Time.time + Random.Range(shootBehaviour.minShootDelay, shootBehaviour.maxShootDelay); } } playerInView = view.InView(transform) && view.InDistance(transform); // If above is restricted, means cannot turn and shoot player if player is above if (shootBehaviour.restrictAbove && player.transform.position.y > transform.position.y) { wasAbove = true; return; } else if (wasAbove) { // If was above, reset shooting behaviour wasAbove = false; if (Time.time > lastShot) { lastShot = Time.time + Random.Range(shootBehaviour.minShootDelay, shootBehaviour.maxShootDelay); } } // If below is restricted, means cannot turn and shoot player if player is below // Added offset of 2 so player doesn't have to be directly above if (shootBehaviour.restrictBelow && player.transform.position.y < (transform.position.y - 2)) { wasBelow = true; return; } else if (wasBelow) { // If was below, reset shooting behaviour wasBelow = false; if (Time.time > lastShot) { lastShot = Time.time + Random.Range(shootBehaviour.minShootDelay, shootBehaviour.maxShootDelay); } } // Rotate towards and face the player if (playerInView && facePlayer) { Vector3 target = player.transform.position - transform.position; // In case enemy flips direction, this object will also flip so need to readjust target so that this object rotates and faces player if (transform.parent.localScale.x < 0) { target = -target; } float angle = Mathf.Atan2(target.y, target.x) * Mathf.Rad2Deg; Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward); transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * shootBehaviour.turnSpeed); } // Shoot player if (playerInView && facePlayer && Time.time > lastShot) { StartShoot(); } }
void Update() { playerInDist = Vector3.Distance(transform.position, player.transform.position) <= aggroDist && playerInView.InView(transform); // If player in dash distance and not dead, start up aggro if haven't already if (playerHP.CurrentHP > 0 && !isStartingAggro && playerInDist) { isStartingAggro = true; StartAggro(); } }
// Update is called once per frame void Update() { playerInView = view.InView(transform) && view.InDistance(transform, xDistanceMinus, yDistanceMinus); // Face player when on ground and aggro if (isStartingAggro && collisions.below) { //Swap facing x direction to the player if ((transform.localScale.x > 0 && transform.position.x < player.transform.position.x) || (transform.localScale.x < 0 && transform.position.x > player.transform.position.x)) { transform.localScale = new Vector2(-transform.localScale.x, transform.localScale.y); } } // If player in view and not dead, start up aggro if haven't already, waits until enemy is on ground so it doesn't start up mid jump if (playerHP.CurrentHP > 0 && playerInView && !isStartingAggro && !isAggro && collisions.below) { isStartingAggro = true; StartAggro(); } else if (!playerInView || playerHP.CurrentHP <= 0) // Reset aggro properties if player dies or goes out of view { // Reset aggro properties and animation from aggro or starting aggro if was aggro/starting aggro (for example, if player went out of view during aggro/start aggro) if (isAggro || isStartingAggro) { anim.SetBool("isAggro", false); isStartingAggro = false; isAggro = false; } } // Do not move while starting aggro if (isStartingAggro) { return; } // If aggro, adjust xMoveMultiplier sign so that enemy jump towards player, else jump back and forth if (isAggro && collisions.below) { if ((player.transform.position.x > transform.position.x && Mathf.Sign(xMoveMultiplier) < 0) || (player.transform.position.x < transform.position.x && Mathf.Sign(xMoveMultiplier) > 0)) { xMoveMultiplier *= -1; } } else if (collisions.below) { xMoveMultiplier = -xMoveMultiplier; } // Face direction enemy is jumping/moving in transform.localScale = new Vector2(-Mathf.Sign(xMoveMultiplier), transform.localScale.y); // Check if path is blocked by wall if ((Mathf.Sign(xMoveMultiplier) > 0 && collisions.right) || (Mathf.Sign(xMoveMultiplier) < 0 && collisions.left)) { velocity.x = 0; } // Reset velocity if hits ground or ceiling if (collisions.above || collisions.below) { velocity.y = 0; // Apply jump velocity if enemy is touching ground if (collisions.below) { velocity.y = jumpVelocityMultiplier; // Check if path is blocked by wall if ((Mathf.Sign(xMoveMultiplier) > 0 && collisions.right) || (Mathf.Sign(xMoveMultiplier) < 0 && collisions.left)) { velocity.x = 0; } else { velocity.x = xMoveMultiplier; } } } // Apply gravity force ApplyGravity(); // Apply movement to enemy based on velocity * Time.deltaTime ApplyMovement(velocity * Time.deltaTime); }