protected void FixedUpdate() { base.FixedUpdate(); if (Target == null) { return; } if (state == AIState.Follow || state == AIState.Charge || state == AIState.Cooldown) { faceDir = Mathf.RoundToInt(Mathf.Sign(Target.position.x - transform.position.x)); targetSpeed = MoveSpeed; } else if (state == AIState.Attack) { targetSpeed = LungeSpeed; } else if (state == AIState.Patrol) { targetSpeed = PatrolSpeed; faceDir = Mathf.RoundToInt(Mathf.Sign(Mathf.Sin(Time.time * PatrolSwitchRate))); } else if (state == AIState.Idle) { targetSpeed = 0.0f; } // Separate checks for logic that only applies to individual states if (state == AIState.Follow) { targetSpeed = MoveSpeed; } else if (state == AIState.Charge) { targetSpeed = MoveSpeed * 0.1f; } else if (state == AIState.Cooldown) { targetSpeed = 0.0f; } targetSpeed *= GetSpeed().GetValue(); frb.Accelerate(Vector3.right * (targetSpeed * faceDir - frb.GetVelocity().x) * Acceleration); // Accelerate toward the target transform.rotation = Quaternion.LookRotation(Vector3.forward * faceDir, Vector3.up); // Particles to show charge and attack states (for testing) if (chargeParticles != null) { if (state == AIState.Charge) { if (!chargeParticles.isPlaying) { chargeParticles.Play(); } } else if (chargeParticles.isPlaying) { chargeParticles.Stop(); } } if (attackParticles != null) { if (state == AIState.Attack) { if (!attackParticles.isPlaying) { attackParticles.Play(); } } else if (attackParticles.isPlaying) { attackParticles.Stop(); } } }
protected void FixedUpdate() { base.FixedUpdate(); if (Target == null) { return; } if (state == AIState.Follow || state == AIState.Charge || state == AIState.Attack || state == AIState.Cooldown) { // Rotation assumes that local up direction is forward lookDir = Vector3.Slerp(lookDir, Target.position - transform.position, RotationRate * Time.fixedDeltaTime); // Rotate to face target targetSpeed = MoveSpeed; frb.Accelerate((transform.position - Target.position).normalized * Mathf.Min(GetAvoidCloseness(), AvoidAccelLimit) * Acceleration * AvoidAmount); // Acceleration to keep away from the target } else if (state == AIState.Patrol) { //For now, patrolling just moves the drone in a circle lookDir = Quaternion.AngleAxis(PatrolRotateRate * patrolDir * Time.fixedDeltaTime, Vector3.forward) * lookDir; targetSpeed = PatrolSpeed; } else if (state == AIState.Idle) { targetSpeed = 0.0f; } targetSpeed *= GetSpeed().GetValue(); frb.Accelerate(transform.up * (targetSpeed - frb.GetVelocity().magnitude *Mathf.Clamp01(Vector3.Dot(transform.up, frb.GetVelocity().normalized))) * Acceleration); // Accelerate toward the target frb.Accelerate(-transform.right * frb.GetVelocity().magnitude *Vector3.Dot(transform.right, frb.GetVelocity().normalized) * SideDecel); // Deceleration to prevent sideways movement transform.rotation = Quaternion.LookRotation(Vector3.forward, lookDir); // Actual rotation // Particles to show charge and attack states (for testing) if (chargeParticles != null) { if (state == AIState.Charge) { if (!chargeParticles.isPlaying) { chargeParticles.Play(); } } else if (chargeParticles.isPlaying) { chargeParticles.Stop(); } } if (attackParticles != null) { if (state == AIState.Attack) { if (!attackParticles.isPlaying) { attackParticles.Play(); } } else if (attackParticles.isPlaying) { attackParticles.Stop(); } } if (state == AIState.Attack) { if (attackLaunchTime <= 0) { attackLaunchTime = AttackLaunchInterval; if (AttackProjectile != null) { Instantiate(AttackProjectile, transform.position, transform.rotation); } } } else { attackLaunchTime = 0.0f; } attackLaunchTime = Mathf.Max(0.0f, attackLaunchTime - Time.fixedDeltaTime); }
private void FixedUpdate() { float moveInput = (Input.GetKey(KeyCode.RightArrow) ? 1.0f : 0.0f) - (Input.GetKey(KeyCode.LeftArrow) ? 1.0f : 0.0f); frb.Accelerate(Vector3.right * (moveInput * MoveSpeed - frb.GetVelocity().x) * MoveAcceleration); }