Exemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        NavMeshHit hit;

        agent.FindClosestEdge(out hit);
        var rot = Quaternion.LookRotation(hit.normal);

        transform.rotation = Quaternion.Slerp(transform.rotation, rot, 3.0f * Time.deltaTime);


        enemies  = GameObject.FindGameObjectsWithTag("Enemy");
        enemiesT = new Transform[enemies.Length];
        for (int i = 0; i < enemies.Length; i++)
        {
            enemiesT [i] = enemies [i].transform;
        }
        closestBH = GetClosestEnemy(enemiesT);


        timer -= Time.deltaTime;

        if (timer <= 0.0f)
        {
            if ((closestBH != null) && Vector3.Distance(transform.position, closestBH.position) < 150.0f)
            {
                Quaternion turretRotation = Quaternion.LookRotation(closestBH.position - turret.position);
                turret.rotation = Quaternion.Slerp(turret.rotation, turretRotation, Time.deltaTime * 500.0f);
                ShootBullet();
            }
            timer = interval;
        }
    }
        void DoFindClosestEdge()
        {
            if (_agent == null)
            {
                return;
            }

            NavMeshHit _NavMeshHit;

            bool _nearestEdgeFound = _agent.FindClosestEdge(out _NavMeshHit);

            nearestEdgeFound.Value = _nearestEdgeFound;

            position.Value = _NavMeshHit.position;
            normal.Value   = _NavMeshHit.normal;
            distance.Value = _NavMeshHit.distance;
            mask.Value     = _NavMeshHit.mask;
            hit.Value      = _NavMeshHit.hit;

            if (_nearestEdgeFound)
            {
                if (!FsmEvent.IsNullOrEmpty(nearestEdgeFoundEvent))
                {
                    Fsm.Event(nearestEdgeFoundEvent);
                }
            }
            else
            {
                if (!FsmEvent.IsNullOrEmpty(nearestEdgeNotFoundEvent))
                {
                    Fsm.Event(nearestEdgeNotFoundEvent);
                }
            }
        }
Exemplo n.º 3
0
        public override TaskStatus OnUpdate()
        {
            if (_agent == null)
            {
                Debug.LogWarning("NavMeshAgent is null");
                return(TaskStatus.Failure);
            }

            //Debug.Log(_agent + " agent");

            NavMeshHit _NavMeshHit;

            bool _nearestEdgeFound = _agent.FindClosestEdge(out _NavMeshHit);

            nearestEdgeFound.Value = _nearestEdgeFound;

            position.Value = new Vector3(_NavMeshHit.position.x, NavAgentGameObject.Value.gameObject.transform.position.y, _NavMeshHit.position.z);
            //Debug.Log("Edge position " + position.Value);
            normal.Value   = _NavMeshHit.normal;
            distance.Value = _NavMeshHit.distance;
            mask.Value     = _NavMeshHit.mask;
            hit.Value      = _NavMeshHit.hit;

            if (nearestEdgeFound.Value == true)
            {
                //Debug.Log("SUCCESS edge found");
                return(TaskStatus.Success);
            }
            else
            {
                //Debug.Log("FAILURE NO edge found");
                return(TaskStatus.Failure);
            }
        }
    void Move()
    {
        if (isManualControll)
        {
            _rigidbody.AddForce(manualForce * Time.fixedDeltaTime * verticalMovementPower);
            _rigidbody.AddForce(-_rigidbody.velocity * Time.fixedDeltaTime * verticalMovementDamper);
        }
        else
        {
            if (!_targetAgentInstance.isOnNavMesh)
            {
                NavMeshHit hit;
                bool       isHitting = _targetAgentInstance.FindClosestEdge(out hit);
                Debug.Assert(isHitting, "Set NavMesh.");
                ChangeCurrentAgentPosition(hit.position);
            }
            var target = _targetAgentInstance.transform.position;
            var diff   = transform.position - target;
            diff = Vector3.ClampMagnitude(diff, _maxDifference);
            _rigidbody.AddForce(-diff * Time.fixedDeltaTime * verticalMovementPower);
            _rigidbody.AddForce(-_rigidbody.velocity * Time.fixedDeltaTime * verticalMovementDamper);
        }

        var velocityOnPlane = _rigidbody.velocity;

        velocityOnPlane.y = 0f;

        var     fixedVelocity = _rigidbody.velocity; fixedVelocity.y = 0f;
        Vector3 yaw           = Vector3.Project(Vector3.Cross(fixedVelocity.normalized, transform.forward), Vector3.up);
        var     yawForce      = -yaw * 100000f;

        _rigidbody.AddTorque(Clamp(yawForce, 0, 20000f));

        // Vector3 pitch = Vector3.ProjectOnPlane(Vector3.Cross(fixedVelocity*0.1f, transform.forward), transform.right);
        // Vector3 pitch = Vector3.ProjectOnPlane(Vector3.Cross(fixedVelocity*0.1f, transform.forward), transform.right);
        Vector3 pitch      = -Vector3.Project(Vector3.Cross(Vector3.up, transform.up), transform.right) - transform.right * _rigidbody.velocity.y * 0.002f;
        var     pitchForce = pitch * 100000f;

        _rigidbody.AddTorque(Clamp(pitchForce, 0, 20000f));


        if (1.0f < velocityOnPlane.magnitude)
        {
            // Quaternion targetRot = Quaternion.LookRotation(fixedVelocity, Vector3.up);
            // float y = Vector3.Project(fixedVelocity.normalized, transform.up);
            // Quaternion targetRot = Quaternion.FromToRotation(transform.forward, fixedVelocity);
            // _rigidbody.AddTorque(targetRot.eulerAngles*10f);
            // _rigidbody.AddTorque(-targetRot.eulerAngles*10f);
            // var q = Quaternion.RotateTowards(_rigidbody.rotation, targetRot, 50f * Time.fixedDeltaTime);
            // _rigidbody.MoveRotation(q);
        }
        Vector3 rollBaseVector = Vector3.ProjectOnPlane(Vector3.up, transform.forward).normalized;
        Vector3 roll           = Vector3.Project(Vector3.Cross(rollBaseVector, transform.up), transform.forward);
        var     rollForce      = -roll * 100000f;

        _rigidbody.AddTorque(Clamp(rollForce, 0, 20000f));

        _rigidbody.AddTorque(-_rigidbody.angularVelocity * 20000f);
    }
