Example #1
0
 public void Update()
 {
     if (currentState != null)
     {
         SteeringManager.PrintMessage("Current state: " + currentState.Description());
         currentState.Update();
     }
 }
        public override void Update()
        {
            bool recalculate = false;

            SteeringManager.PrintMessage("Press P to toggle smooth paths");
            SteeringManager.PrintMessage("Press O to toggle 3D paths");

            if (Input.GetKeyDown(KeyCode.P) && !lastPressed)
            {
                pathFinder.Smooth = !pathFinder.Smooth;
                recalculate       = true;
            }

            if (Input.GetKeyDown(KeyCode.O) && !lastPressed)
            {
                pathFinder.isThreeD = !pathFinder.isThreeD;
                recalculate         = true;
            }

            GameObject camera = (GameObject)GameObject.FindGameObjectWithTag("MainCamera");

            if (Input.GetMouseButton(0))
            {
                Plane           worldPlane = new Plane(new Vector3(0, 1, 0), 0);
                UnityEngine.Ray ray        = new UnityEngine.Ray(camera.transform.position, camera.transform.forward);
                float           distance   = 0;
                if (worldPlane.Raycast(ray, out distance))
                {
                    targetPos = camera.transform.position + (camera.transform.forward * distance);
                }
                recalculate = true;
            }

            if (recalculate)
            {
                Path path = pathFinder.FindPath(startPos, targetPos);
                if (path.Waypoints.Count == 0)
                {
                    leader.GetComponent <SteeringBehaviours>().TurnOffAll();
                }
                else
                {
                    leader.GetComponent <SteeringBehaviours>().FollowPathEnabled = true;
                }
                leader.GetComponent <SteeringBehaviours>().path      = path;
                leader.GetComponent <SteeringBehaviours>().path.draw = true;
                leader.GetComponent <SteeringBehaviours>().path.Next = 0;
            }

            if (Input.anyKeyDown)
            {
                lastPressed = true;
            }
            else
            {
                lastPressed = false;
            }

            if (pathFinder.message != "")
            {
                SteeringManager.PrintMessage(pathFinder.message);
            }

            base.Update();
        }