Advertisement
har5452

Untitled

Feb 13th, 2025
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3.  
  4. public class MotorcycleController : MonoBehaviour
  5. {
  6. public float speed = 30f;
  7. public float turnSpeed = 80f;
  8. public static bool isRiding;
  9. private Rigidbody rb;
  10. public GameObject player;
  11. public Transform seatPosition;
  12. public SpriteRenderer sprite;
  13. public RectTransform handleUI; // UI element for the handle
  14. private float handleRotation = 0f;
  15. public bool rideCheck;
  16. public float mountRadius = 2f; // Radius for player to mount
  17.  
  18. void Awake()
  19. {
  20. rb = GetComponent<Rigidbody>();
  21.  
  22. if (!rideCheck)
  23. rb.isKinematic = true; // Make motorcycle kinematic initially
  24. }
  25.  
  26. void Update()
  27. {
  28. if (isRiding)
  29. {
  30. player.transform.position = seatPosition.position;
  31. HandleMovement();
  32. if (Input.GetKey(KeyCode.B))
  33. {
  34. print("dismount");
  35. }
  36. // Dismount with B
  37.  
  38. // Sync motorcycle's rotation with player rotation on Y-axis
  39. SyncMotorcycleRotation();
  40. }
  41.  
  42.  
  43. else
  44. {
  45. // Check if player is nearby using a sphere radius
  46. Collider[] hitColliders = Physics.OverlapSphere(transform.position, mountRadius);
  47. foreach (Collider hit in hitColliders)
  48. {
  49. if (hit.CompareTag("Player") && Input.GetKeyDown(KeyCode.E))
  50. {
  51. Mount();
  52. break;
  53. }
  54. }
  55. }
  56. }
  57.  
  58. void SyncMotorcycleRotation()
  59. {
  60. // Match the motorcycle's rotation to the player's rotation on the Y-axis
  61. float playerRotationY = player.transform.eulerAngles.y; // Get player's rotation around Y-axis
  62. transform.rotation = Quaternion.Euler(0, playerRotationY, 0); // Set motorcycle's Y rotation to match player
  63. }
  64.  
  65. void HandleMovement()
  66. {
  67. float moveInput = Input.GetAxis("Vertical"); // W/S or Up/Down
  68. float turnInput = Input.GetAxis("Horizontal"); // A/D or Left/Right
  69.  
  70. // Move forward/backward along the direction the player is facing
  71. Vector3 moveDirection = player.transform.forward * moveInput; // Use the player's forward direction
  72. rb.AddForce(moveDirection * speed, ForceMode.Acceleration);
  73.  
  74. // Prevent the motorcycle from rotating due to physics forces
  75. rb.angularVelocity = Vector3.zero; // Reset any angular velocity that might be applied by physics
  76.  
  77. // Handle the UI of the handle based on the player's input
  78. handleRotation = Mathf.Clamp(turnInput * 30f, -30f, 30f);
  79. handleUI.rotation = Quaternion.Euler(0, 0, handleRotation);
  80. }
  81.  
  82.  
  83.  
  84. public void Mount()
  85. {
  86. isRiding = true;
  87. rideCheck = true;
  88. rb.isKinematic = false; // Enable physics when riding
  89. sprite.enabled = false;
  90. // Move player to seat and make the player a child of the seat
  91. player.transform.SetParent(seatPosition); // Set player as a child of the seat
  92.  
  93. }
  94.  
  95. public void Dismount()
  96. {
  97. isRiding = false;
  98. rb.isKinematic = true; // Disable physics when dismounted
  99. sprite.enabled = true;
  100. // Unset the parent to stop being a child of the seat
  101. player.transform.SetParent(null); // Remove the player from the seat hierarchy
  102.  
  103. // Position player near the motorcycle
  104. player.transform.position = transform.position + transform.right * 1.5f;
  105. }
  106.  
  107.  
  108. void OnDrawGizmosSelected()
  109. {
  110. // Draw the mount radius in the scene view
  111. Gizmos.color = Color.yellow;
  112. Gizmos.DrawWireSphere(transform.position, mountRadius);
  113. }
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement