Esempio n. 1
0
	// Use this for initialization
	void Start () {
        GameObject playa = GameObject.FindGameObjectWithTag("Player");
        pl = playa.GetComponent<Player>();
        pl2 = playa.GetComponent<NavMeshAgent>();
        GameObject waypt = GameObject.FindGameObjectWithTag("path_02");
        wp = waypt.GetComponent<WayPointScript>();
    }
    void Update()
    {
        float step = speed * Time.deltaTime;

        transform.position = Vector3.MoveTowards(transform.position, to.transform.position, step);


        Vector3 diff = (to.transform.position - from.transform.position).normalized;
        //diff.x = 0;
        Quaternion target = Quaternion.LookRotation(diff);

        target            *= Quaternion.Euler(90, 0, 0);
        transform.rotation = Quaternion.Lerp(transform.rotation, target, 10 * Time.deltaTime);


        // If it has arrived, find new transform
        if (to.transform.position == transform.position)
        {
            WayPointScript tmp;
            do
            {
                tmp = to.getRandomNeighbor();
            } while (tmp.transform.position == from.transform.position);
            from = to;
            to   = tmp;
        }
    }
Esempio n. 3
0
    // Use this for initialization
    void Start()
    {
        GameObject playa = GameObject.FindGameObjectWithTag("Player");

        pl  = playa.GetComponent <Player>();
        pl2 = playa.GetComponent <NavMeshAgent>();
        GameObject waypt = GameObject.FindGameObjectWithTag("path_02");

        wp = waypt.GetComponent <WayPointScript>();
    }
Esempio n. 4
0
    void OnSceneGUI()
    {
        Debug.Log("Running");
        WayPointScript t = target as WayPointScript;

        //Create Text on each checkpoint //
        for (int i = 0; i < t.waypoints.Length; i++)
        {
            UnityEditor.Handles.Label(t.waypoints[i].transform.position, (i + 1).ToString());
        }
    }
Esempio n. 5
0
        public Stack <Transform> FindPath(Vector3 enemyPos, Vector3 playerPos)
        {
            GameObject[]   points          = GameObject.FindGameObjectsWithTag(TagName.WAYPOINT);
            float          minDisSqrEnemy  = 99999.0f;
            float          minDisSqrPlayer = 99999.0f;
            WayPointScript from            = null;
            WayPointScript to = null;

            foreach (GameObject wObj in points)
            {
                WayPointScript w = wObj.GetComponent <WayPointScript>();
                w.parent = null;
                float disSqrEnemy = (w.transform.position - enemyPos).magnitude;
                if (disSqrEnemy < minDisSqrEnemy)
                {
                    Ray ray = new Ray(enemyPos + new Vector3(0, 0.5f, 0), w.transform.position - enemyPos);

                    RaycastHit hit;
                    //if (!Physics.CapsuleCast(enemyPos, enemyPos + Vector3.up, 1, w.transform.position - enemyPos, disSqrEnemy, 1 << PhysicsLayer.WALL | 1 << PhysicsLayer.FLOOR))
                    if (!Physics.Raycast(ray, out hit, disSqrEnemy, 1 << PhysicsLayer.WALL | 1 << PhysicsLayer.TRANSPARENT_WALL | 1 << PhysicsLayer.FLOOR))
                    {
                        from           = w;
                        minDisSqrEnemy = disSqrEnemy;
                    }
                }

                to = GameApp.GetInstance().GetGameScene().GetPlayer().NearestWayPoint;
            }
            if (from != null && to != null)
            {
                path = SearchPath(from, to);
            }
            if (to == null)
            {
                Debug.Log("to null");
            }
            return(path);
        }
Esempio n. 6
0
        public void UpdateNearestWayPoint()
        {
            GameObject[] points          = GameObject.FindGameObjectsWithTag(TagName.WAYPOINT);
            float        minDisSqrPlayer = 99999.0f;

            foreach (GameObject wObj in points)
            {
                WayPointScript w         = wObj.GetComponent <WayPointScript>();
                float          disPlayer = (w.transform.position - playerTransform.position).magnitude;

                if (disPlayer < minDisSqrPlayer)
                {
                    Ray ray = new Ray(playerTransform.position + new Vector3(0, 0.5f, 0), w.transform.position - playerTransform.position);

                    RaycastHit hit;
                    if (!Physics.Raycast(ray, out hit, disPlayer, 1 << PhysicsLayer.WALL | 1 << PhysicsLayer.FLOOR))
                    {
                        NearestWayPoint = w;
                        minDisSqrPlayer = disPlayer;
                    }
                }
            }
        }
Esempio n. 7
0
        public Stack <Transform> SearchPath(WayPointScript from, WayPointScript to)
        {
            Stack <Transform> path = new Stack <Transform>();

            if (from == to)
            {
                path.Push(to.transform);
                return(path);
            }


            //Debug.Log("search Path: from " + from.transform.name + " to " + to.transform.name);
            openStack.Push(from);

            while (openStack.Count > 0)
            {
                if (openStack.Count > 100)
                {
                    Debug.Log("Memeroy Explode! To many nodes in open stack..");
                    Debug.Break();
                    break;
                }
                WayPointScript currentWayPoint = openStack.Pop();
                closeStack.Push(currentWayPoint);
                WayPointScript[] nodes = currentWayPoint.nodes;
                foreach (WayPointScript w in nodes)
                {
                    if (w == to)
                    {
                        w.parent = currentWayPoint;
                        break;
                    }
                    if (!openStack.Contains(w) && !closeStack.Contains(w))
                    {
                        w.parent = currentWayPoint;
                        openStack.Push(w);
                    }
                }
            }
            openStack.Clear();
            closeStack.Clear();


            WayPointScript wayPoint = to;

            path.Push(to.transform);
            //Debug.Log("Find Path " + to.transform.name);
            while (wayPoint.parent != null)
            {
                wayPoint = wayPoint.parent;
                //Debug.Log("-> " + wayPoint.transform.name);
                if (path.Count > 30)
                {
                    Debug.Log("Memeroy Explode! Parent Forever..");
                    Debug.Break();
                    break;
                }
                path.Push(wayPoint.transform);
            }


            return(path);
        }
 void Start()
 {
     to   = initial;
     from = initial;
 }