DoorController.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using UnityEngine;
  2. namespace Pathfinding.Examples {
  3. /// <summary>Example script used in the example scenes</summary>
  4. [HelpURL("http://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_examples_1_1_door_controller.php")]
  5. public class DoorController : MonoBehaviour {
  6. private bool open = false;
  7. public int opentag = 1;
  8. public int closedtag = 1;
  9. public bool updateGraphsWithGUO = true;
  10. public float yOffset = 5;
  11. Bounds bounds;
  12. public void Start () {
  13. // Capture the bounds of the collider while it is closed
  14. bounds = GetComponent<Collider>().bounds;
  15. // Initially open the door
  16. SetState(open);
  17. }
  18. void OnGUI () {
  19. // Show a UI button for opening and closing the door
  20. if (GUI.Button(new Rect(5, yOffset, 100, 22), "Toggle Door")) {
  21. SetState(!open);
  22. }
  23. }
  24. public void SetState (bool open) {
  25. this.open = open;
  26. if (updateGraphsWithGUO) {
  27. // Update the graph below the door
  28. // Set the tag of the nodes below the door
  29. // To something indicating that the door is open or closed
  30. GraphUpdateObject guo = new GraphUpdateObject(bounds);
  31. int tag = open ? opentag : closedtag;
  32. // There are only 32 tags
  33. if (tag > 31) { Debug.LogError("tag > 31"); return; }
  34. guo.modifyTag = true;
  35. guo.setTag = tag;
  36. guo.updatePhysics = false;
  37. AstarPath.active.UpdateGraphs(guo);
  38. }
  39. // Play door animations
  40. if (open) {
  41. GetComponent<Animation>().Play("Open");
  42. } else {
  43. GetComponent<Animation>().Play("Close");
  44. }
  45. }
  46. }
  47. }