Esempio n. 1
0
    public override Vector3 CalculateForce(AutonomousMovementComponent movementComponent)
    {
        if (movementComponent.PursuitTarget != target)
        {
            target          = movementComponent.PursuitTarget;
            targetRigidbody = target.GetComponent <Rigidbody>();

#if UNITY_EDITOR
            if (targetRigidbody == null)
            {
                Debug.Log("Target does not have a rigidbody for Pursuit behavior in " + movementComponent.gameObject.name);
            }
#endif
        }

        Vector3 agentPosition  = movementComponent.transform.position;
        Vector3 targetPosition = target.position;

        Vector3 agentVelocity  = movementComponent.EntityRigidbody.velocity;
        Vector3 targetVelocity = targetRigidbody.velocity;

        Vector3 toTarget = (targetPosition - agentPosition);
        if (Vector3.Dot(toTarget, movementComponent.transform.forward) > 0 && Vector3.Dot(movementComponent.transform.forward, target.forward) < -0.95f)
        {
            return(seek.CalculateForce(movementComponent));
        }

        float movementProjectionTime = toTarget.magnitude / (movementComponent.maxSpeed + targetVelocity.magnitude);

        Vector3 updatedTargetPosition = targetPosition + (targetVelocity * movementProjectionTime);

        return(seek.SeekToPosition(agentPosition, updatedTargetPosition, movementComponent.maxSpeed, movementComponent.CurrentVelocity));
    }
Esempio n. 2
0
    public override Vector3 CalculateForce(AutonomousMovementComponent movementComponent)
    {
        if (entityTrigger == null)
        {
            entityTrigger = InitializeEntityTrigger(movementComponent.gameObject, movementComponent.CohesionRadius);
        }
        if (TaggedNeighbors.Count == 0)
        {
            return(Vector3.zero);
        }

        HashSet <Transform> .Enumerator neighborEnumerator = TaggedNeighbors.GetEnumerator();
        Vector3 centerOfMass = Vector3.zero;

        while (neighborEnumerator.MoveNext())
        {
            Transform neighbor = neighborEnumerator.Current;

            centerOfMass += neighbor.position;
        }

        centerOfMass /= TaggedNeighbors.Count;

        return(seek.SeekToPosition(movementComponent.transform.position, centerOfMass, movementComponent.MaxSpeed, movementComponent.CurrentVelocity));
    }
Esempio n. 3
0
    public override Vector3 CalculateForce(AutonomousMovementComponent movementComponent)
    {
        if (agent != movementComponent.transform)
        {
            agent          = movementComponent.transform;
            agentRenderer  = agent.GetComponent <Renderer>();
            agentRigidbody = agent.GetComponent <Rigidbody>();
        }

        bool isAtHidingSpot = (hidingSpot != Vector3.zero && (movementComponent.transform.position - hidingSpot).sqrMagnitude < 0.1f);

        if (isAtHidingSpot)
        {
            if ((movementComponent.HideTarget.position - targetPosition).sqrMagnitude < 1f)
            {
                return(Vector3.zero);
            }
            else
            {
                hasHidingSpot = false;
            }
        }

        if (!hasHidingSpot)
        {
            hidingSpot     = GenerateHidingSpot(movementComponent.transform, movementComponent.HideTarget);
            hasHidingSpot  = true;
            targetPosition = movementComponent.HideTarget.position;
        }

        return(arrive.ArriveToPosition(hidingSpot - agent.position, movementComponent.maxSpeed, agentRigidbody.velocity, 1));
    }
Esempio n. 4
0
    public override Vector3 CalculateForce(AutonomousMovementComponent movementComponent)
    {
        if (entityTrigger == null)
        {
            entityTrigger = InitializeEntityTrigger(movementComponent.gameObject, movementComponent.SeparationRadius);
        }

        Quaternion averageHeading = Quaternion.identity;
        Vector4    cumulative     = Vector4.zero;
        Quaternion entityHeading  = movementComponent.transform.rotation;
        int        headingCount   = 0;

        HashSet <Transform> .Enumerator neighborEnumerator = TaggedNeighbors.GetEnumerator();

        while (neighborEnumerator.MoveNext())
        {
            Transform  neighbor        = neighborEnumerator.Current;
            Quaternion neighborHeading = neighbor.rotation;
            headingCount++;

            averageHeading = AverageQuaternion(ref cumulative, neighborHeading, entityHeading, headingCount);
        }

        movementComponent.transform.rotation = averageHeading;

        return(Vector3.zero);
    }
