Ejemplo n.º 1
0
        public override Vector3[] GetPointsArray(Vector3 startPosition, Vector3 targetPosition, AC.Char _char = null)
        {
            NavMeshPath _path = new NavMeshPath();

            if (!NavMesh.CalculatePath(startPosition, targetPosition, -1, _path))
            {
                // Could not find path with current vectors
                float maxDistance  = 0.001f;
                float originalDist = Vector3.Distance(startPosition, targetPosition);

                NavMeshHit hit = new NavMeshHit();
                for (maxDistance = 0.001f; maxDistance < originalDist; maxDistance += 0.05f)
                {
                    if (NavMesh.SamplePosition(startPosition, out hit, maxDistance, -1))
                    {
                        startPosition = hit.position;
                        break;
                    }
                }

                bool foundNewEnd = false;
                for (maxDistance = 0.001f; maxDistance < originalDist; maxDistance += 0.05f)
                {
                    if (NavMesh.SamplePosition(targetPosition, out hit, maxDistance, -1))
                    {
                        targetPosition = hit.position;
                        foundNewEnd    = true;
                        break;
                    }
                }

                if (!foundNewEnd)
                {
                    return(new Vector3[0]);
                }

                NavMesh.CalculatePath(startPosition, targetPosition, -1, _path);
            }

            List <Vector3> pointArray = new List <Vector3>();

            for (int i = 0; i < _path.corners.Length; i++)
            {
                pointArray.Add(_path.corners[i]);
            }
            if (pointArray.Count > 1 && Mathf.Approximately(pointArray[0].x, startPosition.x) && Mathf.Approximately(pointArray[0].z, startPosition.x))
            {
                pointArray.RemoveAt(0);
            }
            else if (pointArray.Count == 0)
            {
                pointArray.Clear();
                pointArray.Add(targetPosition);
            }

            return(pointArray.ToArray());
        }
Ejemplo n.º 2
0
    public static bool HasNavMeshAtOrigin(int areaMask = -1, int agentTypeID = 0)
    {
        NavMeshHit         navMeshHit         = (NavMeshHit)null;
        NavMeshQueryFilter navMeshQueryFilter = (NavMeshQueryFilter)null;

        ((NavMeshQueryFilter) ref navMeshQueryFilter).set_areaMask(areaMask);
        ((NavMeshQueryFilter) ref navMeshQueryFilter).set_agentTypeID(agentTypeID);
        return(NavMesh.SamplePosition(Vector3.get_zero(), ref navMeshHit, 0.1f, navMeshQueryFilter));
    }
Ejemplo n.º 3
0
    /// <summary>
    /// Same as NavMesh.Raycast but all the calculations are done in the hidden surface.
    /// Trace a line between two points on the NavMesh.
    /// </summary>
    /// <param name="sourcePosition">The origin of the ray.</param>
    /// <param name="targetPosition">The end of the ray.</param>
    /// <param name="hit">Holds the properties of the ray cast resulting location.</param>
    /// <param name="areaMask">A bitfield mask specifying which NavMesh areas can be passed when tracing the ray.</param>
    /// <returns></returns>
    public static bool Raycast(Vector3 sourcePosition, Vector3 targetPosition, out NavMeshHit hit, int areaMask)
    {
        var hiddenSource = sourcePosition + HiddenTranslation;
        var hiddenTarget = targetPosition + HiddenTranslation;
        var result       = NavMesh.Raycast(hiddenSource, hiddenTarget, out hit, areaMask);

        hit.position -= HiddenTranslation;
        return(result);
    }
    public static bool HasNavMeshAtOrigin(int areaMask = NavMesh.AllAreas, int agentTypeID = 0)
    {
        var hit    = new NavMeshHit();
        var filter = new NavMeshQueryFilter();

        filter.areaMask    = areaMask;
        filter.agentTypeID = agentTypeID;
        return(NavMesh.SamplePosition(Vector3.zero, out hit, 0.1f, filter));
    }
Ejemplo n.º 5
0
    void Start()
    {
        NavMeshHit nHit = new NavMeshHit();
        Vector3    pos  = new Vector3(Random.Range(0, 128), 10f, Random.Range(0, 128));

        if (NavMesh.SamplePosition(pos, out nHit, 150f, 1))
        {
            Agent.SetDestination(nHit.position);
        }
    }
