Example #1
0
 void OnDestroy()
 {
     if (NavMesh2D.SceneHasNavmesh())
     {
         BreakLink();
     }
 }
Example #2
0
        //切换状态后的行动
        public override void Act(GameObject player, GameObject target, GameObject npc, Animator anime)
        {
            anime.SetBool("attack", false);
            Vector2 rdDirection = new Vector2(Random.value, Random.value).normalized;

            while (_time > 2f)
            {
                if (_path != null)
                {
                    _path.RemoveAt(0);
                }
                _path = NavMesh2D.GetSmoothedPath(npc.transform.position, target.transform.position);
                _time = 0f;
            }
            if (_path != null && _path.Count != 0)
            {
                npc.transform.position = Vector2.MoveTowards(npc.transform.position, _path[0], 1.5f * Time.deltaTime);
                //npc.GetComponent<Rigidbody2D>().velocity = new Vector2(npc.transform.position.x - _path[0].x, npc.transform.position.y - _path[0].y).normalized;
                if (Vector2.Distance(npc.transform.position, _path[0]) < 0.8f)
                {
                    _path.RemoveAt(0);
                }
            }
            _time += Time.fixedDeltaTime;
        }
    private void Awake()
    {
        instance = this;
        cache    = new PathCache();

        pathProcessor = new RemoveRedundantPathPoints(new RemoveEndBacktracking(new RemoveStartBacktracking(null)));
    }
Example #4
0
        private static List <uint> Reconstruct(IDictionary <uint, uint> cameFrom, uint current,
                                               NavMesh2D <KuroiNavMesh.GeometryNode> navmesh)
        {
            if (!cameFrom.ContainsKey(current))
            {
                return(null);
            }

            var currentCameFrom = cameFrom[current];
            var totalPath       = new List <uint> {
                currentCameFrom
            };

            while (cameFrom.ContainsKey(current))
            {
                if (!cameFrom.TryGetValue(current, out var link))
                {
                    Debug.Log("Couldn't find value for " + current + "@ " + cameFrom);
                    return(totalPath);
                }

                current = link;
                totalPath.Insert(0, link);
            }

            return(totalPath);
        }
Example #5
0
        public bool SetTarget(Vector3 t)
        {
            if (enabled && NavMesh2D.GetNavMeshObject())
            {
                pathingTarget = NearestPoint(t, NavMesh2D.GetNavMeshObject().NavMesh);
                path          = NavMesh2D.GetSmoothedPath(transform.position, pathingTarget);

                if (path.Count <= 0)
                {
                    Vector3?cn = ClosestNode(t);

                    if (cn == null)
                    {
                        return(false);
                    }

                    pathingTarget = (Vector3)cn;

                    path = NavMesh2D.GetSmoothedPath(transform.position, pathingTarget);
                }

                Vector2 vk = Vector2.zero;
                for (int i = 0; i < path.Count; i++)
                {
                    vk = path[i];
                }

                if (path != null && path.Count > 0)
                {
                    velocity = lastVelBeforeZero;
                }
            }

            return(true);
        }
Example #6
0
 private static float Distance(uint @from, uint to, NavMesh2D <KuroiNavMesh.GeometryNode> navMesh)
 {
     return(Vector2.Distance(
                navMesh.grid.GetCellCenterWorld((Vector3Int)navMesh.PositionOf(from)),
                navMesh.grid.GetCellCenterWorld((Vector3Int)navMesh.PositionOf(to))
                ));
 }
Example #7
0
        void Update()
        {
            if (NavMesh2D.SceneHasNavmesh() == false)
            {
                PointA = null;
                PointB = null;
                return;
            }


            EnforceConnection();

            if (LinkEstablished && !LinkActive)
            {
                BreakLink();
            }
            else if (LinkEstablished == false && LinkActive)
            {
                EstablishLink();
            }
            else if (((Vector3)_lastPointAPos != transform.TransformPoint(PointAPos) || (Vector3)_lastPointBPos != transform.TransformPoint(PointBPos)) || (_lastBidirectional != Bidirectional))
            {
                BreakLink();
                EstablishLink();
            }
        }
Example #8
0
        public static void DoDefaultSceneGUI(NavMesh2D <G> target)
        {
            var navmesh = target;

            if (navmesh == null)
            {
                return;
            }

            var grid = navmesh.grid;

            if (grid == null)
            {
                return;
            }

            var min = navmesh.Min;
            var max = navmesh.Max;

            DoPositionHandle(grid, ref min);
            DoPositionHandle(grid, ref max);
            navmesh.Min = min;
            navmesh.Max = max;
            DrawBoundaries(navmesh);
        }
Example #9
0
        private static void DrawBoundaries(NavMesh2D <G> navmesh)
        {
            var min = navmesh.Min;
            var max = navmesh.Max;

            EditorX.DrawBoundaries(min, max, navmesh.grid);
        }
Example #10
0
 // we need a function to check if an entity can attack another.
 // => overwrite to add more cases like 'monsters can only attack players'
 //    or 'player can attack pets but not own pet' etc.
 // => raycast NavMesh to prevent attacks through walls, while allowing
 //    attacks through steep hills etc. (unlike Physics.Raycast). this is
 //    very important to prevent exploits where someone might try to attack a
 //    boss monster through a dungeon wall, etc.
 public virtual bool CanAttack(Entity entity)
 {
     return(health > 0 &&
            entity.health > 0 &&
            entity != this &&
            !inSafeZone && !entity.inSafeZone &&
            !NavMesh2D.Raycast(transform.position, entity.transform.position, out NavMeshHit2D hit, NavMesh2D.AllAreas));;
 }
Example #11
0
        public void RemoveWaypointCurrent_Leaves_current_as_null_when_removing_last_remaining_element()
        {
            var navMesh = new NavMesh2D(new List <Vector2> {
                new Vector2(0, 0)
            });

            navMesh.RemoveWaypointCurrent();
            Assert.AreEqual(null, navMesh.CurrentWaypoint);
        }
Example #12
0
        public static bool CanTransverse(NavMesh2D <KuroiNavMesh.GeometryNode> navmesh, ILink link)
        {
            if (link is DirectLink direct)
            {
                return(navmesh.Nodes[direct.Destination].IsSupported());
            }

            return(true);
        }
Example #13
0
        public void RemoveWaypointAt_Leaves_current_as_null_when_removing_last_remaining_element()
        {
            var navMesh = new NavMesh2D(new List <Vector2> {
                new Vector2(0, 0)
            });

            navMesh.RemoveWaypointAt(0);
            Assert.IsFalse(navMesh.CurrentWaypoint.HasValue);
        }
Example #14
0
    private void BreakLink()
    {
        if (LinkEstablished)
        {
            NavMesh2D.GetNavMeshObject().GetNode(PointA).DisconnectFrom(PointB);
            NavMesh2D.GetNavMeshObject().GetNode(PointB).DisconnectFrom(PointA);
        }

        PointA = -1;
        PointB = -1;
    }
Example #15
0
 private void Start()
 {
     navMesh     = NavMesh2D.instance;
     Destination = null;
     if (navMesh == null)
     {
         Debug.LogError("The scene does not contain a NavMesh2D. No navigation possible!");
         return;
     }
     navMesh.RegisterAgent(this);
 }
Example #16
0
 private void EnforceConnection()
 {
     if (LinkEstablished && LinkActive)
     {
         NavMesh2D.GetNavMeshObject().GetNode(PointA).ConnectTo(PointB, NavMesh2DConnection.ConnectionType.Standard);
         if (Bidirectional)
         {
             NavMesh2D.GetNavMeshObject().GetNode(PointB).ConnectTo(PointA, NavMesh2DConnection.ConnectionType.Standard);
         }
     }
 }
    //
    //    void OnCollisionStay2D(Collision2D coll)
    //    {
    //        if(coll.gameObject.tag == Tags.enemy )
    //        {
    //            timer = 0f;
    //            timer += Time.deltaTime;
    //            if(timer > 8f )
    //                this.targeting = true;
    //
    //            print("collision staying" + timer);
    //        }
    //    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (tankHero == null)
        {
            return;
        }
        if (Time.deltaTime == 0f)
        {
            return;
        }

        var targetPoint = this.tankHero.transform.position;

        targetPoint.z = this.transform.position.z;

        if (this.targeting)
        {
            var direction = (targetPoint - this.transform.position).normalized;
            base.movingTarget = targetPoint + direction * 0.25f;
            if (this.targetFlag != null)
            {
                if (this.lastTargetFlag != null)
                {
                    Destroy(this.lastTargetFlag.gameObject);
                }
                lastTargetFlag = Instantiate(targetFlag, base.movingTarget, this.transform.rotation) as Transform;
                path           = NavMesh2D.GetSmoothedPath(this.transform.position, lastTargetFlag.position);
            }
            this.targeting = false;
        }

        if (path != null && path.Count != 0)
        {
            this.transform.position = Vector2.MoveTowards(this.transform.position, path [0], 2f * Time.deltaTime);

            if (Vector2.Distance(transform.position, path [0]) < 0.01f)
            {
                path.RemoveAt(0);
            }
        }

        base.fireTarget    = targetPoint;
        base.baseDirection = (base.movingTarget - this.transform.position).normalized;
        //        this.transform.position += base.baseDirection * base.speed * Time.deltaTime;


        if ((this.transform.position - base.movingTarget).magnitude < 2f)
        {
            this.targeting = true;
        }

        //Debug.Log (string.Format ("{0} -> {1} | {2}", this.transform.position, this.targetPosition, this.targeting));
    }
