예제 #1
0
    private void AssignWanderPosition()
    {
        var     nearestTarget = Targeting.FindNearest(Targeting.GetOpposingTeam(Team), vehicle.transform.position, float.PositiveInfinity);
        Vector3 wanderTo;
        var     randomSpherePoint = Random.insideUnitSphere;

        if (nearestTarget != null)
        {
            wanderTo = (vehicle.transform.position - nearestTarget.transform.position) + 30f * new Vector3(randomSpherePoint.x, 0f, randomSpherePoint.z);
        }
        else
        {
            wanderTo = vehicle.transform.position + vehicle.transform.forward * 10f + 10f * new Vector3(randomSpherePoint.x, 0f, randomSpherePoint.z);
        }

        var navPath = new UnityEngine.AI.NavMeshPath();

        if (UnityEngine.AI.NavMesh.CalculatePath(vehicle.transform.position, wanderTo, UnityEngine.AI.NavMesh.AllAreas, navPath))
        {
            path = navPath.corners;
        }
        else
        {
            path = new[] { vehicle.transform.position };
        }
        curPathIndex = 0;
    }
예제 #2
0
    private void Start()
    {
        triggerCollider = GetComponent <SphereCollider>();
        owner           = GetComponentInParent <AutonomousAgent>();
        opposingTeam    = Targeting.GetOpposingTeam(owner.Team);

        lastColliders = new List <Collider>();
        StartCoroutine(CheckInside(Random.Range(1f, 2f)));
    }
예제 #3
0
    public void SetTeam(Team team)
    {
        opposingTeam    = Targeting.GetOpposingTeam(team);
        enemyAgents     = new List <ActorAgent>();
        triggerCollider = GetComponent <SphereCollider>();
        lastColliders   = new List <Collider>();

        StartCoroutine(CheckInside(Random.Range(0.5f, 1f)));
    }
예제 #4
0
    private void Follow()
    {
        if (path == null)
        {
            AssignFollowPosition();
        }
        var followForce = GetFollowForce();

        var pitchYaw = GetSteerToPoint(path[curPathIndex]);

        vehicle.SetPitchYaw(0f, pitchYaw.y);
        var forward = Vector3.Dot(followForce, vehicle.transform.forward);
        var strafe  = Vector3.Dot(followForce, vehicle.transform.right);

        vehicle.SetAimAt(path[curPathIndex] + vehicle.PathAimHeight * Vector3.up + vehicle.transform.forward);
        vehicle.SetRun(false);
        vehicle.SetMove(forward, strafe);

        if (!reachedPathEnd)
        {
            var toPathDestination = path[curPathIndex] - vehicle.transform.position;
            if (toPathDestination.sqrMagnitude <= 1f)
            {
                curPathIndex++;
                if (curPathIndex == path.Length)
                {
                    reachedPathEnd = true;
                }
            }
        }

        if (reachedPathEnd)
        {
            AssignWanderPosition();
            reachedPathEnd = false;
            curPathIndex   = 0;
            //vehicle.SetMove(0f, 0f);
        }

        // Reconsider target
        if (targetReconsiderCooldown > 0f)
        {
            targetReconsiderCooldown -= Time.deltaTime;
            if (targetReconsiderCooldown < 0f)
            {
                target = Targeting.FindNearest(Targeting.GetOpposingTeam(Team), vehicle.transform.position, 100f);
                targetReconsiderCooldown = TargetReconsiderRate;
            }
        }

        if (HasTarget())
        {
            state = NpcSoldierState.Chase;
        }
    }
예제 #5
0
 private void OnVehicleDamage(Collider hitCollider, Vector3 position, Vector3 direction, float power, float damage, GameObject attacker)
 {
     if (attacker != null)
     {
         var targetCandidate = attacker.GetComponentInParent <ActorAgent>();
         if (targetCandidate != null)
         {
             if (targetCandidate.Team == Targeting.GetOpposingTeam(Team))
             {
                 target = targetCandidate.GetVehicle();
                 SetTarget(target);
                 AlertNeighbours(target);
             }
         }
     }
 }