Exemplo n.º 5
0
    static int FindClosestEdge(IntPtr L)
    {
        LuaScriptMgr.CheckArgsCount(L, 2);
        NavMeshAgent obj  = LuaScriptMgr.GetNetObject <NavMeshAgent>(L, 1);
        NavMeshHit   arg0 = LuaScriptMgr.GetNetObject <NavMeshHit>(L, 2);
        bool         o    = obj.FindClosestEdge(out arg0);

        LuaScriptMgr.Push(L, o);
        LuaScriptMgr.PushValue(L, arg0);
        return(2);
    }
Exemplo n.º 6
0
    static int FindClosestEdge(IntPtr L)
    {
        LuaScriptMgr.CheckArgsCount(L, 2);
        NavMeshAgent obj = (NavMeshAgent)LuaScriptMgr.GetUnityObjectSelf(L, 1, "NavMeshAgent");
        NavMeshHit   arg0;
        bool         o = obj.FindClosestEdge(out arg0);

        LuaScriptMgr.Push(L, o);
        LuaScriptMgr.PushValue(L, arg0);
        return(2);
    }
Exemplo n.º 7
0
    void CheckForBadPaths()
    {
        if (navAgent.pathStatus == NavMeshPathStatus.PathInvalid || navAgent.pathStatus == NavMeshPathStatus.PathPartial)
        {
            Debug.Log("yikes we got pretty lost: " + navAgent.pathStatus);
            navAgent.ResetPath();

            // this sucks, just run to a wall
            NavMeshHit hit;
            if (navAgent.FindClosestEdge(out hit))
            {
                navAgent.SetDestination(hit.position);
            }
        }
    }
Exemplo n.º 8
0
    // Update is called once per frame
    void Update()
    {
        Quaternion wanderrotation = Quaternion.LookRotation(NextWanderPosition - transform.position);

        transform.rotation = Quaternion.Slerp(transform.rotation, wanderrotation, Time.deltaTime * 2.0f);
        transform.Translate(Vector3.forward * Time.deltaTime * 300.0f);

        if (Vector3.Distance(transform.position, NextWanderPosition) < 50.0f)
        {
            FindNextWanderPoint();
        }

        NavMeshHit hit;

        agent.FindClosestEdge(out hit);
        var rot = Quaternion.LookRotation(hit.normal + transform.forward);

        transform.rotation = Quaternion.Slerp(transform.rotation, rot, 5.0f * Time.deltaTime);
    }
