private void OnEnterStrangerState(int previousStateId) { if (previousStateId == (int)Relationship.Inactive) { gameObject.SetActive(true); // Give the NPC with random values so it behaves differently fromt other NPCs nextDirectionChangeTime = GetNextDirectionChangeTime(); currentDirection = UnityExtensions.GetRandomUnitVector(); currentSpeed = Random.Range(minSpeed, maxSpeed); } }
// All physics calculations should always be done in FixedUpdate private void FixedUpdate() { if (Time.time > pauseMovementEndTime) { // Resume movement if (rb.isKinematic) { rb.isKinematic = false; rb.velocity = savedVelocity; } // Get the new direction if (Time.time > nextDirectionChangeTime) { currentSpeed = Random.Range(minSpeed, maxSpeed); currentDirection = UnityExtensions.GetRandomUnitVector(); nextDirectionChangeTime = GetNextDirectionChangeTime(); } // Call each of the state's FixedUpdates stateMachine.FixedUpdate(); // Clamp the velocity if (rb.velocity.sqrMagnitude > maxSpeed * maxSpeed) { rb.velocity = Vector2.ClampMagnitude(rb.velocity, maxSpeed); } } else { // Pause movement savedVelocity = rb.velocity; rb.velocity = Vector2.zero; rb.isKinematic = true; } }