Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- public class MotorcycleController : MonoBehaviour
- {
- public float speed = 30f;
- public float turnSpeed = 80f;
- public static bool isRiding;
- private Rigidbody rb;
- public GameObject player;
- public Transform seatPosition;
- public SpriteRenderer sprite;
- public RectTransform handleUI; // UI element for the handle
- private float handleRotation = 0f;
- public bool rideCheck;
- public float mountRadius = 2f; // Radius for player to mount
- void Awake()
- {
- rb = GetComponent<Rigidbody>();
- if (!rideCheck)
- rb.isKinematic = true; // Make motorcycle kinematic initially
- }
- void Update()
- {
- if (isRiding)
- {
- player.transform.position = seatPosition.position;
- HandleMovement();
- if (Input.GetKey(KeyCode.B))
- {
- print("dismount");
- }
- // Dismount with B
- // Sync motorcycle's rotation with player rotation on Y-axis
- SyncMotorcycleRotation();
- }
- else
- {
- // Check if player is nearby using a sphere radius
- Collider[] hitColliders = Physics.OverlapSphere(transform.position, mountRadius);
- foreach (Collider hit in hitColliders)
- {
- if (hit.CompareTag("Player") && Input.GetKeyDown(KeyCode.E))
- {
- Mount();
- break;
- }
- }
- }
- }
- void SyncMotorcycleRotation()
- {
- // Match the motorcycle's rotation to the player's rotation on the Y-axis
- float playerRotationY = player.transform.eulerAngles.y; // Get player's rotation around Y-axis
- transform.rotation = Quaternion.Euler(0, playerRotationY, 0); // Set motorcycle's Y rotation to match player
- }
- void HandleMovement()
- {
- float moveInput = Input.GetAxis("Vertical"); // W/S or Up/Down
- float turnInput = Input.GetAxis("Horizontal"); // A/D or Left/Right
- // Move forward/backward along the direction the player is facing
- Vector3 moveDirection = player.transform.forward * moveInput; // Use the player's forward direction
- rb.AddForce(moveDirection * speed, ForceMode.Acceleration);
- // Prevent the motorcycle from rotating due to physics forces
- rb.angularVelocity = Vector3.zero; // Reset any angular velocity that might be applied by physics
- // Handle the UI of the handle based on the player's input
- handleRotation = Mathf.Clamp(turnInput * 30f, -30f, 30f);
- handleUI.rotation = Quaternion.Euler(0, 0, handleRotation);
- }
- public void Mount()
- {
- isRiding = true;
- rideCheck = true;
- rb.isKinematic = false; // Enable physics when riding
- sprite.enabled = false;
- // Move player to seat and make the player a child of the seat
- player.transform.SetParent(seatPosition); // Set player as a child of the seat
- }
- public void Dismount()
- {
- isRiding = false;
- rb.isKinematic = true; // Disable physics when dismounted
- sprite.enabled = true;
- // Unset the parent to stop being a child of the seat
- player.transform.SetParent(null); // Remove the player from the seat hierarchy
- // Position player near the motorcycle
- player.transform.position = transform.position + transform.right * 1.5f;
- }
- void OnDrawGizmosSelected()
- {
- // Draw the mount radius in the scene view
- Gizmos.color = Color.yellow;
- Gizmos.DrawWireSphere(transform.position, mountRadius);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement