Esempio n. 1
0
        // alert
        // agent might try to avoid himself or other agent (if he detect it as wall)
        public override Steering GetSteering()
        {
            Steering steering  = new Steering();
            Vector3  position  = transform.position;
            Vector3  rayVector = Agent.GetVelocity().normalized *lookAhead;
            Vector3  direction = rayVector;
            Collider col       = this.GetComponent <Collider>();

            if (Agent is Agent2D)
            {
                RaycastHit2D hit = Physics2D.Raycast(position + direction.normalized, direction, lookAhead);
                if (hit && (col == null || hit.collider != col))
                {
                    position   = hit.point + hit.normal * avoidDistance;
                    _targetPos = position;
                    steering   = base.GetSteering(_targetPos);
                }
            }
            else
            {
                RaycastHit hit;
                if (Physics.Raycast(position, direction, out hit, lookAhead))
                {
                    position   = hit.point + hit.normal * avoidDistance;
                    _targetPos = position;
                    steering   = base.GetSteering(_targetPos);
                }
            }
            return(steering);
        }
Esempio n. 2
0
        public override Steering GetSteering()
        {
            Steering   steering           = new Steering();
            float      shortestTime       = Mathf.Infinity;
            GameObject firstTarget        = null;
            float      firstMinSeparation = 0.0f;
            float      firstDistance      = 0.0f;
            Vector3    firstRelativePos   = Vector3.zero;
            Vector3    firstRelativeVel   = Vector3.zero;

            foreach (GameObject t in targets)
            {
                Vector3 relativePos;
                IAgent  targetAgent = t.GetComponent <IAgent>();
                relativePos = t.transform.position - transform.position;
                Vector3 relativeVel     = targetAgent.GetVelocity() - Agent.GetVelocity();
                float   relativeSpeed   = relativeVel.magnitude;
                float   timeToCollision = Vector3.Dot(relativePos, relativeVel);
                timeToCollision /= relativeSpeed * relativeSpeed * -1;
                float distance      = relativePos.magnitude;
                float minSeparation = distance - relativeSpeed * timeToCollision;
                if (minSeparation > 2 * CollisionRadius)
                {
                    continue;
                }
                if (timeToCollision > 0.0f && timeToCollision < shortestTime)
                {
                    shortestTime       = timeToCollision;
                    firstTarget        = t;
                    firstMinSeparation = minSeparation;
                    firstRelativePos   = relativePos;
                    firstRelativeVel   = relativeVel;
                }
            }
            if (firstTarget == null)
            {
                return(steering);
            }
            if (firstMinSeparation <= 0.0f || firstDistance < 2 * CollisionRadius)
            {
                firstRelativePos = firstTarget.transform.position;
            }
            else
            {
                firstRelativePos += firstRelativeVel * shortestTime;
            }
            firstRelativePos.Normalize();
            steering.linear = -firstRelativePos *Agent.GetMaxAccel();

            return(steering);
        }
Esempio n. 3
0
        public override Steering GetSteering()
        {
            Steering steering  = new Steering();
            Vector3  direction = Agent.GetVelocity().normalized *lookAhead;
            Vector3  position  = _transform.position + direction;

            bool outOfBounds = false;

            if (position.x >= TopRightBoundary.x || position.x <= DownLeftBoundary.x)
            {
                outOfBounds = true;
            }
            else if (position.y >= TopRightBoundary.y || position.y <= DownLeftBoundary.y)
            {
                outOfBounds = true;
            }
            if (outOfBounds)
            {
                position = position * -1 * avoidDistance;
                Target.transform.position = position;
                steering = base.GetSteering();
                float orientation;
                direction *= -1;
                if (Agent is Agent2D)
                {
                    orientation = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
                }
                else
                {
                    orientation = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
                }
                steering.angular = orientation;
                Debug.Log("Out of bounds");
            }

            /* if (Physics.Raycast(position, direction, out hit, lookAhead))
             * {
             *   position = hit.point + hit.normal * avoidDistance;
             *   Target.transform.position = position;
             *   steering = base.GetSteering();
             * }*/
            return(steering);
        }
Esempio n. 4
0
        public override Steering GetSteering()
        {
            Vector3 velocity = Agent.GetVelocity();

            if (velocity.magnitude <= 0.0001f)
            {
                return(new Steering());
            }

            if (Agent is Agent2D)
            {
                _targetOrientation = Mathf.Atan2(velocity.y, velocity.x) * Mathf.Rad2Deg;
            }
            else
            {
                _targetOrientation = Mathf.Atan2(velocity.x, velocity.z) * Mathf.Rad2Deg;
            }

            return(base.GetSteering(_targetOrientation, Agent));
        }
Esempio n. 5
0
        public override Steering GetSteering()
        {
            if (_targetAgent == null)
            {
                _targetAgent = Target.GetComponent <IAgent>();
            }
            Vector3 direction = Target.transform.position - _transform.position;
            float   distance  = direction.magnitude;
            float   speed     = Agent.GetVelocity().magnitude;
            float   prediction;

            if (speed <= distance / maxPrediction)
            {
                prediction = maxPrediction;
            }
            else
            {
                prediction = distance / speed;
            }
            _targetPosition  = Target.transform.position;
            _targetPosition += _targetAgent.GetVelocity() * prediction;

            return(base.GetSteering(_targetPosition));
        }
Esempio n. 6
0
 void Update()
 {
     _animator.SetFloat(SpeedFloatVar, _agent.GetVelocity().magnitude);
 }