Ejemplo n.º 6
0
    /*
     * Set the target of the NavMeshAgent
     * Params:
     *      pos: the position of the target
     */
    public void SetTarget(Vector3 pos)
    {
        // check the position is on the navmesh
        NavMeshHit hit = new NavMeshHit();

        NavMesh.SamplePosition(pos, out hit, 5, -1);

        transform.rotation = Quaternion.LookRotation((hit.position - transform.position).normalized); // start the ant looking at its destination
        m_agent.SetDestination(hit.position);                                                         // set the destination of the agent
    }
    IEnumerator Patrullar()
    {
        Wachin.Rifle = false;

        var offsetDePatrulla = Quaternion.Euler(0f, Random.value * 360f, 0f) * Vector3.right;
        var distOffsetDePatrulla = Random.value * (liderazgo ? liderazgo.rangoAutoAsociarPatrulla : 1f);
        var cambiarPosicionCuando = Time.time + Random.Range(cambiarPosicionCada[0], cambiarPosicionCada[1]);

        while (!_objetivo)
        {
            if (liderazgo)
            {
                if (Patrulla.Lider == this)
                {
                    liderazgo.LiderarPatrulla(Patrulla);
                    Wachin.PosBuscada = Patrulla.destinoPatrulla;
                }
                else
                {
                    var newPos = Patrulla.PosLider + offsetDePatrulla * distOffsetDePatrulla;
                    var hit = new NavMeshHit();
                    if (NavMesh.Raycast(Patrulla.PosLider, newPos, out hit, NavMesh.AllAreas))
                    {
                        Wachin.PosBuscada = hit.position;
                    }
                    else Wachin.PosBuscada = newPos;
                }
                Wachin.MiraHacia = Wachin.PosBuscada;
            }

            if (Time.time >= cambiarPosicionCuando)
            {
                cambiarPosicionCuando = Time.time + Random.Range(cambiarPosicionCada[0], cambiarPosicionCada[1]);
                offsetDePatrulla = Quaternion.Euler(0f, Random.value * 360f, 0f) * offsetDePatrulla;
                distOffsetDePatrulla = Random.value * liderazgo.rangoAutoAsociarPatrulla;
            }

            var objetivoDePatrulla = Patrulla.TomarObjetivoRandom();
            if (objetivoDePatrulla)
            {
                _objetivo = objetivoDePatrulla;
            }
            else
            {
                var nuevoObjetivo = VigilarPorNuevoObjetivo();
                if (nuevoObjetivo)
                {
                    Patrulla.NuevoObjetivo(_objetivo = nuevoObjetivo);
                    yield break;
                }
            }

            yield return null;
        }
    }
Ejemplo n.º 8
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);
    }
Ejemplo n.º 9
0
    private Vector3 RunAwayPoint(Vector3 origin, float dist, int layerMask)
    {
        Vector3 runDirection = _player.transform.forward * dist;

        runDirection += origin;

        NavMeshHit navMeshHit = new NavMeshHit();

        NavMesh.SamplePosition(runDirection, out navMeshHit, dist, layerMask);
        return(navMeshHit.position);
    }
Ejemplo n.º 10
0
    private Vector3 RandomNavPoint(Vector3 origin, float dist, int layerMask)
    {
        Vector3 randomDirection = Random.insideUnitSphere * dist;

        randomDirection += origin;

        NavMeshHit navMeshHit = new NavMeshHit();

        NavMesh.SamplePosition(randomDirection, out navMeshHit, dist, layerMask);
        return(navMeshHit.position);
    }
    // This seems to be a bit too heavy atm
    private Vector3 SamplePosition(Vector3 position, float range)
    {
        NavMeshHit hit = new NavMeshHit();

        if (NavMesh.SamplePosition(position, out hit, range, WALKABLE_AREA_MASK))
        {
            Debug.DrawRay(hit.position, Vector3.up, Color.blue, 5);
            return(hit.position);
        }
        return(position);
    }
