Ejemplo n.º 1
0
        void Awake()
        {
            facingCosineVal = Mathf.Cos(facingCosine * Mathf.Deg2Rad);

            rb             = GetComponent <MovementAIRigidbody>();
            steeringBasics = GetComponent <SteeringBasics>();
        }
Ejemplo n.º 2
0
        public Vector3 GetSteering(MovementAIRigidbody target)
        {
            /* Calculate the distance to the target */
            Vector3 displacement = target.Position - 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 */
            Vector3 explicitTarget = target.Position + target.Velocity * prediction;

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

            return(steeringBasics.Seek(explicitTarget));
        }
Ejemplo n.º 3
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));
        }
Ejemplo n.º 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));
        }
Ejemplo n.º 5
0
        void Start()
        {
            MovementAIRigidbody rb = obj.GetComponent <MovementAIRigidbody>();

            /* Manually set up the MovementAIRigidbody since the given obj can be a prefab */
            rb.SetUp();
            isObj3D = rb.is3D;

            /* Find the size of the map */
            float distAway = Camera.main.WorldToViewportPoint(Vector3.zero).z;

            bottomLeft = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, distAway));
            Vector3 topRight = Camera.main.ViewportToWorldPoint(new Vector3(1, 1, distAway));

            widthHeight = topRight - bottomLeft;

            /* Create the create the objects */
            for (int i = 0; i < numberOfObjects; i++)
            {
                /* Try to place the objects multiple times before giving up */
                for (int j = 0; j < 10; j++)
                {
                    if (TryToCreateObject())
                    {
                        break;
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public Vector3 GetSteering(MovementAIRigidbody target)
        {
            /* Calculate the distance to the target */
            Vector3 displacement = target.Position - transform.position;
            float distance = displacement.magnitude;

            /* Get the targets's speed */
            float speed = target.Velocity.magnitude;

            /* Calculate the prediction time */
            float prediction;
            if (speed <= distance / maxPrediction)
            {
                prediction = maxPrediction;
            }
            else
            {
                prediction = distance / speed;
                //Place the predicted position a little before the target reaches the character
                prediction *= 0.9f;
            }

            /* Put the target together based on where we think the target will be */
            Vector3 explicitTarget = target.Position + target.Velocity * prediction;

            return flee.GetSteering(explicitTarget);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Finds the param for the closest point on the segment vw given the point p
        /// </summary>
        /// <returns>The parameter for segment.</returns>
        float GetParamForSegment(Vector3 p, Vector3 v, Vector3 w, MovementAIRigidbody rb)
        {
            Vector3 vw = w - v;

            vw = rb.ConvertVector(vw);

            float l2 = Vector3.Dot(vw, vw);

            if (l2 == 0)
            {
                return(0);
            }

            float t = Vector3.Dot(p - v, vw) / l2;

            if (t < 0)
            {
                t = 0;
            }
            else if (t > 1)
            {
                t = 1;
            }

            /* Multiple by (v - w).magnitude instead of Sqrt(l2) because we want the magnitude of the full 3D line segment */
            return(t * (v - w).magnitude);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Gets the param for the closest point on the path given a position
        /// </summary>
        public float GetParam(Vector3 position, MovementAIRigidbody rb)
        {
            int closestSegment = GetClosestSegment(position);

            float param = this.distances[closestSegment] + GetParamForSegment(position, nodes[closestSegment], nodes[closestSegment + 1], rb);

            return(param);
        }
Ejemplo n.º 9
0
        //GameObject debugRing;

        void Awake()
        {
            //		DebugDraw debugDraw = gameObject.GetComponent<DebugDraw> ();
            //		debugRing = debugDraw.createRing (Vector3.zero, wanderRadius);

            steeringBasics = GetComponent <SteeringBasics>();

            rb = GetComponent <MovementAIRigidbody>();
        }
Ejemplo n.º 10
0
        void TryToRemove(Component other)
        {
            MovementAIRigidbody rb = other.GetComponent <MovementAIRigidbody>();

            if (rb != null)
            {
                _targets.Remove(rb);
            }
        }
Ejemplo n.º 11
0
        void Start()
        {
            rb  = GetComponent <MovementAIRigidbody>();
            cam = Camera.main.transform;

            if (autoAttachToCamera)
            {
                cam.GetComponent <ThirdPersonCamera>().target = transform;
            }
        }
Ejemplo n.º 12
0
        Vector3 GetHidingPosition(MovementAIRigidbody obstacle, MovementAIRigidbody target)
        {
            float distAway = obstacle.Radius + distanceFromBoundary;

            Vector3 dir = obstacle.Position - target.Position;

            dir.Normalize();

            return(obstacle.Position + dir * distAway);
        }
Ejemplo n.º 13
0
        public Vector3 Interpose(MovementAIRigidbody target1, MovementAIRigidbody target2)
        {
            Vector3 midPoint = (target1.Position + target2.Position) / 2;

            float timeToReachMidPoint = Vector3.Distance(midPoint, transform.position) / maxVelocity;

            Vector3 futureTarget1Pos = target1.Position + target1.Velocity * timeToReachMidPoint;
            Vector3 futureTarget2Pos = target2.Position + target2.Velocity * timeToReachMidPoint;

            midPoint = (futureTarget1Pos + futureTarget2Pos) / 2;

            return(Arrive(midPoint));
        }
Ejemplo n.º 14
0
 void Awake()
 {
     rb = GetComponent <MovementAIRigidbody>();
 }
Ejemplo n.º 15
0
        public Vector3 GetSteering(MovementAIRigidbody target, ICollection <MovementAIRigidbody> obstacles)
        {
            Vector3 bestHidingSpot;

            return(GetSteering(target, obstacles, out bestHidingSpot));
        }
Ejemplo n.º 16
0
        public Vector3 GetSteering(MovementAIRigidbody target, Vector3 offset)
        {
            Vector3 targetPos;

            return(GetSteering(target, offset, out targetPos));
        }
Ejemplo n.º 17
0
 void Awake()
 {
     rb             = GetComponent <MovementAIRigidbody>();
     steeringBasics = GetComponent <SteeringBasics>();
 }
Ejemplo n.º 18
0
 static bool IsNull(MovementAIRigidbody r)
 {
     return(r == null || r.Equals(null));
 }
Ejemplo n.º 19
0
        public Vector3 GetSteering(ICollection <MovementAIRigidbody> targets)
        {
            Vector3 acceleration = Vector3.zero;

            /* 1. Find the target that the character will collide with first */

            /* The first collision time */
            float shortestTime = float.PositiveInfinity;

            /* The first target that will collide and other data that
             * we will need and can avoid recalculating */
            MovementAIRigidbody firstTarget = null;
            float   firstMinSeparation = 0, firstDistance = 0, firstRadius = 0;
            Vector3 firstRelativePos = Vector3.zero, firstRelativeVel = Vector3.zero;

            foreach (MovementAIRigidbody r in targets)
            {
                /* Calculate the time to collision */
                Vector3 relativePos   = rb.ColliderPosition - r.ColliderPosition;
                Vector3 relativeVel   = rb.RealVelocity - r.RealVelocity;
                float   distance      = relativePos.magnitude;
                float   relativeSpeed = relativeVel.magnitude;

                if (relativeSpeed == 0)
                {
                    continue;
                }

                float timeToCollision = -1 * Vector3.Dot(relativePos, relativeVel) / (relativeSpeed * relativeSpeed);

                /* Check if they will collide at all */
                Vector3 separation    = relativePos + relativeVel * timeToCollision;
                float   minSeparation = separation.magnitude;

                if (minSeparation > rb.Radius + r.Radius + distanceBetween)
                {
                    continue;
                }

                /* Check if its the shortest */
                if (timeToCollision > 0 && timeToCollision < shortestTime)
                {
                    shortestTime       = timeToCollision;
                    firstTarget        = r;
                    firstMinSeparation = minSeparation;
                    firstDistance      = distance;
                    firstRelativePos   = relativePos;
                    firstRelativeVel   = relativeVel;
                    firstRadius        = r.Radius;
                }
            }

            /* 2. Calculate the steering */

            /* If we have no target then exit */
            if (firstTarget == null)
            {
                return(acceleration);
            }

            /* If we are going to collide with no separation or if we are already colliding then
             * steer based on current position */
            if (firstMinSeparation <= 0 || firstDistance < rb.Radius + firstRadius + distanceBetween)
            {
                acceleration = rb.ColliderPosition - firstTarget.ColliderPosition;
            }
            /* Else calculate the future relative position */
            else
            {
                acceleration = firstRelativePos + firstRelativeVel * shortestTime;
            }

            /* Avoid the target */
            acceleration = rb.ConvertVector(acceleration);
            acceleration.Normalize();
            acceleration *= maxAcceleration;

            return(acceleration);
        }