Advertisement
DugganSC

Untitled

Feb 27th, 2025
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class MazeRightHandRule : MonoBehaviour
  4. {
  5. public CharacterController controller;
  6. public float moveSpeed = 5f;
  7. public float rotationSpeed = 90f; // Degrees per second
  8. public float cellWidth = 5f; // Width of each maze cell
  9. public LayerMask wallLayer; // Layer containing maze walls
  10.  
  11. private Vector3 currentDirection = Vector3.forward;
  12. private float targetRotation = 0f;
  13. private bool isRotating = false;
  14.  
  15. void Update()
  16. {
  17. if (isRotating)
  18. {
  19. RotateCharacter();
  20. }
  21. else
  22. {
  23. MoveCharacter();
  24. }
  25. }
  26.  
  27. void MoveCharacter()
  28. {
  29. if (CanMoveForward())
  30. {
  31. Vector3 moveDirection = currentDirection * moveSpeed * Time.deltaTime;
  32. controller.Move(moveDirection);
  33. }
  34. else
  35. {
  36. Debug.Log("Bonk");
  37. TurnLeft();
  38. }
  39. }
  40.  
  41. bool CanMoveForward()
  42. {
  43. Vector3 checkPosition = transform.position + currentDirection * (cellWidth / 2f + 0.1f); // Add a small offset
  44. return !Physics.CheckBox(checkPosition, new Vector3(cellWidth / 2f, 1f, cellWidth / 2f), transform.rotation, wallLayer);
  45. }
  46.  
  47. void TurnRight()
  48. {
  49. targetRotation = transform.eulerAngles.y + 90f;
  50. isRotating = true;
  51. }
  52.  
  53. void TurnLeft()
  54. {
  55. targetRotation = transform.eulerAngles.y - 90f;
  56. isRotating = true;
  57. }
  58.  
  59. void RotateCharacter()
  60. {
  61. float angle = Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetRotation, rotationSpeed * Time.deltaTime);
  62. transform.eulerAngles = new Vector3(0f, angle, 0f);
  63.  
  64. if (Mathf.Approximately(angle, targetRotation))
  65. {
  66. isRotating = false;
  67. currentDirection = Quaternion.Euler(0f, targetRotation - transform.eulerAngles.y, 0f) * currentDirection;
  68. currentDirection.Normalize();
  69. }
  70. }
  71.  
  72. void FixedUpdate()
  73. {
  74. if (!isRotating)
  75. {
  76. CheckRightWall();
  77. }
  78. }
  79.  
  80. void CheckRightWall()
  81. {
  82. Vector3 rightDirection = Quaternion.Euler(0f, 90f, 0f) * currentDirection;
  83. Vector3 checkPosition = transform.position + rightDirection * (cellWidth / 2f + 0.1f);
  84.  
  85. Debug.Log(checkPosition);
  86. Debug.DrawRay(checkPosition, rightDirection * cellWidth, Color.yellow);
  87.  
  88. if (!Physics.CheckBox(checkPosition, new Vector3(cellWidth / 2f, 1f, cellWidth / 2f), transform.rotation, wallLayer))
  89. {
  90. TurnRight();
  91. }
  92. }
  93.  
  94. void OnDrawGizmos() // Use OnDrawGizmos for drawing in the editor
  95. {
  96. if (Application.isPlaying) // Only draw gizmos while playing
  97. {
  98. // Debug visualization (draw the forward check box)
  99. Vector3 forwardCheckPosition = transform.position + currentDirection * (cellWidth / 2f + 0.1f);
  100. Gizmos.color = Color.red;
  101. Gizmos.DrawWireCube(forwardCheckPosition, new Vector3(cellWidth, 2f, cellWidth));
  102.  
  103. // Debug visualization (draw the right check box)
  104. Vector3 rightDirection = Quaternion.Euler(0f, 90f, 0f) * currentDirection;
  105. Vector3 rightCheckPosition = transform.position + rightDirection * (cellWidth / 2f + 0.1f);
  106. Gizmos.color = Color.blue;
  107. Gizmos.DrawWireCube(rightCheckPosition, new Vector3(cellWidth, 2f, cellWidth));
  108. }
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement