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

        rb             = GetComponent <MovementAIRigidbody>();
        steeringBasics = GetComponent <SteeringBasics>();
    }
Esempio n. 2
0
    // Use this for initialization
    void Start()
    {
        rb  = GetComponent <MovementAIRigidbody>();
        cam = GetComponentInChildren <Camera>().transform;

        lookAtChar();
    }
Esempio n. 3
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));
    }
Esempio n. 4
0
    // Use this for initialization
    void Start()
    {
        MovementAIRigidbody rb = obj.GetComponent <MovementAIRigidbody>();

        rb.setUp(); //Manually set up the MovementAIRigidbody since the given obj can be a prefab
        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;
                }
            }
        }
    }
Esempio n. 5
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));
    }
Esempio 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));
    }
Esempio n. 7
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));
    }
Esempio n. 8
0
    /* Finds the param for the closest point on the segment vw given the point p */
    private 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);
    }
Esempio n. 9
0
    // Use this for initialization
    public override void Start()
    {
        rb       = GetComponent <MovementAIRigidbody>();
        ff       = GetComponentInChildren <Flash>();
        nextFire = Time.time;

        shootClip = transform.FindChild("ShootSound").GetComponent <AudioSource>();
    }
Esempio n. 10
0
    /* Gets the param for the closest point on the path given a position */
    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);
    }
Esempio n. 11
0
    //private GameObject debugRing;

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

        steeringBasics = GetComponent <SteeringBasics> ();

        rb = GetComponent <MovementAIRigidbody>();
    }
Esempio n. 12
0
    private void tryToRemove(Component other)
    {
        MovementAIRigidbody rb = other.GetComponent <MovementAIRigidbody>();

        if (rb != null)
        {
            _targets.Remove(rb);
        }
    }
Esempio n. 13
0
    private Vector3 getHidingPosition(MovementAIRigidbody obstacle, MovementAIRigidbody target)
    {
        float distAway = obstacle.radius + distanceFromBoundary;

        Vector3 dir = obstacle.position - target.position;

        dir.Normalize();

        return(obstacle.position + dir * distAway);
    }
Esempio n. 14
0
    public bool Equals(MovementAIRigidbody p)
    {
        // If parameter is null return false:
        if ((object)p == null)
        {
            return(false);
        }

        // Return true if the fields match:
        return((rb3D == p.rb3D) && (rb2D == p.rb2D));
    }
Esempio n. 15
0
    void Awake()
    {
        //stuff for the wander behavior
        float theta = Random.value * 2 * Mathf.PI;

        //create a vector to a target position on the wander circle
        wanderTarget = new Vector3(wanderRadius * Mathf.Cos(theta), wanderRadius * Mathf.Sin(theta), 0f);

        steeringBasics = GetComponent <SteeringBasics>();

        rb = GetComponent <MovementAIRigidbody>();
    }
Esempio n. 16
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));
    }
Esempio n. 17
0
    public override bool Equals(System.Object obj)
    {
        // If parameter is null return false.
        if (obj == null)
        {
            return(false);
        }

        // If parameter cannot be cast to Point return false.
        MovementAIRigidbody p = obj as MovementAIRigidbody;

        if ((System.Object)p == null)
        {
            return(false);
        }

        // Return true if the fields match:
        return((rb3D == p.rb3D) && (rb2D == p.rb2D));
    }
Esempio n. 18
0
    void Awake()
    {
        steeringBasics = GetComponent <SteeringBasics>();

        rb = GetComponent <MovementAIRigidbody>();
    }
Esempio n. 19
0
    public Vector3 getSteering(MovementAIRigidbody target, ICollection <MovementAIRigidbody> obstacles)
    {
        Vector3 bestHidingSpot;

        return(getSteering(target, obstacles, out bestHidingSpot));
    }
Esempio n. 20
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;
        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   = transform.position - r.position;
            Vector3 relativeVel   = rb.velocity - r.velocity;
            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;

            float targetRadius = r.boundingRadius;

            if (minSeparation > rb.boundingRadius + targetRadius)
            //if (minSeparation > 2 * agentRadius)
            {
                continue;
            }

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

        /* 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.boundingRadius + firstRadius)
        //if (firstMinSeparation <= 0 || firstDistance < 2 * agentRadius)
        {
            acceleration = transform.position - firstTarget.position;
        }
        /* Else calculate the future relative position */
        else
        {
            acceleration = firstRelativePos + firstRelativeVel * shortestTime;
        }

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

        return(acceleration);
    }
Esempio n. 21
0
 // Use this for initialization
 void Start()
 {
     rb             = GetComponent <MovementAIRigidbody>();
     steeringBasics = GetComponent <SteeringBasics>();
 }
Esempio n. 22
0
 private static bool isNull(MovementAIRigidbody r)
 {
     return(r == null || r.Equals(null));
 }
Esempio n. 23
0
 // Use this for initialization
 void Start()
 {
     player         = GameObject.Find("Player").GetComponent <MovementAIRigidbody>();
     steeringBasics = GetComponent <SteeringBasics>();
     pursue         = GetComponent <Pursue>();
 }
Esempio n. 24
0
 void Awake()
 {
     rb = GetComponent <MovementAIRigidbody>();
 }
Esempio n. 25
0
    public Vector3 getSteering(MovementAIRigidbody target, Vector3 offset)
    {
        Vector3 targetPos;

        return(getSteering(target, offset, out targetPos));
    }