/// <summary>
        /// Gets the steering output.
        /// </summary>
        /// <param name="input">The steering input containing relevant information to use when calculating the steering output.</param>
        /// <param name="output">The steering output to be populated.</param>
        public void GetSteering(SteeringInput input, SteeringOutput output)
        {
            GetDesiredSteering(input, output);

            if (output.hasOutput)
            {
                output.desiredAcceleration *= this.weight;
            }
        }
        /// <summary>
        /// Gets the steering output.
        /// </summary>
        /// <param name="input">The steering input containing relevant information to use when calculating the steering output.</param>
        /// <param name="output">The steering output to be populated.</param>
        public void GetSteering(SteeringInput input, SteeringOutput output)
        {
            for (int i = 0; i < _steeringComponents.Count; i++)
            {
                var c = _steeringComponents[i];

                _memberOutput.Clear();
                c.GetSteering(input, _memberOutput);

                output.overrideHeightNavigation |= _memberOutput.overrideHeightNavigation;
                if (_memberOutput.hasOutput)
                {
                    output.desiredAcceleration += _memberOutput.desiredAcceleration;
                    output.verticalForce += _memberOutput.verticalForce;
                    output.maxAllowedSpeed = Mathf.Max(output.maxAllowedSpeed, _memberOutput.maxAllowedSpeed);
                    output.pause |= _memberOutput.pause;
                }
            }
        }
예제 #3
0
        public void GetSteering(SteeringInput input, SteeringOutput output)
        {
            for (int i = 0; i < _steeringComponents.Count; i++)
            {
                var c = _steeringComponents[i];

                _memberOutput.Clear();
                c.GetSteering(input, _memberOutput);

                output.overrideGravity |= _memberOutput.overrideGravity;
                if (_memberOutput.hasOutput)
                {
                    output.desiredAcceleration += _memberOutput.desiredAcceleration;
                    output.verticalForce       += _memberOutput.verticalForce;
                    output.maxAllowedSpeed      = Mathf.Max(output.maxAllowedSpeed, _memberOutput.maxAllowedSpeed);
                    output.pause |= _memberOutput.pause;
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="SteeringGroup"/> class.
 /// </summary>
 /// <param name="priority">The priority of the group.</param>
 public SteeringGroup(int priority)
 {
     this.priority = priority;
     _memberOutput = new SteeringOutput();
     _steeringComponents = new List<ISteeringBehaviour>();
 }
        private void Awake()
        {
            this.WarnIfMultipleInstances();

            _transform = this.transform;
            var rb = this.GetComponent<Rigidbody>();

            //Resolve the mover
            _mover = this.As<IMoveUnits>();
            if (_mover == null)
            {
                var fact = this.As<IMoveUnitsFactory>();
                var charController = this.GetComponent<CharacterController>();

                if (fact != null)
                {
                    _mover = fact.Create();
                }
                else if (charController != null)
                {
                    _mover = new CharacterControllerMover(charController);
                }
                else if (rb != null)
                {
                    _mover = new RigidBodyMover(rb);
                }
                else
                {
                    _mover = new DefaultMover(_transform);
                }
            }

            //Height resolver
            _heights = this.As<IHeightNavigator>();
            if (_heights == null)
            {
                _heights = NoHeightNavigator.Instance;
            }

            //Assign unit ref, container for components and steering visitors
            _steeringComponents = new List<ISteeringBehaviour>();
            _orientationComponents = new List<IOrientationBehaviour>();
            _steering = new SteeringOutput();
            _orientation = new OrientationOutput();
            _steeringInput = new SteeringInput();
        }
 /// <summary>
 /// Gets the desired steering output.
 /// </summary>
 /// <param name="input">The steering input containing relevant information to use when calculating the steering output.</param>
 /// <param name="output">The steering output to be populated.</param>
 public abstract void GetDesiredSteering(SteeringInput input, SteeringOutput output);
        /// <summary>
        /// Gets the desired steering output.
        /// </summary>
        /// <param name="input">The steering input containing relevant information to use when calculating the steering output.</param>
        /// <param name="output">The steering output to be populated.</param>
        public override void GetDesiredSteering(SteeringInput input, SteeringOutput output)
        {
            if (input.currentPlanarVelocity.sqrMagnitude < _minSpeedSquared)
            {
                return;
            }

            var otherUnits = _scanner.Units;
            float maxAdj = 0f;
            float adjSum = 0f;
            Vector3 moveVector = Vector3.zero;

            for (int i = 0; i < otherUnits.Length; i++)
            {
                var other = otherUnits[i];
                if (other.Equals(null) || other.Equals(input.unit.collider))
                {
                    continue;
                }

                Vector3 evadePos;
                var otherVelo = other.GetUnitFacade();

                if (otherVelo == null)
                {
                    evadePos = other.transform.position;
                }
                else
                {
                    var otherPos = otherVelo.position;
                    var distToOther = (otherPos - input.unit.position).magnitude;
                    var otherSpeed = otherVelo.velocity.magnitude;

                    var predictionTime = 0.1f;
                    if (otherSpeed > 0f)
                    {
                        //Half the prediction time for better behavior
                        predictionTime = (distToOther / otherSpeed) / 2f;
                    }

                    evadePos = otherPos + (otherVelo.velocity * predictionTime);
                }

                var offset = input.unit.position - evadePos;
                var offsetMagnitude = offset.magnitude;

                //Only do avoidance if inside vision cone or very close to the unit
                if (offsetMagnitude > _omniAwareRadius && Vector3.Dot(input.unit.forward, offset / offsetMagnitude) > _fovReverseAngleCos)
                {
                    continue;
                }

                //The adjustment normalizes the offset and adjusts its impact according to the offset length, i.e. the further the other unit is away the less it will impact the steering
                var adj = 1f / (offsetMagnitude * offsetMagnitude);
                adjSum += adj;
                if (adj > maxAdj)
                {
                    maxAdj = adj;
                }

                moveVector += (offset * adj);
            }

            if (maxAdj > 0f)
            {
                //Lastly we average out the move vector based on adjustments
                moveVector = moveVector / (adjSum / maxAdj);
                output.desiredAcceleration = Seek(input.unit.position + moveVector, input);
            }
        }
예제 #8
0
        private void Awake()
        {
            this.WarnIfMultipleInstances();

            _transform = this.transform;
            var rb = this.GetComponent<Rigidbody>();

            //Resolve the mover
            _mover = this.As<IMoveUnits>();
            if (_mover == null)
            {
                var fact = this.As<IMoveUnitsFactory>();
                var charController = this.GetComponent<CharacterController>();

                if (fact != null)
                {
                    _mover = fact.Create();
                }
                else if (charController != null)
                {
                    _mover = new CharacterControllerMover(charController);
                }
                else if (rb != null)
                {
                    _mover = new RigidBodyMover(rb);
                }
                else
                {
                    _mover = new DefaultMover(_transform);
                }
            }

            //Make sure the rigidbody obeys the rules
            if (rb != null)
            {
                rb.constraints |= RigidbodyConstraints.FreezeRotation;
                rb.useGravity = false;
            }

            //Height resolver
            _heights = GameServices.heightStrategy.heightSampler;

            //Assign unit ref, container for components and steering visitors
            _steeringComponents = new List<ISteeringBehaviour>();
            _orientationComponents = new List<IOrientationBehaviour>();
            _steering = new SteeringOutput();
            _orientation = new OrientationOutput();
            _steeringInput = new SteeringInput();
        }
예제 #9
0
 public SteeringGroup(int priority)
 {
     this.priority       = priority;
     _memberOutput       = new SteeringOutput();
     _steeringComponents = new List <ISteeringBehaviour>();
 }
예제 #10
0
 /// <summary>
 /// Gets the desired steering output.
 /// </summary>
 /// <param name="input">The steering input containing relevant information to use when calculating the steering output.</param>
 /// <param name="output">The steering output to be populated.</param>
 public abstract void GetDesiredSteering(SteeringInput input, SteeringOutput output);