Ejemplo n.º 12
0
    void WarpPlayer(Vector3 newpos)
    {
        NavMeshHit hit = new NavMeshHit();

        if (NavMesh.SamplePosition(newpos, out hit, 4f, NavMesh.AllAreas))
        {
            agent.Warp(hit.position);
        }

        agent.Warp(newpos);
    }
Ejemplo n.º 13
0
    static int FindClosestEdge(IntPtr L)
    {
        LuaScriptMgr.CheckArgsCount(L, 3);
        Vector3    arg0 = LuaScriptMgr.GetNetObject <Vector3>(L, 1);
        NavMeshHit arg1 = LuaScriptMgr.GetNetObject <NavMeshHit>(L, 2);
        int        arg2 = (int)LuaScriptMgr.GetNumber(L, 3);
        bool       o    = NavMesh.FindClosestEdge(arg0, out arg1, arg2);

        LuaScriptMgr.Push(L, o);
        LuaScriptMgr.PushValue(L, arg1);
        return(2);
    }
Ejemplo n.º 14
0
    public void CheckMask()
    {
        NavMeshHit hit = new NavMeshHit();

        nav.SamplePathPosition(NavMesh.AllAreas, 0.0f, out hit);
        int ll = LayerMask.GetMask("Human");

        if (hit.mask != ll)
        {
            Accroupi();
        }
    }
Ejemplo n.º 15
0
    protected virtual void Awake()
    {
        // get components
        healthText = GetComponentInChildren <Text>();
        if (healthText)
        {
            if (health > maxHealth)
            {
                health = maxHealth;
            }
            RefreshHealthText();
        }

        spriteRenderer = GetComponent <SpriteRenderer>();

        teamColor = transform.GetChild(0).GetComponent <SpriteRenderer>();
        if (!teamColor)
        {
            Debug.LogWarning("Can't find team color object of " + name);
        }

        normalColor = spriteRenderer.color;
        dimmedColor = normalColor * 0.8f;

        obstacle = GetComponent <NavMeshObstacle>();
        if (!obstacle)
        {
            obstacle         = gameObject.AddComponent <NavMeshObstacle>();
            obstacle.carving = true;
        }

        hitParticles = GetComponentInChildren <ParticleSystem>();
        // ///////////////////////////////////////////////////

        // create terrain mask
        foreach (TerrainType terrain in availableTerrains)
        {
            terrainMask |= (1 << NavMesh.GetAreaFromName(terrain.ToString()));
        }

        // place object on NavMesh
        NavMeshHit navMeshHit = new NavMeshHit();

        if (NavMesh.SamplePosition(transform.position, out navMeshHit, 0.5f, terrainMask))
        {
            transform.position    = navMeshHit.position;
            transform.eulerAngles = new Vector3(90, 0, 0);
        }
        else
        {
            Debug.LogWarning("Can't place '" + name + "' (" + GetType() + ") object on a NavMesh!");
        }
    }
Ejemplo n.º 16
0
    void ShiftPosition()
    {
        bool       inMesh = false;
        NavMeshHit hit    = new NavMeshHit();

        while (!inMesh)
        {
            inMesh = NavMesh.SamplePosition(RandomInCircle(transform.position, moveRange), out hit, 1f, NavMesh.AllAreas);
        }

        transform.position = hit.position;
    }
Ejemplo n.º 17
0
    static int Raycast(IntPtr L)
    {
        LuaScriptMgr.CheckArgsCount(L, 3);
        NavMeshAgent obj  = LuaScriptMgr.GetNetObject <NavMeshAgent>(L, 1);
        Vector3      arg0 = LuaScriptMgr.GetNetObject <Vector3>(L, 2);
        NavMeshHit   arg1 = LuaScriptMgr.GetNetObject <NavMeshHit>(L, 3);
        bool         o    = obj.Raycast(arg0, out arg1);

        LuaScriptMgr.Push(L, o);
        LuaScriptMgr.PushValue(L, arg1);
        return(2);
    }
Ejemplo n.º 18
0
    public static Vector3 NavSamplePosition(Vector3 srcPosition)
    {
        Vector3    dstPosition = srcPosition;
        NavMeshHit meshHit     = new NavMeshHit();
        int        layer       = 1 << NavMesh.GetAreaFromName("Walkable");

        if (NavMesh.SamplePosition(srcPosition, out meshHit, 100, layer))
        {
            dstPosition = meshHit.position;
        }
        return(dstPosition);
    }