Esempio n. 5
0
    public override Vector3 CalculateForce(AutonomousMovementComponent movementComponent)
    {
        float wanderDistance = movementComponent.WanderDistance;
        float wanderRadius   = movementComponent.WanderRadius;
        float wanderJitter   = movementComponent.WanderJitter;

        return(generateNewWanderTarget(movementComponent.transform.forward, wanderDistance, wanderRadius, wanderJitter));
    }
Esempio n. 6
0
    public override Vector3 CalculateForce(AutonomousMovementComponent movementComponent)
    {
        Vector3 agentPosition  = movementComponent.transform.position;
        Vector3 targetPosition = movementComponent.ArriveTarget.position;

        Vector3 toTarget     = targetPosition - agentPosition;
        int     deceleration = movementComponent.Clumsiness;

        return(ArriveToPosition(toTarget, movementComponent.maxSpeed, movementComponent.CurrentVelocity, deceleration));
    }
Esempio n. 7
0
    public override Vector3 CalculateForce(AutonomousMovementComponent movementComponent)
    {
        Vector3 targetPosition = movementComponent.SeekLocation;

        if (movementComponent.SeekTarget != null)
        {
            targetPosition = movementComponent.SeekTarget.position;
        }

        return(SeekToPosition(movementComponent.transform.position, targetPosition, movementComponent.MaxSpeed, movementComponent.CurrentVelocity));
    }
Esempio n. 8
0
    protected override void Awake()
    {
        base.Awake();
        movementComponent = GetComponent <AutonomousMovementComponent>();
        entityRigidbody   = GetComponent <Rigidbody>();
        cachedVelocities  = new Vector3[valuesToSmooth];

        for (int i = 0; i < cachedVelocities.Length; i++)
        {
            cachedVelocities[i] = Vector3.zero;
        }
    }
Esempio n. 9
0
    public Vector3 CalculateInterposePoint(AutonomousMovementComponent movementComponent, Transform primaryTarget, Transform secondaryTarget, Vector3 primaryTargetVelocity, Vector3 secondaryTargetVelocity)
    {
        Vector3 midpointBetweenTargets = (primaryTarget.position + secondaryTarget.position) / 2f;

        float timeToReachMidpoint = (midpointBetweenTargets - movementComponent.transform.position).magnitude / movementComponent.maxSpeed;

        Vector3 updatedPrimaryTargetPosition   = primaryTarget.position + (primaryTargetVelocity * timeToReachMidpoint);
        Vector3 updatedSecondaryTargetPosition = secondaryTarget.position + (secondaryTargetVelocity * timeToReachMidpoint);

        Vector3 updatedMidpoint = (updatedPrimaryTargetPosition + updatedSecondaryTargetPosition) / 2f;

        return(updatedMidpoint);
    }
Esempio n. 10
0
    public override Vector3 CalculateForce(AutonomousMovementComponent movementComponent)
    {
        if (target != movementComponent.OffsetPursuitTarget)
        {
            target          = movementComponent.OffsetPursuitTarget;
            targetRigidbody = target.GetComponent <Rigidbody>();
        }
        Vector3 worldOffsetPosition = target.position + movementComponent.PursuitOffset;

        Vector3 toOffset      = worldOffsetPosition - movementComponent.transform.position;
        float   timeToOffset  = toOffset.magnitude / (movementComponent.MaxSpeed + targetRigidbody.velocity.magnitude);
        Vector3 updatedTarget = worldOffsetPosition + (targetRigidbody.velocity * timeToOffset);

        return(arrive.ArriveToPosition(updatedTarget - movementComponent.transform.position, movementComponent.MaxSpeed, movementComponent.CurrentVelocity, 1));
    }