예제 #6
0
    private void Chase()
    {
        AssignChasePosition();
        if (!reachedPathEnd)
        {
            var toPathDestination = path[curPathIndex] - vehicle.transform.position;
            if (toPathDestination.sqrMagnitude <= 1f)
            {
                curPathIndex++;
                if (curPathIndex == path.Length)
                {
                    reachedPathEnd = true;
                    curPathIndex   = 0;
                }
            }
        }

        if (reachedPathEnd)
        {
            reachedPathEnd = false;
        }

        var chaseForce = GetChaseForce();

        Vehicle targetVehicle = null;

        if (HasTarget())
        {
            targetVehicle = target.GetComponent <Vehicle>();
        }
        if (targetVehicle != null)
        {
            var pitchYaw = GetSteerToPoint(targetVehicle.transform.position);
            vehicle.SetPitchYaw(0f, pitchYaw.y);

            var toTarget = targetVehicle.transform.position - GetVehicle().transform.position;
            if (toTarget.sqrMagnitude < AttackDistance * AttackDistance)
            {
                isAttacking = true;

                if (isAttacking != wasAttacking)
                {
                    vehicle.AlertNeighbours();
                }

                var aimAt     = targetVehicle.transform.position + vehicle.GetPrimaryWeapon().AimtHeight *Vector3.up + GetAimRadius(toTarget.sqrMagnitude) * Random.insideUnitSphere;
                var shootFrom = vehicle.GetPrimaryWeaponShootPoint();
                var aimRay    = new Ray(shootFrom, aimAt - shootFrom);

                vehicle.SetAimAt(aimAt);

                var angleToTarget = Vector3.Angle(aimRay.direction, vehicle.GetPrimaryWeaponAimDirection());
                if (Mathf.Abs(angleToTarget) < AimAngleTolerance)
                {
                    // Avoid shooting friends
                    RaycastHit aimHit;
                    var        dontShoot = false;
                    if (Physics.Raycast(aimRay, out aimHit, AttackDistance, ~LayerMask.GetMask("Sensors", "MissileSensors")))
                    {
                        if (aimHit.collider.gameObject.layer == LayerMask.NameToLayer("Terrain"))
                        {
                            if (aimHit.distance < 2f)
                            {
                                dontShoot = true;
                            }
                        }

                        var npc = aimHit.collider.GetComponentInParent <ActorAgent>();
                        if (npc != null)
                        {
                            if (npc.Team == Team)
                            {
                                dontShoot = true;
                            }
                        }
                        else
                        {
                            var hitDistance = aimHit.distance;
                            if (hitDistance * hitDistance < toTarget.sqrMagnitude)
                            {
                                dontShoot = true;
                            }
                        }
                        var tooClose = vehicle.GetPrimaryWeapon().SplashRadius() + 5f;
                        if (aimHit.distance < tooClose)
                        {
                            dontShoot = true;
                        }
                    }

                    if (!dontShoot)
                    {
                        vehicle.TriggerPrimaryWeapon();
                    }
                    else
                    {
                        vehicle.ReleasePrimaryWeapon();
                    }
                }
                else
                {
                    vehicle.ReleasePrimaryWeapon();
                }
                vehicle.SetRun(false);
            }
            else
            {
                isAttacking = false;
                vehicle.SetAimAt(path[curPathIndex] + vehicle.PathAimHeight * Vector3.up + vehicle.transform.forward);
                vehicle.ReleasePrimaryWeapon();
                vehicle.SetRun(true);
            }
        }
        else
        {
            isAttacking = false;
            vehicle.ReleasePrimaryWeapon();
        }

        wasAttacking = isAttacking;

        var forward = Vector3.Dot(chaseForce, vehicle.transform.forward);
        var strafe  = Vector3.Dot(chaseForce, vehicle.transform.right);

        vehicle.SetMove(forward, strafe);

        // Reconsider target
        if (targetReconsiderCooldown > 0f)
        {
            targetReconsiderCooldown -= Time.deltaTime;
            if (targetReconsiderCooldown < 0f)
            {
                target = Targeting.FindNearest(Targeting.GetOpposingTeam(Team), vehicle.transform.position, 100f);
                targetReconsiderCooldown = TargetReconsiderRate;
            }
        }

        if (!HasTarget())
        {
            SetNoTargetState();
        }
    }
예제 #7
0
 private void Awake()
 {
     owner        = GetComponentInParent <AutonomousAgent>();
     opposingTeam = Targeting.GetOpposingTeam(owner.Team);
 }