Example #18
0
        public void Init()
        {
            navMesh = new NavMesh2D(new List <Vector2> {
                new Vector2(0, 0),
                new Vector2(1, 1),
                new Vector2(2, 2),
                new Vector2(3, 3)
            });

            emptyNavMesh = new NavMesh2D();

            waypointToAdd = new Vector2(500, 500);
        }
Example #19
0
    void FindTarget()
    {
        if (pathingTarget == null)
        {
            pathingTarget = BattleManager.Instance.GetPathingTarget(unit.m_EnemyCamp, unit.m_UnitInfo);
        }

        if (pathingTarget != null && m_bFindPath)
        {
            if (m_bFindPath)
            {
                StartCoroutine(ComponentTools.SetWaitTime(0.3f, () => { m_bFindPath = true; }));
            }
            path        = NavMesh2D.GetSmoothedPath(transform.position, pathingTarget.transform.position);
            m_bFindPath = false;

            //if (pathingTarget.transform.position != lastTargetPos)
            //{
            //    path = NavMesh2D.GetSmoothedPath(transform.position, pathingTarget.transform.position);
            //    lastTargetPos = pathingTarget.transform.position;
            //}
        }

        if (path != null && path.Count != 0)
        {
            transform.position = Vector2.MoveTowards(transform.position, path[0], unit.m_UnitInfo.speed * Time.deltaTime);

            float fDis = Vector2.Distance(transform.position, path[0]);
            if (fDis < 0.01f)
            {
                path.RemoveAt(0);
            }

            //if (path.Count == 1)
            //{
            //    if (Mathf.Abs(transform.position.x - path[0].x) < 0.1f)
            //    {
            //        path.RemoveAt(0);
            //    }
            //}
            //else
            //{
            //    float fDis = Vector2.Distance(transform.position, path[0]);
            //    if (fDis  < 0.1f)
            //    {
            //        path.RemoveAt(0);
            //    }
            //}
        }
    }
Example #20
0
    void OnDrawGizmos()
    {
        Gizmos.DrawWireSphere(transform.position, 0.05f);
        if (LinkEstablished == false || NavMesh2D.SceneHasNavmesh() == false)
        {
            return;
        }

        Gizmos.color = Color.white;
        GizmosExtra.GizmosDrawArrow(NavMesh2D.GetNavMeshObject().GetNode(PointA).position + Vector3.back * 0.1f, NavMesh2D.GetNavMeshObject().GetNode(PointB).position + Vector3.back * 0.1f, 0.2f);
        if (Bidirectional)
        {
            GizmosExtra.GizmosDrawArrow(NavMesh2D.GetNavMeshObject().GetNode(PointB).position + Vector3.back * 0.1f, NavMesh2D.GetNavMeshObject().GetNode(PointA).position + Vector3.back * 0.1f, 0.2f);
        }
    }
Example #21
0
    // LateUpdate is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.E))
        {
            path = NavMesh2D.GetSmoothedPath(transform.position, pathingTarget.position);
        }

        if (path != null && path.Count != 0)
        {
            transform.position = Vector2.MoveTowards(transform.position, path[0], 5 * Time.deltaTime);
            if (Vector2.Distance(transform.position, path[0]) < 0.01f)
            {
                path.RemoveAt(0);
            }
        }
    }
        public void GenerateNodes(NavMesh2D <G> navmesh)
        {
            var min   = navmesh.Min;
            var max   = navmesh.Max;
            var nodes = new G[navmesh.Area];

            for (var x = min.x; x <= max.x; x++)
            {
                for (var y = min.y; y <= max.y; y++)
                {
                    var index = navmesh.IndexOfUnsafe(x, y);
                    nodes[index] = navmesh.GenerateNode(x, y);
                }
            }

            navmesh.ImportNodes(nodes);
        }
