Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- using System.Collections.Generic;
- public class HorsebackRidingSystem : MonoBehaviour
- {
- public float mountRange = 3f;
- public GameObject player;
- public GameObject[] horses;
- public Transform ridingTextUI;
- public Animator horseAnimator;
- public GameObject playerUnmountPosition;
- public LayerMask terrainLayer;
- private List<Transform> horsesInRange = new List<Transform>();
- private Transform mountedHorse;
- private Vector3 playerOffsetFromHorse;
- private bool isMounted = false;
- private bool isRiding = false;
- private bool isGalloping = false;
- private bool isJumping = false;
- private void Update()
- {
- // Check if the player reference is null
- if (player == null || ridingTextUI == null || horseAnimator == null)
- {
- Debug.LogError("Player, Riding Text UI, or Horse Animator references are not assigned in the inspector!");
- return;
- }
- UpdateHorsesInRange();
- if (!isMounted)
- {
- if (horsesInRange.Count > 0 && Input.GetKeyDown(KeyCode.E))
- {
- Mount(GetClosestHorse());
- }
- else if (horsesInRange.Count > 0)
- {
- ridingTextUI.GetComponent<Text>().text = "Hit E to Mount";
- }
- else
- {
- ridingTextUI.GetComponent<Text>().text = " ";
- }
- }
- else if (!isRiding)
- {
- if (Input.GetKeyDown(KeyCode.E))
- {
- Dismount();
- }
- else if (Input.GetKeyDown(KeyCode.R))
- {
- StartRiding();
- }
- else
- {
- ridingTextUI.GetComponent<Text>().text = "Hit R to Ride or E to unmount";
- }
- }
- else
- {
- if (Input.GetKeyDown(KeyCode.Space) && !isJumping)
- {
- Jump();
- }
- else if (Input.GetKeyDown(KeyCode.R))
- {
- StopRiding();
- }
- else if (Input.GetKeyDown(KeyCode.LeftShift))
- {
- StartGallop();
- }
- else if (Input.GetKeyUp(KeyCode.LeftShift))
- {
- StopGallop();
- }
- else
- {
- ridingTextUI.GetComponent<Text>().text = "Hold Left Shift to gallop\nSpace to jump\nR to stop riding";
- }
- }
- }
- private void UpdateHorsesInRange()
- {
- horsesInRange.Clear();
- foreach (GameObject horse in horses)
- {
- if (horse == null)
- {
- Debug.LogWarning("One of the horses is null!");
- continue;
- }
- float distance = Vector3.Distance(player.transform.position, horse.transform.position);
- if (distance <= mountRange)
- {
- horsesInRange.Add(horse.transform);
- }
- }
- }
- private Transform GetClosestHorse()
- {
- float minDistance = Mathf.Infinity;
- Transform closestHorse = null;
- foreach (Transform horse in horsesInRange)
- {
- if (horse == null)
- {
- Debug.LogWarning("One of the horses is null!");
- continue;
- }
- float distance = Vector3.Distance(player.transform.position, horse.position);
- if (distance < minDistance)
- {
- minDistance = distance;
- closestHorse = horse;
- }
- }
- return closestHorse;
- }
- private void Mount(Transform horse)
- {
- if (horse == null)
- {
- Debug.LogWarning("No horse found to mount!");
- return;
- }
- if (player == null || ridingTextUI == null)
- {
- Debug.LogError("Player or Riding Text UI references are not assigned in the inspector!");
- return;
- }
- ridingTextUI.GetComponent<Text>().text = "Hit R to Ride or E to unmount";
- mountedHorse = horse;
- mountedHorse.Find("Rider").gameObject.SetActive(true);
- player.SetActive(false);
- playerOffsetFromHorse = player.transform.position - mountedHorse.position;
- // Activate HorseMouseMovement script
- HorseMouseMovement horseMouseMovement = mountedHorse.GetComponent<HorseMouseMovement>();
- if (horseMouseMovement != null)
- {
- horseMouseMovement.Activate();
- }
- player.transform.position = mountedHorse.position + playerOffsetFromHorse;
- // Update the unmounted player position to the current player position
- playerUnmountPosition.transform.position = player.transform.position;
- isMounted = true;
- }
- private void Dismount()
- {
- if (player == null || ridingTextUI == null)
- {
- Debug.LogError("Player or Riding Text UI references are not assigned in the inspector!");
- return;
- }
- player.SetActive(true);
- // Deactivate HorseMouseMovement script
- HorseMouseMovement horseMouseMovement = mountedHorse.GetComponent<HorseMouseMovement>();
- if (horseMouseMovement != null)
- {
- horseMouseMovement.Deactivate();
- }
- // Adjust player position to match playerUnmountPosition
- player.transform.position = playerUnmountPosition.transform.position;
- mountedHorse.Find("Rider").gameObject.SetActive(false);
- isMounted = false;
- isRiding = false;
- isGalloping = false;
- ridingTextUI.GetComponent<Text>().text = "Hit E to Mount";
- }
- private void StartRiding()
- {
- if (horseAnimator == null)
- {
- Debug.LogError("Horse Animator reference is not assigned in the inspector!");
- return;
- }
- isRiding = true;
- horseAnimator.SetTrigger("Move");
- }
- private void StopRiding()
- {
- if (horseAnimator == null)
- {
- Debug.LogError("Horse Animator reference is not assigned in the inspector!");
- return;
- }
- isRiding = false;
- isGalloping = false;
- horseAnimator.SetTrigger("Idle");
- }
- private void StartGallop()
- {
- if (horseAnimator == null)
- {
- Debug.LogError("Horse Animator reference is not assigned in the inspector!");
- return;
- }
- isGalloping = true;
- horseAnimator.SetTrigger("Run");
- }
- private void StopGallop()
- {
- if (horseAnimator == null)
- {
- Debug.LogError("Horse Animator reference is not assigned in the inspector!");
- return;
- }
- isGalloping = false;
- horseAnimator.SetTrigger("Move");
- }
- private void Jump()
- {
- if (horseAnimator == null)
- {
- Debug.LogError("Horse Animator reference is not assigned in the inspector!");
- return;
- }
- isJumping = true;
- horseAnimator.SetTrigger("Jump");
- StartCoroutine(ResetJumpFlag(1.333f)); // Adjust duration as needed
- }
- private IEnumerator ResetJumpFlag(float animationDuration)
- {
- yield return new WaitForSeconds(animationDuration);
- isJumping = false;
- if (Input.GetKey(KeyCode.LeftShift))
- {
- StartGallop();
- }
- else
- {
- horseAnimator.SetTrigger("Move");
- }
- }
- private void FixedUpdate()
- {
- if (isMounted)
- {
- RaycastHit hit;
- // Cast a ray downwards to detect the terrain beneath the horse
- if (Physics.Raycast(mountedHorse.position, Vector3.down, out hit, Mathf.Infinity, terrainLayer))
- {
- // Adjust the horse's position to match the terrain height
- Vector3 targetPosition = hit.point + Vector3.up * 0.5f; // Offset to avoid clipping into terrain
- mountedHorse.position = Vector3.Lerp(mountedHorse.position, targetPosition, Time.deltaTime * 5f);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement