public override Vector3 CalculateSteeringForce(float dt, BehaviourContext context)
        {
            _positionTarget = _CurrentTarget.transform.position;

            _velocityDesired = (_positionTarget - context._position).normalized * context._settings._maxDesiredVelocity;
            return(_velocityDesired - context._velocity);
        }
Beispiel #2
0
 public override void OnDrawGizmos(BehaviourContext context)
 {
     Support.DrawRay(context._position, context._velocity, Color.red);
     Support.DrawRay(context._position, _velocityDesired, Color.blue);
     UnityEditor.Handles.color = Color.black;
     UnityEditor.Handles.DrawSolidDisc(_positionTarget, Vector3.up, 0.25f);
 }
Beispiel #3
0
        public override void start(BehaviourContext context)
        {
            base.start(context);
            Debug.Log(FindColliderWithLayer(context._settings._hideLayer));

            _colliders = FindColliderWithLayer(context._settings._hideLayer);
        }
        public override Vector3 CalculateSteeringForce(float dt, BehaviourContext context)
        {
            _positionTarget   = GameObject.Find("Priority Target").transform.position;
            _positionTarget.y = context._position.y;

            Vector3 stopVector   = (context._position - _positionTarget).normalized * context._settings._arriveDistance;
            Vector3 stopPosition = _positionTarget + stopVector;

            Vector3 targetOffset = stopPosition - context._position;
            float   distance     = targetOffset.magnitude;

            float rampedSpeed  = context._settings._maxDesiredVelocity * (distance / context._settings._slowingDistance);
            float clippedSpeed = Mathf.Min(rampedSpeed, context._settings._maxDesiredVelocity);

            if (distance > 0.001f)
            {
                _velocityDesired = (clippedSpeed / distance) * targetOffset;
            }
            else
            {
                _velocityDesired = Vector3.zero;
            }

            return(_velocityDesired - context._velocity);
        }
Beispiel #5
0
 public override void start(BehaviourContext context)
 {
     base.start(context);
     waypointIndex     = 0;
     waypointPositions = GameObject.FindGameObjectsWithTag("Waypoint");
     Debug.Log(waypointPositions.Length);
 }
        public override Vector3 CalculateSteeringForce(float dt, BehaviourContext context)
        {
            Ray ray = new Ray(context._position, context._velocity);

            _doAvoidObject = Physics.Raycast(ray, out RaycastHit hit, context._settings._avoidDistance, _obstacleLayer, QueryTriggerInteraction.Collide);

            if (!_doAvoidObject)
            {
                return(Vector3.zero);
            }

            _hitPoint = hit.point;

            _velocityDesired = (_hitPoint - hit.collider.transform.position).normalized * context._settings._avoidMaxForce;

            float angle = Vector3.Angle(_velocityDesired, context._velocity);

            if (angle > 179)
            {
                _velocityDesired = Vector3.Cross(Vector3.up, context._velocity);
            }

            _positionTarget = context._position + _velocityDesired;
            return(_velocityDesired - context._velocity);
        }
Beispiel #7
0
        public Vector3 CalculateHidingPlace(BehaviourContext context, Collider collider, Vector3 enemy_Position)
        {
            Vector3 obstacleDirection = (collider.transform.position - enemy_Position).normalized;
            Vector3 pointOtherSide    = collider.transform.position + obstacleDirection;
            Vector3 hidingPlace       = collider.ClosestPoint(pointOtherSide) + (obstacleDirection * context._settings._hideOffset);

            return(hidingPlace);
        }
Beispiel #8
0
        public override void OnDrawGizmos(BehaviourContext context)
        {
            base.OnDrawGizmos(context);

            foreach (Vector3 hidingPlace in _hidingPlaces)
            {
                Support.DrawSolidSphere(hidingPlace, Color.blue, 0.25f);
            }
            Support.DrawWireSphere(_hidingPlace, Color.blue, 0.35f);
        }
        public override Vector3 CalculateSteeringForce(float dt, BehaviourContext context)
        {
            var hitColliders = Physics.OverlapSphere(context._position, context._settings._AvoidRange, context._settings._SeekingObject);

            Support.GetClosestEnemy(context._position, hitColliders);


            _positionTarget = Support.GetClosestEnemy(context._position, hitColliders).gameObject.transform.position;

            _velocityDesired = -(_positionTarget - context._position).normalized * context._settings._maxDesiredVelocity;
            return(_velocityDesired - context._velocity);
        }