Ejemplo n.º 19
0
    static int Raycast(IntPtr L)
    {
        LuaScriptMgr.CheckArgsCount(L, 4);
        Vector3    arg0 = LuaScriptMgr.GetNetObject <Vector3>(L, 1);
        Vector3    arg1 = LuaScriptMgr.GetNetObject <Vector3>(L, 2);
        NavMeshHit arg2 = LuaScriptMgr.GetNetObject <NavMeshHit>(L, 3);
        int        arg3 = (int)LuaScriptMgr.GetNumber(L, 4);
        bool       o    = NavMesh.Raycast(arg0, arg1, out arg2, arg3);

        LuaScriptMgr.Push(L, o);
        LuaScriptMgr.PushValue(L, arg2);
        return(2);
    }
Ejemplo n.º 20
0
    static int SamplePathPosition(IntPtr L)
    {
        LuaScriptMgr.CheckArgsCount(L, 4);
        NavMeshAgent obj  = LuaScriptMgr.GetNetObject <NavMeshAgent>(L, 1);
        int          arg0 = (int)LuaScriptMgr.GetNumber(L, 2);
        float        arg1 = (float)LuaScriptMgr.GetNumber(L, 3);
        NavMeshHit   arg2 = LuaScriptMgr.GetNetObject <NavMeshHit>(L, 4);
        bool         o    = obj.SamplePathPosition(arg0, arg1, out arg2);

        LuaScriptMgr.Push(L, o);
        LuaScriptMgr.PushValue(L, arg2);
        return(2);
    }
Ejemplo n.º 21
0
    static int SamplePosition(IntPtr L)
    {
        LuaScriptMgr.CheckArgsCount(L, 4);
        Vector3    arg0 = LuaScriptMgr.GetNetObject <Vector3>(L, 1);
        NavMeshHit arg1 = LuaScriptMgr.GetNetObject <NavMeshHit>(L, 2);
        float      arg2 = (float)LuaScriptMgr.GetNumber(L, 3);
        int        arg3 = (int)LuaScriptMgr.GetNumber(L, 4);
        bool       o    = NavMesh.SamplePosition(arg0, out arg1, arg2, arg3);

        LuaScriptMgr.Push(L, o);
        LuaScriptMgr.PushValue(L, arg1);
        return(2);
    }
Ejemplo n.º 22
0
    public override void OnCreate()
    {
        base.OnCreate();
        cAnimator            = GetComponentInChildren <Animator>();
        Controller           = gameObject.AddUniqueComponent <CharacterController>();
        Agent                = gameObject.AddUniqueComponent <NavMeshAgent>();
        Agent.updatePosition = false; //关闭NavMeshAgent的自带路径行走
        Agent.updateRotation = false; //关闭NavMeshAgent的自带朝向旋转
        mNavMeshPath         = new NavMeshPath();
        mNavMeshHit          = new NavMeshHit();

        Attr.AttrIntChangedDelegate = OnAttrChange;
    }
Ejemplo n.º 23
0
 protected override Vector3 GetDropLocation()
 {
     // We might need to try more than once to get on the NavMesh
     for (int i = 0; i < ATTEMPTS; i++)
     {
         Vector3    randomPoint = this.transform.position + Random.insideUnitSphere * scatterDistance;
         NavMeshHit hit         = new NavMeshHit();
         if (NavMesh.SamplePosition(randomPoint, out hit, 0.1f, NavMesh.AllAreas))
         {
             return(hit.position);
         }
     }
     return(this.transform.position);
 }
Ejemplo n.º 24
0
    /// <summary>
    /// 是否靠近边界 by邓成
    /// </summary>
    /// <param name="desPos"></param>
    /// <returns></returns>
    bool IsClosingEdge(Vector3 desPos)
    {
        NavMeshHit navMeshEdge = new NavMeshHit();
        bool       edgeFound   = NavMesh.FindClosestEdge(desPos, out navMeshEdge, NavMesh.AllAreas);

        if (edgeFound)
        {
            if ((desPos - navMeshEdge.position).sqrMagnitude < 0.05f)
            {
                return(true);
            }
        }
        return(false);
    }
