// Update is called once per frame void Update() { //Has no target, try and find new target if (player[0] == null) { Physics.OverlapSphereNonAlloc(rb.position, aggroRange, player, 1 << 8); } //Got a target if (player[0] != null) { //Outside of range, reset target if (Vector3.Distance(player[0].transform.position, rb.position) > chaseRange) { player[0] = null; } else { Debug.Log("Chasing"); kinMotor.Move((player[0].transform.position - rb.position)); //Close enough to attack if (Vector3.Distance(player[0].transform.position, rb.position) <= attackRange) { if (animator != null) { animator.SetTrigger("Attack"); } } } } }
/* * Physics Update. */ private void FixedUpdate() { speed = input; bool justLanded = becameGrounded && !kinMotor.IsGrounded; bool justFell = (!kinMotor.IsJumping || !jump) && !becameGrounded && kinMotor.IsGrounded; becameGrounded = kinMotor.IsGrounded; //Can check here the landing frame. if (justLanded) { animator.SetBool(JumpFallAnimationState, false); } //Can check here the falling frame. if (justFell) { animator.SetBool(JumpFallAnimationState, true); } /* * If pressed lock, check for enemies close by and lock camera to closest */ if (pressedLocking) { //Reset variables when clicking to lock closestEnemy = null; pressedLocking = false; //Try to lock to the closest enemy isLocked = LockCameraToClosestEnemy(); } /* * If is in locked combat but moves too far away, disengage camera lock */ if (isLocked) { float distance = Vector3.Distance(closestEnemy.transform.position, rb.position); if (distance > 5f) { isLocked = false; closestEnemy = null; myCamera.LockCameraOnTarget(null); } } //Set arms animation to locked animator.SetBool(FightStanceAnimationState, isLocked); /* * Dodge press; */ if (dodge) { dodge = false; isDodging = true; } /* * Check speed magnitude from leg part. * Dodge / Sprint / Normal run */ if (isDodging) { timerDodge += Time.deltaTime; //timerDodge starts as 0 and ends at 200 speed *= (bodyParts.LegPart.FlashStepSpeed - (timerDodge * timerDodge * 10 * 40)); if (timerDodge >= .05f) { isDodging = false; timerDodge = 0f; } } else if (isSprinting) { speed *= bodyParts.LegPart.RunningSpeed; } else { speed *= bodyParts.LegPart.MovementSpeed; } //Set animation type for walking/sprinting if (speed.magnitude > 0) { if (isSprinting) { animator.SetBool(SprintingAnimationState, true); } else { animator.SetBool(WalkingAnimationState, true); animator.SetBool(SprintingAnimationState, false); } } else { animator.SetBool(WalkingAnimationState, false); animator.SetBool(SprintingAnimationState, false); } kinMotor.Move(speed); bodyParts.SetMovementState(speed, kinMotor.IsJumping, isSprinting && speed.magnitude > 0); }