Example #23
0
    public override void OnInspectorGUI()
    {
        navMesh = target as NavMesh2D;

        if (GUILayout.Button("Add obstacle"))
        {
            Undo.RecordObject(navMesh, "Add Obstacle");
            navMesh.AddObstacle();
            EditorUtility.SetDirty(navMesh);
        }

        if (GUILayout.Button("Generate"))
        {
            Undo.RecordObject(navMesh, "Generate");
            navMesh.Decompose();
            EditorUtility.SetDirty(navMesh);
        }

        DrawDefaultInspector();
    }
Example #24
0
        Vector3?ClosestNode(Vector3 point)
        {
            var navmeshObject = NavMesh2D.GetNavMeshObject();

            if (navmeshObject == null)
            {
                return(null);
            }

            var node = navmeshObject.ActualClosestNodeTo(new Vector2(point.x, point.y), false);

            if (node == null)
            {
                return(null);
            }

            Vector3 n = node.position;

            return(n);
        }
Example #25
0
    void EstablishLink()
    {
        _lastPointAPos     = transform.TransformPoint(PointAPos);
        _lastPointBPos     = transform.TransformPoint(PointBPos);
        _lastBidirectional = Bidirectional;

        PointA = NavMesh2D.GetNavMeshObject().ClosestNodeIndexTo(transform.TransformPoint(PointAPos));
        PointB = NavMesh2D.GetNavMeshObject().ClosestNodeIndexTo(transform.TransformPoint(PointBPos));

        if (PointA == -1 || PointB == -1)
        {
            PointA = NavMesh2D.GetNavMeshObject().ActualClosestNodeIndexTo(transform.TransformPoint(PointAPos));
            PointB = NavMesh2D.GetNavMeshObject().ActualClosestNodeIndexTo(transform.TransformPoint(PointBPos));
            if (PointA == -1 || PointB == -1)
            {
                return;
            }
        }

        if (Bidirectional && NavMesh2D.GetNavMeshObject().GetNode(PointA).ConnectedTo(PointB) && NavMesh2D.GetNavMeshObject().GetNode(PointB).ConnectedTo(PointA))
        {
            PointA = -1;
            PointB = -1;
            return;
        }

        if (!Bidirectional && NavMesh2D.GetNavMeshObject().GetNode(PointA).ConnectedTo(PointB))
        {
            PointA = -1;
            PointB = -1;
            return;
        }

        NavMesh2D.GetNavMeshObject().GetNode(PointA).ConnectTo(PointB, NavMesh2DConnection.ConnectionType.Standard);
        if (Bidirectional)
        {
            NavMesh2D.GetNavMeshObject().GetNode(PointB).ConnectTo(PointA, NavMesh2DConnection.ConnectionType.Standard);
        }
    }
Example #26
0
        void EstablishLink()
        {
            _lastPointAPos     = transform.TransformPoint(PointAPos);
            _lastPointBPos     = transform.TransformPoint(PointBPos);
            _lastBidirectional = Bidirectional;

            PointA = NavMesh2D.ClosestNodeTo(transform.TransformPoint(PointAPos));
            PointB = NavMesh2D.ClosestNodeTo(transform.TransformPoint(PointBPos));

            if (PointA == null || PointB == null)
            {
                PointA = NavMesh2D.ActualClosestNodeTo(transform.TransformPoint(PointAPos));
                PointB = NavMesh2D.ActualClosestNodeTo(transform.TransformPoint(PointBPos));
                if (PointA == null || PointB == null)
                {
                    return;
                }
            }

            if (Bidirectional && PointA.IsConnectedTo(PointB) && PointB.IsConnectedTo(PointA))
            {
                PointA = null;
                PointB = null;
                return;
            }

            if (!Bidirectional && PointA.IsConnectedTo(PointB))
            {
                PointA = null;
                PointB = null;
                return;
            }

            PointA.ConnectTo(PointB, NavNodeConnection.NavConnectionType.Standard);
            if (Bidirectional)
            {
                PointB.ConnectTo(PointA, NavNodeConnection.NavConnectionType.Standard);
            }
        }