Ejemplo n.º 25
0
        private void Start()
        {
            EnemyModifiers     = new List <EnemyModifier>();
            Agent              = gameObject.AddComponent <NavMeshAgent>();
            Agent.speed        = 4;
            Agent.radius       = 1;
            Agent.acceleration = 2;
            Agent.autoRepath   = true;
            Agent.destination  = WayPoints[position];


            _path     = new NavMeshPath();
            _agentHit = new NavMeshHit();
        }
Ejemplo n.º 26
0
        public override Vector3[] GetPointsArray(Vector3 startPosition, Vector3 targetPosition)
        {
            NavMeshHit  hit   = new NavMeshHit();
            NavMeshPath _path = new NavMeshPath();

            if (!NavMesh.CalculatePath(startPosition, targetPosition, -1, _path))
            {
                // Could not find path with current vectors
                float maxDistance = 0.001f;

                for (maxDistance = 0.001f; maxDistance < 1f; maxDistance += 0.05f)
                {
                    if (NavMesh.SamplePosition(startPosition, out hit, maxDistance, -1))
                    {
                        startPosition = hit.position;
                        break;
                    }
                }

                for (maxDistance = 0.001f; maxDistance < 1f; maxDistance += 0.05f)
                {
                    if (NavMesh.SamplePosition(targetPosition, out hit, maxDistance, -1))
                    {
                        targetPosition = hit.position;
                        break;
                    }
                }

                NavMesh.CalculatePath(startPosition, targetPosition, -1, _path);
            }

            List <Vector3> pointArray = new List <Vector3>();

            foreach (Vector3 corner in _path.corners)
            {
                pointArray.Add(corner);
            }
            if (pointArray.Count > 1 && pointArray[0].x == startPosition.x && pointArray[0].z == startPosition.z)
            {
                pointArray.RemoveAt(0);
            }
            else if (pointArray.Count == 0)
            {
                pointArray.Clear();
                pointArray.Add(targetPosition);
            }

            return(pointArray.ToArray());
        }
Ejemplo n.º 27
0
    private Vector3 RandomPointOnMap(int areaMask)
    {
        bool       foundPos   = false;
        NavMeshHit navMeshHit = new NavMeshHit();
        int        reTryCount = 0;

        while (!foundPos || reTryCount < 10)
        {
            Vector2 randomCirclePos = Random.insideUnitCircle;
            Vector3 targetPos       = new Vector3(randomCirclePos.x * mapPivotReference.localScale.x * 0.5f, 0, randomCirclePos.y * mapPivotReference.localScale.z * 0.5f) + mapPivotReference.position;
            foundPos = NavMesh.SamplePosition(targetPos, out navMeshHit, mapPivotReference.localScale.magnitude * 2, areaMask);
            reTryCount++;
        }
        return(foundPos ? navMeshHit.position : Vector3.zero);
    }
Ejemplo n.º 28
0
    public static Vector3 RandomOnCircle(Vector3 center, float min, float max)
    {
        Vector3 randomPoint = UnityEngine.Random.insideUnitCircle * max;

        randomPoint += randomPoint.normalized;
        center      += randomPoint;
        NavMeshHit meshHit = new NavMeshHit();
        int        layer   = 1 << NavMesh.GetAreaFromName("Walkable");

        if (NavMesh.SamplePosition(center, out meshHit, 100, layer))
        {
            return(meshHit.position);
        }
        return(center);
    }