Exemplo n.º 9
0
 public virtual void Follow()
 {
     //follow the player
     target = TargetNearestPlayer();
     if (!navAgent.isOnNavMesh)
     {
         NavMeshHit _hit;
         navAgent.FindClosestEdge(out _hit);
         navAgent.Warp(_hit.position);
     }
     if (navAgent.remainingDistance > 1f || navAgent.remainingDistance == 0)
     {
         navAgent.SetDestination(target.transform.position);
         timeBetweenAttacks += 0.1f;
     }
     if (!navAgent.hasPath)
     {
         navAgent.SetDestination(target.transform.position);
     }
 }
Exemplo n.º 10
0
 void Update()
 {
     if (_playerInfo.IsHiding)
     {
         NavMeshHit hit;
         if (_agent.FindClosestEdge(out hit))
         {
             _destination = hit.position;
             _agent.SetDestination(_destination);
         }
     }
     else
     {
         var distanceToPlayerSq = (_player.position - _destination).sqrMagnitude;
         if (distanceToPlayerSq > maxDistance * maxDistance || distanceToPlayerSq < minDistance * minDistance)
         {
             //MoveToRandomPointNearPlayer();
             FollowPlayer();
         }
     }
 }
Exemplo n.º 11
0
    // Update is called once per frame
    void Update()
    {
        Quaternion targetRotation = Quaternion.LookRotation(pointList.transform.position - transform.position);

        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0f);
        transform.Translate(Vector3.forward * Time.deltaTime * 150.0f);

        building = GameObject.FindGameObjectsWithTag("building");
        if (building.Length <= 0)
        {
            GameObject.Find("GameController").gameObject.SendMessage("GameEnd");
        }
        buildingsT = new Transform[building.Length];

        for (int i = 0; i < building.Length; i++)
        {
            buildingsT[i] = building[i].transform;
        }

        closestB = GetClosestEnemy(buildingsT);
        timer   -= Time.deltaTime;

        if (timer <= 0.0f)
        {
            if (Vector3.Distance(transform.position, closestB.position) < 300.0f)
            {
                Quaternion turretRotation = Quaternion.LookRotation(closestB.position - turret.position);
                turret.rotation = Quaternion.Slerp(turret.rotation, turretRotation, Time.deltaTime * 100.0f);
                ShootBullet();
            }
            timer = interval;
        }

        NavMeshHit hit;

        agent.FindClosestEdge(out hit);
        var rot = Quaternion.LookRotation(hit.normal + transform.forward);

        transform.rotation = Quaternion.Slerp(transform.rotation, rot, 3.0f * Time.deltaTime);
    }
Exemplo n.º 12
0
 public override TaskStatus Run()
 {
     if (Compare.Count != Vision.Count)
     {
         NavMeshHit hit = new NavMeshHit();
         hit.position = Vision[0].position;
         if (agent.FindClosestEdge(out hit))
         {
             DrawCircle(Vision[0].position, hit.distance, Color.red);
             //if player is in sight of enemy
             //navmesh.setdestiantion
             foreach (Transform target in EnemiesVision)
             {
                 if (target == Player.transform)
                 {
                     agent.SetDestination(Vision[0].position);
                     return(TaskStatus.Success);
                 }
             }
         }
     }
     return(TaskStatus.Failed);
 }
