Exemple #1
0
        public void UpdateForTick()
        {
            if (isPaused)
            {
                return;
            }

            if (_currentBehaviors.Count > 0)
            {
                _totalActiveBehaviors = 0;
                BaseBehavior b = null;
                // Get total active behaviors
                for (int i = 0; i < _currentBehaviors.Count; ++i)
                {
                    b = _currentBehaviors[i];
                    if (b.CanUpdateThisFrame())
                    {
                        _totalActiveBehaviors++;
                    }
                }

                if (_totalActiveBehaviors > 0)
                {
                    // Calculate the weight scale factor based on total active
                    _weightScaleFactor = _totalActiveBehaviors > 0?_currentBehaviors.Count / _totalActiveBehaviors:1;
                    _steeringForce     = Vector3.zero;

                    for (int i = 0; i < _currentBehaviors.Count; ++i)
                    {
                        b = _currentBehaviors[i];
                        if (b.CanUpdateThisFrame())
                        {
                            // Calculate the steering force by each behavior
                            b.UpdateForFrame();
                            bool hasSteeringForce = b.steeringForce.sqrMagnitude > 0;
                            if (hasSteeringForce)
                            {
                                if (UsePrioritizedWeightSum())
                                {
                                    AccumulateForceForWeightedPriority(b);
                                }
                                else if (UsePriortizedSkip())
                                {
                                    _steeringForce += b.steeringForce;
                                    break;
                                }
                                else if (UsePriortizedDither())
                                {
                                    float randomChance = Random.Range(0.0f, 1.0f);
                                    if (randomChance < b.probability)
                                    {
                                        _steeringForce += b.steeringForce;
                                        break;
                                    }
                                }
                                else if (UseWeightedSum())
                                {
                                    _steeringForce += b.steeringForce * b.weight;
                                }
                            }
                        }
                    }
                    // Steering Force
                    ClampSteeringForce();
                    // Acceleration
                    _acceleration = _steeringForce / mass;
                    // Velocity
                    _velocity += _acceleration;
                    // Position
                    if (_velocity.sqrMagnitude > ZERO_VELOCITY_APPROX)
                    {
                        ClampVelocity();
                        _position += _velocity * Time.deltaTime;
                        if (SteeringManager.Instance.useXYPlane)
                        {
                            _rotation = Quaternion.LookRotation(_velocity, -Vector3.forward);
                        }
                        else
                        {
                            _rotation = Quaternion.LookRotation(_velocity);
                        }
                    }
                }
            }
        }