Ejemplo n.º 29
0
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            LastPos = Input.mousePosition;
        }

        if (Input.GetMouseButton(0))
        {
            transform.Translate(new Vector3(Input.mousePosition.x - LastPos.x, 0, Input.mousePosition.y - LastPos.y) * Time.deltaTime);
        }

        Vector3 pos = transform.position;

        pos.x = Mathf.Clamp(pos.x, Bounds.x, Bounds.y);
        pos.z = Mathf.Clamp(pos.z, Bounds.z, Bounds.w);
        transform.position = pos;

        if (Input.GetMouseButtonDown(0))
        {
            Ray        ray    = Cam.ScreenPointToRay(Input.mousePosition);
            RaycastHit rayHit = new RaycastHit();
            if (Physics.Raycast(ray, out rayHit))
            {
                NavMeshAgent t = rayHit.transform.gameObject.GetComponent <NavMeshAgent>();
                if (t != null)
                {
                    Agent = t;
                }
            }
        }

        if (Input.GetMouseButtonDown(1))
        {
            Ray        ray    = Cam.ScreenPointToRay(Input.mousePosition);
            RaycastHit rayHit = new RaycastHit();

            if (Physics.Raycast(ray, out rayHit))
            {
                if (Agent != null && rayHit.transform.tag == "Terrain")
                {
                    NavMeshHit hit = new NavMeshHit();
                    NavMesh.SamplePosition(rayHit.point, out hit, 2f, 1);
                    Agent.SetDestination(hit.position);
                }
            }
        }
    }
Ejemplo n.º 30
0
    void Update()
    {
        if (agent.isOnNavMesh && agent.remainingDistance < 0.01)
        {
            if ((count == 1) && (isCollided == false))
            {
                prevTarget = target1;
                newTarget  = target2;
                count++;
                isCollided = true;
            }

            if ((count == 2) && (isCollided == false))
            {
                prevTarget = target2;
                newTarget  = target3;
                count++;
                isCollided = true;
            }

            if ((count == 3) && (isCollided == false))
            {
                prevTarget = target3;
                newTarget  = target1;
                count      = 1;
                isCollided = true;
            }

            var to  = prevTarget.transform;
            var hit = new NavMeshHit();
            var pos = to.position + new Vector3(Random.Range(-10.0f, 10.0f), 0, Random.Range(-10.0f, 10.0f));
            NavMesh.SamplePosition(pos, out hit, 100, 1);
            Debug.Log(hit.position);
            prevTarget.transform.position = hit.position + new Vector3(0, 0.45f, 0);

            agent.destination = newTarget.transform.position;

            isCollided  = false;
            stop        = true;
            agent.speed = 0;
        }

        if (stop == true)
        {
            stop = false;
            StartCoroutine(Delay());
        }
    }
Ejemplo n.º 31
0
 public bool Raycast(Vector3 targetPosition, out NavMeshHit hit)
 {
     return INTERNAL_CALL_Raycast(this, ref targetPosition, out hit);
 }
Ejemplo n.º 32
0
		private static bool INTERNAL_CALL_SamplePosition(ref Vector3 sourcePosition, out NavMeshHit hit, float maxDistance, int allowedMask){}
Ejemplo n.º 33
0
		public static bool SamplePosition(Vector3 sourcePosition, out NavMeshHit hit, float maxDistance, int allowedMask){}
Ejemplo n.º 34
0
		private static bool INTERNAL_CALL_FindClosestEdge(ref Vector3 sourcePosition, out NavMeshHit hit, int passableMask){}
Ejemplo n.º 35
0
		public static bool FindClosestEdge(Vector3 sourcePosition, out NavMeshHit hit, int passableMask){}
Ejemplo n.º 36
0
		public static bool Raycast(Vector3 sourcePosition, Vector3 targetPosition, out NavMeshHit hit, int passableMask){}
Ejemplo n.º 37
0
		public bool SamplePathPosition(int passableMask, float maxDistance, out NavMeshHit hit){}
Ejemplo n.º 38
0
		private static bool INTERNAL_CALL_Raycast(NavMeshAgent self, ref Vector3 targetPosition, out NavMeshHit hit){}
Ejemplo n.º 39
0
 public extern bool SamplePathPosition(int areaMask, float maxDistance, out NavMeshHit hit);
Ejemplo n.º 40
0
		public bool FindClosestEdge(out NavMeshHit hit){}
Ejemplo n.º 41
0
		private static bool INTERNAL_CALL_Raycast(ref Vector3 sourcePosition, ref Vector3 targetPosition, out NavMeshHit hit, int passableMask){}
Ejemplo n.º 42
0
		public bool Raycast(Vector3 targetPosition, out NavMeshHit hit){}
Ejemplo n.º 43
0
 public extern bool FindClosestEdge(out NavMeshHit hit);