Beispiel #10
0
        public Vector3 GetFuturePosition(float dt, BehaviourContext context)
        {
            // hier calculate je steeds de positions met een waarde van 1 of meer ertussen als ik het goed heb
            _previousTargetPosition = _currentTargetPosition;
            _currentTargetPosition  = _positionTarget;

            // hier reken je de directie van de Target uit
            Vector3 targetVelocity = (_currentTargetPosition - _previousTargetPosition) / dt;

            // hier bereken en geef je de waarde terug van waar hij heen moet
            return(_currentTargetPosition + targetVelocity * context._settings._lookAheadTime);
        }
        public override Vector3 CalculateSteeringForce(float dt, BehaviourContext context)
        {
            _wanderAngle += Random.Range(-0.5f * context._settings._wanderNoiseAngle * Mathf.Deg2Rad, 0.5f * context._settings._wanderNoiseAngle * Mathf.Deg2Rad);

            Vector3 centerOfCircle = context._position + context._velocity.normalized * context._settings._wanderNoiseAngle;

            Vector3 offset = new Vector3(context._settings._wanderCircleRadius * Mathf.Cos(_wanderAngle), 0.0f, context._settings._wanderCircleRadius * Mathf.Sin(_wanderAngle));

            _positionTarget = centerOfCircle + offset;

            _velocityDesired = (_positionTarget - context._position).normalized * context._settings._maxDesiredVelocity;
            return(_velocityDesired - context._velocity);
        }
Beispiel #12
0
        public override Vector3 CalculateSteeringForce(float dt, BehaviourContext context)
        {
            //////////-----------------------------------------------------------------------------------------------------------

            if (context._settings._followPathMode == SteeringSettings.FPM.Forwards)
            {
                if (waypointDistance < 2f)
                {
                    waypointIndex++;
                }
                if (waypointIndex == waypointPositions.Length)
                {
                    waypointIndex = 0;
                }
                _positionTarget = waypointPositions[waypointIndex].transform.position;
            }

            //////////-----------------------------------------------------------------------------------------------------------
            if (context._settings._followPathMode == SteeringSettings.FPM.Backwards)
            {
                if (waypointDistance < 2f)
                {
                    if (waypointIndex <= 0)
                    {
                        waypointIndex = waypointPositions.Length - 1;
                    }
                    else
                    {
                        waypointIndex--;
                    }
                }
                _positionTarget = waypointPositions[waypointIndex].transform.position;
            }
            //////////-----------------------------------------------------------------------------------------------------------

            if (context._settings._followPathMode == SteeringSettings.FPM.Random)
            {
                if (waypointDistance < 2f)
                {
                    waypointIndex = GetRandomWaypoint();
                }
                _positionTarget = waypointPositions[waypointIndex].transform.position;
            }

            waypointDistance = (waypointPositions[waypointIndex].transform.position - context._position).magnitude;

            _velocityDesired = (_positionTarget - context._position).normalized * context._settings._maxDesiredVelocity;
            return(_velocityDesired - context._velocity);
        }
        public override Vector3 CalculateSteeringForce(float dt, BehaviourContext context)
        {
            Vector3 requested_Direction = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));

            if (requested_Direction != Vector3.zero)
            {
                _positionTarget = context._position + requested_Direction.normalized * context._settings._maxDesiredVelocity;
            }
            else
            {
                _positionTarget = context._position;
            }

            _velocityDesired = (_positionTarget - context._position).normalized * context._settings._maxDesiredVelocity;
            return(_velocityDesired - context._velocity);
        }
        public Vector3 CalculateDesiredVelocity(BehaviourContext context)
        {
            Collider[] neighbors = Physics.OverlapSphere(context._position, _largestRadius, _flockLayer, QueryTriggerInteraction.Collide);
            if (neighbors.Length == 0)
            {
                return(Vector3.zero);
            }

            FlockAlignment  alignment  = new FlockAlignment(context._settings._flockAlignmentRadius);
            FlockCohesion   cohesion   = new FlockCohesion(context._settings._flockCohesionRadius);
            FlockSeperation seperation = new FlockSeperation(context._settings._flockSeparationRadius);

            foreach (Collider neighbor in neighbors)
            {
                if (neighbor == _myCollider)
                {
                    continue;
                }

                Steering neighborSteering = neighbor.gameObject.GetComponent <Steering>();
                if (neighborSteering == null)
                {
                    Debug.LogError($"ERROR: Flock behaviour found neighbor in layer {context._settings._flocklayer} without steering script!");
                    continue;
                }

                Vector3 neighborDirection = neighborSteering._position - context._position;
                float   sqrDistance       = neighborDirection.sqrMagnitude;

                if (Vector3.Angle(_myCollider.transform.forward, neighborDirection) > context._settings._flockVisibilityAngle)
                {
                    continue;
                }

                alignment.AddVelocity(sqrDistance, neighborSteering._velocity);
                cohesion.AddPosition(sqrDistance, neighborSteering._position);
                seperation.AddDirection(sqrDistance, neighborDirection);
            }

            Vector3 desiredVelocity = alignment.DesiredVelocity() * context._settings._flockAlignmentWeight +
                                      cohesion.DesiredVelocity(context._position) * context._settings._flockCohesionWeight +
                                      seperation.DesiredVelocity() * context._settings._flockSeparationWeight;

            return(desiredVelocity.normalized * context._settings._maxDesiredVelocity);
        }
        public override void start(BehaviourContext context)
        {
            base.start(context);

            _flockLayer = LayerMask.GetMask(context._settings._flocklayer);
            if (context._settings._flockAlignmentWeight > 0.0f)
            {
                _largestRadius = Mathf.Max(_largestRadius, context._settings._flockAlignmentRadius);
            }
            if (context._settings._flockAlignmentWeight > 0.0f)
            {
                _largestRadius = Mathf.Max(_largestRadius, context._settings._flockCohesionRadius);
            }
            if (context._settings._flockAlignmentWeight > 0.0f)
            {
                _largestRadius = Mathf.Max(_largestRadius, context._settings._flockSeparationRadius);
            }
        }
Beispiel #16
0
        public override Vector3 CalculateSteeringForce(float dt, BehaviourContext context)
        {
            var hitColliders   = Physics.OverlapSphere(context._position, context._settings._lookRange, context._settings._SeekingObject);
            var _closestTarget = Support.GetClosestEnemy(context._position, hitColliders);

            if (hitColliders != null && _closestTarget != null)
            {
                _positionTarget = _closestTarget.transform.position;
            }
            else
            {
                _positionTarget = context._position;
            }

            _positionTarget   = GetFuturePosition(dt, context);
            _positionTarget.y = context._position.y;

            _velocityDesired = (_positionTarget - context._position).normalized * context._settings._maxDesiredVelocity;
            return(_velocityDesired - context._velocity);
        }
Beispiel #17
0
        public Vector3 CalculateHidingPlace(BehaviourContext context, Vector3 enemy_Position)
        {
            float closestDistanceSqr = float.MaxValue;

            _hidingPlace  = context._position;
            _hidingPlaces = new HideList();

            for (int i = 0; i < _colliders.Count; i++)
            {
                Vector3 hidingPlace = CalculateHidingPlace(context, _colliders[i], enemy_Position);
                _hidingPlaces.Add(hidingPlace);

                float distanceToHidingPlace = (context._position - hidingPlace).sqrMagnitude;
                if (distanceToHidingPlace < closestDistanceSqr)
                {
                    closestDistanceSqr = distanceToHidingPlace;
                    _hidingPlace       = hidingPlace;
                }
            }
            return(_hidingPlace);
        }
Beispiel #18
0
        public override Vector3 CalculateSteeringForce(float dt, BehaviourContext context)
        {
            float distance = (_positionTarget - context._position).magnitude;

            if (Input.GetMouseButtonDown(0))
            {
                if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out RaycastHit hit))
                {
                    _positionTarget   = hit.point;
                    _positionTarget.y = context._position.y;
                }
            }
            if (distance < context._settings._arriveDistance)
            {
                _velocityDesired = Vector3.zero;
            }
            else
            {
                _velocityDesired = (_positionTarget - context._position).normalized * context._settings._maxDesiredVelocity;
            }

            return(_velocityDesired - context._velocity);
        }
 public override Vector3 CalculateSteeringForce(float dt, BehaviourContext context)
 {
     _positionTarget  = context._position + dt * context._velocity;
     _velocityDesired = Vector3.zero;
     return(_velocityDesired - context._velocity);
 }
 public override Vector3 CalculateSteeringForce(float dt, BehaviourContext context)
 {
     _velocityDesired = CalculateDesiredVelocity(context);
     _positionTarget  = context._position + _velocityDesired * dt;
     return(_velocityDesired - context._velocity);
 }
 public override void start(BehaviourContext context)
 {
     base.start(context);
     _obstacleLayer = LayerMask.GetMask(context._settings._avoidLayer);
 }
 public virtual void start(BehaviourContext context)
 {
     _positionTarget = context._position;
 }
 public override void start(BehaviourContext context)
 {
     base.start(context);
 }
 public override void OnDrawGizmos(BehaviourContext context)
 {
     Support.DrawSolidSphere(_positionTarget, Color.black, 0.25f);
 }
 public abstract Vector3 CalculateSteeringForce(float dt, BehaviourContext context);
 public virtual void OnDrawGizmos(BehaviourContext context)
 {
     Support.DrawRay(context._position, _velocityDesired, Color.red);
 }
Beispiel #27
0
 //----------------------------------------------------------------------------------------------------------------------//
 //de point seek doet het niet echt goed omdat hij in een fixedupdate zit
 //----------------------------------------------------------------------------------------------------------------------//
 public override void start(BehaviourContext context)
 {
     base.start(context);
     context._position = _positionTarget;
     Debug.Log(context._position);
 }
 public override void OnDrawGizmos(BehaviourContext context)
 {
     Support.DrawWireSphere(context._position, Color.red, context._settings._AvoidRange);
 }
Beispiel #29
0
 public override void OnDrawGizmos(BehaviourContext context)
 {
     Support.DrawCurrentDestiny(_positionTarget, Color.blue);
     Support.DrawCircle(context._position, Color.white, context._settings._lookRange);
 }