Example #27
0
    IEnumerator createPath()
    {
        Vector2 target;

        while (true)
        {
            if (canSeePlayer)
            {
                target       = player.position;
                currentState = State.chase;
            }
            else
            {
                target       = waypoints[nextWaypoint].position;
                currentState = State.goBack;
            }

            path = NavMesh2D.GetSmoothedPath(transform.position, target);

            yield return(new WaitForSeconds(0.1f));
        }
    }
Example #28
0
 void OnEnable()
 {
     navMesh = target as NavMesh2D;
 }
Example #29
0
 public void goTo(Transform whereToGo)
 {
     path = NavMesh2D.GetPath(transform.position, whereToGo.position);
 }
Example #30
0
    void FixedUpdate()
    {
        if (hacked)
        {
            Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, 10);
            foreach (Collider2D hit in colliders)
            {
                //Debug.Log("hits feitos");
                if (hit.gameObject.tag == "enemy")
                {
                    timer += Time.deltaTime;
                    if (timer > waitingTime)
                    {
                        GameObject dynamicParent = GameObject.Find("DynamicObjects");
                        Transform  newObject     = Instantiate(bulletHacked, new Vector3(rb.position.x, rb.position.y, 0), Quaternion.identity) as Transform;
                        newObject.parent = dynamicParent.transform;
                        Debug.Log("success em principio");
                        timer = 0;
                    }
                }
            }
        }
        else
        {
            if (health <= 0)
            {
                Instantiate(ammo, new Vector3(rb.position.x, rb.position.y, 0), Quaternion.identity);
                Destroy(gameObject);
            }

            if (calm)
            {
                if (patrolDest.Equals(Vector3.zero))
                {
                    GameObject patrolPoint = patrolPoints[Random.Range(0, patrolPoints.Length)];
                    patrolDest = patrolPoint.transform.position;
                    path       = NavMesh2D.GetSmoothedPath(transform.position, patrolDest);
                    Debug.Log("RESET");
                }
                else
                {
                    float playerDist = Vector2.Distance(transform.position, patrolDest);
                    if (playerDist < 0.5f)
                    {
                        patrolDest = Vector3.zero;
                    }
                    if (path != null && path.Count != 0)
                    {
                        moveToPosition(new Vector3(path[0].x, path[0].y, 0));
                        //Debug.Log(path[0].x);
                        //Debug.Log(path[0].y);
                        //Debug.Log(Vector2.Distance(transform.position, path[0]));
                        if (Vector2.Distance(transform.position, path[0]) < 0.1f)
                        {
                            path.RemoveAt(0);
                            Debug.Log("REMOVED");
                        }
                    }
                }
            }
            else
            {
                float playerDist = Vector2.Distance(transform.position, player.transform.position);
                if (playerDist > 5)
                {
                    path = NavMesh2D.GetSmoothedPath(transform.position, player.transform.position);

                    if (path != null && path.Count != 0)
                    {
                        if (currentPoint == 0)
                        {
                            moveToPosition(new Vector3(path[1].x, path[1].y, 0));
                        }
                        if (Vector2.Distance(transform.position, path[currentPoint + 1]) < 0.01f)
                        {
                            currentPoint++;
                            moveToPosition(new Vector3(path[currentPoint].x, path[currentPoint].y, 0));
                        }
                    }
                }
                else
                {
                    rb.velocity        = Vector2.zero;
                    rb.angularVelocity = 0;
                    Vector3 playerDir = player.transform.position - transform.position;
                    transform.rotation = Quaternion.LookRotation(Vector3.forward, playerDir);
                }
            }

            bool tr = canSeePlayer();
            if (tr)
            {
                calm = false;

                timer += Time.deltaTime;
                Debug.Log(timer);
                if (timer > waitingTime)
                {
                    GameObject dynamicParent = GameObject.Find("DynamicObjects");
                    Transform  newObject     = Instantiate(bullet, new Vector3(rb.position.x, rb.position.y, 0), Quaternion.identity) as Transform;
                    newObject.parent = dynamicParent.transform;
                    Debug.Log("SHOOT");
                    timer = 0;
                }
            }
        }
    }