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;
    }
    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;
        }
    }
Exemple #3
0
    public void Trigger(Missile missile)
    {
        //Debug.Log("MISSILE SENSOR TRIGGERED!!!!");
        var missileOwner = missile.GetOwner();

        if (missileOwner != null)
        {
            var missileOwnerActor = missileOwner.GetComponentInParent <ActorAgent>();
            if (missileOwnerActor != null)
            {
                if (missileOwnerActor.Team == opposingTeam)
                {
                    var target = Targeting.FindNearest(opposingTeam, owner.GetVehicle().transform.position, 500f);
                    owner.SetTarget(target);
                }
            }
        }
    }
Exemple #4
0
    private IEnumerator CheckInside(float delay)
    {
        yield return(new WaitForSeconds(delay));

        var recentColliders = Physics.OverlapSphere(transform.position, triggerCollider.radius, LayerMask.GetMask("Detectable")).ToList();

        var enteredColliders = recentColliders.Except(lastColliders).ToList();
        var exitedColliders  = lastColliders.Except(recentColliders).ToList();

        if (enteredColliders.Count > 0 || exitedColliders.Count > 0)
        {
            if (!owner.HasTarget())
            {
                var target = Targeting.FindNearest(opposingTeam, owner.GetVehicle().transform.position, triggerCollider.radius);
                owner.SetTarget(target);
            }
        }

        lastColliders = recentColliders;
        StartCoroutine(CheckInside(CheckFrequency));
    }
    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();
        }
    }