Exemplo n.º 13
0
    IEnumerator Action()
    {
        while (!isDie)
        {
            yield return(ws);

            switch (state)
            {
            case State.DIE:
                agent.isStopped = true;
                agent.ResetPath();
                ecoState = EcoState.NONE;
                animator.SetBool(zombieData.HashMove, false);
                isDie         = true;
                transform.tag = "Food";
                //StopAllCoroutines();
                yield return(new WaitForSeconds(15.0f));

                ResetZombieStat();
                break;

            case State.IDLE:
                if (ecoState != EcoState.NONE)
                {
                    break;
                }
                agent.isStopped = true;
                animator.SetBool(zombieData.HashMove, false);
                break;

            case State.PATROL:
                if (ecoState != EcoState.NONE)
                {
                    break;
                }
                agent.isStopped = false;
                animator.SetBool(zombieData.HashMove, true);
                agent.speed = zombieData.PatrolSpeed;
                if (!agent.hasPath || Vector3.Distance(agent.pathEndPosition, transform.position) <= agent.stoppingDistance)
                {
                    agent.SetDestination(SetRandomPoint(transform, zombieData.PatrolRadius));
                }
                break;

            case State.CHASING:
                if (animator.GetBool(zombieData.HashNotFoundTarget))
                {
                    animator.SetTrigger(zombieData.HashFoundTarget);
                }
                agent.isStopped = false;
                if (!isCalling)
                {
                    NearZombieAttack();
                    isCalling = true;
                }
                animator.SetBool(zombieData.HashMove, true);
                agent.speed = zombieData.ChaseSpeed;
                bool chaseTargetMode;
                if (NavMesh.CalculatePath(transform.position, targetPos, NavMesh.AllAreas, new NavMeshPath()))
                {
                    notHavePath = false;
                    if (mode == Mode.Spread)
                    {
                        if (randomPos == null || Vector3.Distance(targetPos, randomPos) > zombieData.SpreadDist)
                        {
                            CheckTargetPosition(zombieData.SpreadDist);
                        }
                    }
                    chaseTargetMode = (mode == Mode.Basic ||
                                       Vector3.Distance(targetPos, transform.position) <= zombieData.SpreadDist);
                    agent.SetDestination(chaseTargetMode ? targetPos : randomPos);
                }
                else
                {
                    notHavePath = true;
                    NavMeshHit hit;
                    if (agent.FindClosestEdge(out hit))
                    {
                        agent.SetDestination(hit.position);
                    }
                }
                agent.stoppingDistance = zombieData.AttackDist;
                agent.autoBraking      = true;
                break;

            case State.ATTACK:
                agent.isStopped = true;
                animator.SetBool(zombieData.HashMove, false);
                if (notHavePath)
                {
                    animator.SetTrigger(zombieData.HashNotFoundTarget);
                    yield return(new WaitForSeconds(clipTimes["Scream"]));
                }
                else
                {
                    animator.SetTrigger(zombieData.HashAttack);
                    yield return(new WaitForSeconds(clipTimes["Attack"]));

                    NearZombieAttack();
                }
                break;

            case State.TRIGGER:
                if (ecoState != EcoState.NONE)
                {
                    break;
                }
                if (animator.GetBool(zombieData.HashNotFoundTarget))
                {
                    animator.SetTrigger(zombieData.HashFoundTarget);
                }
                agent.isStopped = false;
                agent.speed     = zombieData.ChaseSpeed;
                animator.SetBool(zombieData.HashMove, true);
                if (mode == Mode.Spread)
                {
                    if (randomPos == null || Vector3.Distance(targetPos, randomPos) > zombieData.SpreadDist)
                    {
                        CheckTargetPosition(zombieData.SpreadDist);
                    }
                }
                chaseTargetMode = (mode == Mode.Basic || Vector3.Distance(targetPos, transform.position) <= zombieData.SpreadDist);
                agent.SetDestination(chaseTargetMode ? targetPos : randomPos);
                if (agent.destination.x == 0 && agent.destination.z == 0)
                {
                    agent.SetDestination(new Vector3(targetPos.x, transform.position.y, targetPos.z));
                }
                break;
            }
        }
    }
Exemplo n.º 14
0
    private void UpdateHiding()
    {
        NavMeshHit hit;

        IsHiding = _agent.FindClosestEdge(out hit) && hit.distance < maxDistanceToHide;
    }
Exemplo n.º 15
0
 /*
  * LittleDude.FindClosestEdge
  *
  * The returned NavMeshHit object contains the position and details of the nearest point on the nearest edge of the Navmesh.
  * Since an edge typically corresponds to a wall or other large object, this could be used to make a character take cover as close to the wall as possible.
  */
 public bool FindClosestEdge(out NavMeshHit _hit)
 {
     return(navAgent.FindClosestEdge(out _hit));
 }