Esempio n. 11
0
    public override Vector3 CalculateForce(AutonomousMovementComponent movementComponent)
    {
        if (obstacleAvoidanceTrigger == null)
        {
            obstacleAvoidanceTrigger = InitializeObstacleAvoidanceTrigger(movementComponent.gameObject);
        }

        UpdateTriggerSize(movementComponent.EntityRigidbody.velocity.magnitude, movementComponent.MaxSpeed, movementComponent.transform);

        if (TaggedColliders.Count == 0)
        {
            return(Vector3.zero);
        }

        // In local coordinate system, viewed from above, agent's "forward" is along Z-axis ('vertically'), agent's "right"
        // is along X-axis ("horizontally"). PGAIBE assumes that agent's "forward" is along x-axis ('horizontally') so there's
        // some inconsistency.
        Vector3  steeringForce          = Vector3.zero;
        float    avoidanceTriggerLength = obstacleAvoidanceTrigger.size.z;
        Collider closestCollider        = null;
        float    closestZDistance       = float.MaxValue;

        HashSet <Collider> .Enumerator colliderEnumerator = TaggedColliders.GetEnumerator();

        while (colliderEnumerator.MoveNext())
        {
            Collider  collider          = colliderEnumerator.Current;
            Transform colliderTransform = collider.transform;

            float zDistanceToCollider = movementComponent.transform.InverseTransformPoint(colliderTransform.position).z;

            if (zDistanceToCollider < closestZDistance)
            {
                closestZDistance = zDistanceToCollider;
                closestCollider  = collider;
            }
        }

        float   steeringWeight             = 1.5f + (avoidanceTriggerLength - closestZDistance) / avoidanceTriggerLength;
        Vector3 relativePositionOfCollider = movementComponent.transform.InverseTransformPoint(closestCollider.transform.position);

        float effectiveObstacleRadius = closestCollider.bounds.extents.x;

        steeringForce.x = (effectiveObstacleRadius - relativePositionOfCollider.x) * steeringWeight;
        steeringForce.z = (effectiveObstacleRadius - closestZDistance) * brakingWeight;

        return(movementComponent.transform.TransformVector(steeringForce));
    }
Esempio n. 12
0
    public override Vector3 CalculateForce(AutonomousMovementComponent movementComponent)
    {
        if (movementComponent.PrimaryInterposeTarget == null || movementComponent.SecondaryInterposeTarget == null)
        {
            return(Vector3.zero);
        }

        if (primaryInterposeTarget != movementComponent.PrimaryInterposeTarget || secondaryInterposeTarget != movementComponent.SecondaryInterposeTarget)
        {
            primaryInterposeTarget   = movementComponent.PrimaryInterposeTarget;
            secondaryInterposeTarget = movementComponent.SecondaryInterposeTarget;

            primaryTargetRigidbody   = primaryInterposeTarget.GetComponent <Rigidbody>();
            secondaryTargetRigidbody = secondaryInterposeTarget.GetComponent <Rigidbody>();
        }

        Vector3 targetMidpoint   = CalculateInterposePoint(movementComponent, primaryInterposeTarget, secondaryInterposeTarget, primaryTargetRigidbody.velocity, secondaryTargetRigidbody.velocity);
        Vector3 toTargetMidpoint = targetMidpoint - movementComponent.transform.position;

        return(arrive.ArriveToPosition(toTargetMidpoint, movementComponent.maxSpeed, movementComponent.CurrentVelocity));
    }
Esempio n. 13
0
    public override Vector3 CalculateForce(AutonomousMovementComponent movementComponent)
    {
        if (entityTrigger == null)
        {
            entityTrigger = InitializeEntityTrigger(movementComponent.gameObject, movementComponent.SeparationRadius);
        }

        Vector3 agentPosition = movementComponent.transform.position;
        Vector3 steeringForce = Vector3.zero;

        HashSet <Transform> .Enumerator colliderEnumerator = TaggedNeighbors.GetEnumerator();

        while (colliderEnumerator.MoveNext())
        {
            Transform neighbor = colliderEnumerator.Current;
            Vector3   toAgent  = agentPosition - neighbor.position;

            Vector3 responseForce = (toAgent.normalized / toAgent.magnitude);
            steeringForce += responseForce;
        }

        return(steeringForce);
    }
Esempio n. 14
0
 public void OnEnable()
 {
     nextState         = new PonderingState(this);
     playerThinker     = GetComponent <Thinker>();
     movementComponent = GetComponent <AutonomousMovementComponent>();
 }
Esempio n. 15
0
 protected override void OnEnable()
 {
     base.OnEnable();
     autonomousMovementComponent = (AutonomousMovementComponent)target;
     movementBehaviors           = serializedObject.FindProperty("movementBehaviors");
 }
Esempio n. 16
0
File: Flee.cs Progetto: gewl/fortda
 public override Vector3 CalculateForce(AutonomousMovementComponent movementComponent)
 {
     return(FleeFromPosition(movementComponent.transform.position, movementComponent.FleeTarget.position, movementComponent.maxSpeed, movementComponent.CurrentVelocity));
 }
Esempio n. 17
0
 public abstract Vector3 CalculateForce(AutonomousMovementComponent movementComponent);