Beispiel #1
0
        public Vector3 GetSteering(MovementAIRigidbody target, Vector3 offset, out Vector3 targetPos)
        {
            Vector3 worldOffsetPos = target.Position + target.Transform.TransformDirection(offset);

            //Debug.DrawLine(transform.position, worldOffsetPos);

            /* Calculate the distance to the offset point */
            Vector3 displacement = worldOffsetPos - transform.position;
            float   distance     = displacement.magnitude;

            /* Get the character's speed */
            float speed = rb.Velocity.magnitude;

            /* Calculate the prediction time */
            float prediction;

            if (speed <= distance / maxPrediction)
            {
                prediction = maxPrediction;
            }
            else
            {
                prediction = distance / speed;
            }

            /* Put the target together based on where we think the target will be */
            targetPos = worldOffsetPos + target.Velocity * prediction;

            return(steeringBasics.Arrive(targetPos));
        }
Beispiel #2
0
        public Vector3 GetSteering(ICollection <MovementAIRigidbody> targets)
        {
            Vector3 centerOfMass = Vector3.zero;
            int     count        = 0;

            /* Sums up everyone's position who is close enough and in front of the character */
            foreach (MovementAIRigidbody r in targets)
            {
                if (steeringBasics.IsFacing(r.Position, facingCosineVal))
                {
                    centerOfMass += r.Position;
                    count++;
                }
            }

            if (count == 0)
            {
                return(Vector3.zero);
            }
            else
            {
                centerOfMass = centerOfMass / count;

                return(steeringBasics.Arrive(centerOfMass));
            }
        }
Beispiel #3
0
        void FixedUpdate()
        {
            Vector3 accel = steeringBasics.Arrive(target.transform.position);

            steeringBasics.Steer(accel);
            steeringBasics.LookWhereYoureGoing();
        }
Beispiel #4
0
        public Vector3 GetSteering(MovementAIRigidbody target, ICollection <MovementAIRigidbody> obstacles, out Vector3 bestHidingSpot)
        {
            /* Find the closest hiding spot. */
            float distToClostest = Mathf.Infinity;

            bestHidingSpot = Vector3.zero;

            foreach (MovementAIRigidbody r in obstacles)
            {
                Vector3 hidingSpot = GetHidingPosition(r, target);

                float dist = Vector3.Distance(hidingSpot, transform.position);

                if (dist < distToClostest)
                {
                    distToClostest = dist;
                    bestHidingSpot = hidingSpot;
                }
            }

            /* If no hiding spot is found then just evade the enemy. */
            if (distToClostest == Mathf.Infinity)
            {
                return(evade.GetSteering(target));
            }

            //Debug.DrawLine(transform.position, bestHidingSpot);

            return(steeringBasics.Arrive(bestHidingSpot));
        }
Beispiel #5
0
        public Vector3 GetSteering(LinePath path, bool pathLoop, out Vector3 targetPosition)
        {
            /* If the path has only one node then just go to that position. */
            if (path.Length == 1)
            {
                targetPosition = path[0];
            }
            /* Else find the closest spot on the path to the character and go to that instead. */
            else
            {
                /* Get the param for the closest position point on the path given the character's position */
                float param = path.GetParam(transform.position, rb);

                //Debug.DrawLine(transform.position, path.getPosition(param, pathLoop), Color.red, 0, false);

                if (!pathLoop)
                {
                    Vector3 finalDestination;

                    /* If we are close enough to the final destination then stop moving */
                    if (IsAtEndOfPath(path, param, out finalDestination))
                    {
                        targetPosition = finalDestination;

                        rb.Velocity = Vector3.zero;
                        return(Vector3.zero);
                    }
                }

                /* Move down the path */
                param += pathDirection * pathOffset;

                /* Set the target position */
                targetPosition = path.GetPosition(param, pathLoop);

                //Debug.DrawLine(transform.position, targetPosition, Color.red, 0, false);
            }

            return(steeringBasics.Arrive(targetPosition));
        }