Advertisement
evelynshilosky

HorsebackRidingSystem

Jan 7th, 2025
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.95 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. public class HorsebackRidingSystem : MonoBehaviour
  7. {
  8.     public float mountRange = 3f;
  9.     public GameObject player;
  10.     public GameObject[] horses;
  11.     public Transform ridingTextUI;
  12.     public Animator horseAnimator;
  13.     public GameObject playerUnmountPosition;
  14.     public LayerMask terrainLayer;
  15.     public MonoBehaviour scriptToToggle; // variable for the AI Movement script to turn on and off
  16.  
  17.     private List<Transform> horsesInRange = new List<Transform>();
  18.     private Transform mountedHorse;
  19.     private Vector3 playerOffsetFromHorse;
  20.  
  21.     private bool isMounted = false;
  22.     private bool isRiding = false;
  23.     private bool isGalloping = false;
  24.     private bool isJumping = false;
  25.  
  26.     private bool isWaitingToReenableScript = false;
  27.     private bool isHorseLockedInPlace = false;
  28.  
  29.     private void Update()
  30.     {
  31.         // Check if the player reference is null
  32.         if (player == null || ridingTextUI == null || horseAnimator == null)
  33.         {
  34.             Debug.LogError("Player, Riding Text UI, or Horse Animator references are not assigned in the inspector!");
  35.             return;
  36.         }
  37.  
  38.         UpdateHorsesInRange();
  39.  
  40.         if (!isMounted)
  41.         {
  42.             if (horsesInRange.Count > 0 && Input.GetKeyDown(KeyCode.E))
  43.             {
  44.                 Mount(GetClosestHorse());
  45.             }
  46.             else if (horsesInRange.Count > 0)
  47.             {
  48.                 ridingTextUI.GetComponent<Text>().text = "Hit E to Mount";
  49.             }
  50.             else
  51.             {
  52.                 ridingTextUI.GetComponent<Text>().text = " ";
  53.             }
  54.         }
  55.         else if (!isRiding)
  56.         {
  57.             if (Input.GetKeyDown(KeyCode.E))
  58.             {
  59.                 //ridingTextUI.GetComponent<Text>().text = "Hit L to keep the horse here";
  60.                 Dismount();
  61.             }
  62.             else if (Input.GetKeyDown(KeyCode.R))
  63.             {
  64.                 StartRiding();
  65.             }
  66.             else
  67.             {
  68.                 ridingTextUI.GetComponent<Text>().text = "Hit R to Ride or E to unmount";
  69.             }
  70.         }
  71.         else
  72.         {
  73.             if (Input.GetKeyDown(KeyCode.Space) && !isJumping)
  74.             {
  75.                 Jump();
  76.             }
  77.             else if (Input.GetKeyDown(KeyCode.R))
  78.             {
  79.                 StopRiding();
  80.             }
  81.             else if (Input.GetKeyDown(KeyCode.LeftShift))
  82.             {
  83.                 StartGallop();
  84.             }
  85.             else if (Input.GetKeyUp(KeyCode.LeftShift))
  86.             {
  87.                 StopGallop();
  88.             }
  89.             else
  90.             {
  91.                 ridingTextUI.GetComponent<Text>().text = "Hold Left Shift to gallop\nSpace to jump\nR to stop riding";
  92.             }
  93.         }
  94.     }
  95.  
  96.     private void UpdateHorsesInRange()
  97.     {
  98.         horsesInRange.Clear();
  99.  
  100.         foreach (GameObject horse in horses)
  101.         {
  102.             if (horse == null)
  103.             {
  104.                 Debug.LogWarning("One of the horses is null!");
  105.                 continue;
  106.             }
  107.  
  108.             float distance = Vector3.Distance(player.transform.position, horse.transform.position);
  109.             if (distance <= mountRange)
  110.             {
  111.                 horsesInRange.Add(horse.transform);
  112.             }
  113.         }
  114.     }
  115.  
  116.     private Transform GetClosestHorse()
  117.     {
  118.         float minDistance = Mathf.Infinity;
  119.         Transform closestHorse = null;
  120.  
  121.         foreach (Transform horse in horsesInRange)
  122.         {
  123.             if (horse == null)
  124.             {
  125.                 Debug.LogWarning("One of the horses is null!");
  126.                 continue;
  127.             }
  128.  
  129.             float distance = Vector3.Distance(player.transform.position, horse.position);
  130.             if (distance < minDistance)
  131.             {
  132.                 minDistance = distance;
  133.                 closestHorse = horse;
  134.             }
  135.         }
  136.  
  137.         return closestHorse;
  138.     }
  139.  
  140.     private void Mount(Transform horse)
  141.     {
  142.         if (horse == null)
  143.         {
  144.             Debug.LogWarning("No horse found to mount!");
  145.             return;
  146.         }
  147.  
  148.         if (player == null || ridingTextUI == null)
  149.         {
  150.             Debug.LogError("Player or Riding Text UI references are not assigned in the inspector!");
  151.             return;
  152.         }
  153.  
  154.         ridingTextUI.GetComponent<Text>().text = "Hit R to Ride or E to unmount";
  155.         mountedHorse = horse;
  156.         mountedHorse.Find("Rider").gameObject.SetActive(true);
  157.         player.SetActive(false);
  158.         playerOffsetFromHorse = player.transform.position - mountedHorse.position;
  159.  
  160.         // Activate HorseMouseMovement script
  161.         HorseMouseMovement horseMouseMovement = mountedHorse.GetComponent<HorseMouseMovement>();
  162.         if (horseMouseMovement != null)
  163.         {
  164.             horseMouseMovement.Activate();
  165.         }
  166.  
  167.         player.transform.position = mountedHorse.position + playerOffsetFromHorse;
  168.  
  169.         // Update the unmounted player position to the current player position
  170.         playerUnmountPosition.transform.position = player.transform.position;
  171.  
  172.         isMounted = true;
  173.         isHorseLockedInPlace = false;
  174.  
  175.         // Disable the script when mounted
  176.         if (scriptToToggle != null)
  177.         {
  178.             scriptToToggle.enabled = false;
  179.         }
  180.  
  181.         horseAnimator.SetBool("isRunning", true);
  182.     }
  183.  
  184.     private void Dismount()
  185.     {
  186.         if (player == null || ridingTextUI == null)
  187.         {
  188.             Debug.LogError("Player or Riding Text UI references are not assigned in the inspector!");
  189.             return;
  190.         }
  191.  
  192.         player.SetActive(true);
  193.  
  194.         // Deactivate HorseMouseMovement script
  195.         HorseMouseMovement horseMouseMovement = mountedHorse.GetComponent<HorseMouseMovement>();
  196.         if (horseMouseMovement != null)
  197.         {
  198.             horseMouseMovement.Deactivate();
  199.         }
  200.  
  201.         // Adjust player position to match playerUnmountPosition
  202.         player.transform.position = playerUnmountPosition.transform.position;
  203.  
  204.         mountedHorse.Find("Rider").gameObject.SetActive(false);
  205.         isMounted = false;
  206.         isRiding = false;
  207.         isGalloping = false;
  208.         ridingTextUI.GetComponent<Text>().text = "Hit E to Mount\nHit L to keep the horse here";
  209.  
  210.         horseAnimator.SetBool("isRunning", false);
  211.         horseAnimator.SetTrigger("Idle");
  212.  
  213.         // Start the coroutine to wait before re-enabling the script
  214.         StartCoroutine(WaitToReenableScript());
  215.     }
  216.  
  217.     private IEnumerator WaitToReenableScript()
  218.     {
  219.         isWaitingToReenableScript = true;
  220.         float elapsedTime = 0f;
  221.  
  222.         while (elapsedTime < 10f)
  223.         {
  224.             if (Input.GetKeyDown(KeyCode.L))
  225.             {
  226.                 isHorseLockedInPlace = true;
  227.                 ridingTextUI.GetComponent<Text>().text = "Horse will stay put";
  228.                 yield break; // Exit the coroutine without re-enabling the script
  229.             }
  230.  
  231.             elapsedTime += Time.deltaTime;
  232.             yield return null;
  233.         }
  234.  
  235.         if (!isHorseLockedInPlace && !isMounted)
  236.         {
  237.             // Re-enable the script after 10 seconds if the horse isn't locked in place and the player hasn't remounted
  238.             if (scriptToToggle != null)
  239.             {
  240.                 scriptToToggle.enabled = true;
  241.             }
  242.         }
  243.  
  244.         isWaitingToReenableScript = false;
  245.     }
  246.  
  247.     private void StartRiding()
  248.     {
  249.         if (horseAnimator == null)
  250.         {
  251.             Debug.LogError("Horse Animator reference is not assigned in the inspector!");
  252.             return;
  253.         }
  254.  
  255.         isRiding = true;
  256.         horseAnimator.SetTrigger("Move");
  257.     }
  258.  
  259.     private void StopRiding()
  260.     {
  261.         if (horseAnimator == null)
  262.         {
  263.             Debug.LogError("Horse Animator reference is not assigned in the inspector!");
  264.             return;
  265.         }
  266.  
  267.         isRiding = false;
  268.         isGalloping = false;
  269.         horseAnimator.SetTrigger("Idle");
  270.     }
  271.  
  272.     private void StartGallop()
  273.     {
  274.         if (horseAnimator == null)
  275.         {
  276.             Debug.LogError("Horse Animator reference is not assigned in the inspector!");
  277.             return;
  278.         }
  279.  
  280.         isGalloping = true;
  281.         horseAnimator.SetTrigger("Run");
  282.     }
  283.  
  284.     private void StopGallop()
  285.     {
  286.         if (horseAnimator == null)
  287.         {
  288.             Debug.LogError("Horse Animator reference is not assigned in the inspector!");
  289.             return;
  290.         }
  291.  
  292.         isGalloping = false;
  293.         horseAnimator.SetTrigger("Move");
  294.     }
  295.  
  296.     private void Jump()
  297.     {
  298.         if (horseAnimator == null)
  299.         {
  300.             Debug.LogError("Horse Animator reference is not assigned in the inspector!");
  301.             return;
  302.         }
  303.  
  304.         isJumping = true;
  305.         horseAnimator.SetTrigger("Jump");
  306.         StartCoroutine(ResetJumpFlag(1.333f)); // Adjust duration as needed
  307.     }
  308.  
  309.     private IEnumerator ResetJumpFlag(float animationDuration)
  310.     {
  311.         yield return new WaitForSeconds(animationDuration);
  312.         isJumping = false;
  313.  
  314.         if (Input.GetKey(KeyCode.LeftShift))
  315.         {
  316.             StartGallop();
  317.         }
  318.         else
  319.         {
  320.             horseAnimator.SetTrigger("Move");
  321.         }
  322.     }
  323.  
  324.     private void FixedUpdate()
  325.     {
  326.         if (isMounted)
  327.         {
  328.             RaycastHit hit;
  329.  
  330.             // Cast a ray downwards to detect the terrain beneath the horse
  331.             if (Physics.Raycast(mountedHorse.position, Vector3.down, out hit, Mathf.Infinity, terrainLayer))
  332.             {
  333.                 // Adjust the horse's position to match the terrain height
  334.                 Vector3 targetPosition = hit.point + Vector3.up * 0.5f; // Offset to avoid clipping into terrain
  335.                 mountedHorse.position = Vector3.Lerp(mountedHorse.position, targetPosition, Time.deltaTime * 5f);
  336.             }
  337.         }
  338.     }
  339. }
  340.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement