Advertisement
evelynshilosky

HorsebackRidingSystem

Apr 26th, 2024
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.29 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.  
  16.     private List<Transform> horsesInRange = new List<Transform>();
  17.     private Transform mountedHorse;
  18.     private Vector3 playerOffsetFromHorse;
  19.  
  20.     private bool isMounted = false;
  21.     private bool isRiding = false;
  22.     private bool isGalloping = false;
  23.     private bool isJumping = false;
  24.  
  25.     private void Update()
  26.     {
  27.         // Check if the player reference is null
  28.         if (player == null || ridingTextUI == null || horseAnimator == null)
  29.         {
  30.             Debug.LogError("Player, Riding Text UI, or Horse Animator references are not assigned in the inspector!");
  31.             return;
  32.         }
  33.  
  34.         UpdateHorsesInRange();
  35.  
  36.         if (!isMounted)
  37.         {
  38.             if (horsesInRange.Count > 0 && Input.GetKeyDown(KeyCode.E))
  39.             {
  40.                 Mount(GetClosestHorse());
  41.             }
  42.             else if (horsesInRange.Count > 0)
  43.             {
  44.                 ridingTextUI.GetComponent<Text>().text = "Hit E to Mount";
  45.             }
  46.             else
  47.             {
  48.                 ridingTextUI.GetComponent<Text>().text = " ";
  49.             }
  50.         }
  51.         else if (!isRiding)
  52.         {
  53.             if (Input.GetKeyDown(KeyCode.E))
  54.             {
  55.                 Dismount();
  56.             }
  57.             else if (Input.GetKeyDown(KeyCode.R))
  58.             {
  59.                 StartRiding();
  60.             }
  61.             else
  62.             {
  63.                 ridingTextUI.GetComponent<Text>().text = "Hit R to Ride or E to unmount";
  64.             }
  65.         }
  66.         else
  67.         {
  68.             if (Input.GetKeyDown(KeyCode.Space) && !isJumping)
  69.             {
  70.                 Jump();
  71.             }
  72.             else if (Input.GetKeyDown(KeyCode.R))
  73.             {
  74.                 StopRiding();
  75.             }
  76.             else if (Input.GetKeyDown(KeyCode.LeftShift))
  77.             {
  78.                 StartGallop();
  79.             }
  80.             else if (Input.GetKeyUp(KeyCode.LeftShift))
  81.             {
  82.                 StopGallop();
  83.             }
  84.             else
  85.             {
  86.                 ridingTextUI.GetComponent<Text>().text = "Hold Left Shift to gallop\nSpace to jump\nR to stop riding";
  87.             }
  88.         }
  89.     }
  90.  
  91.     private void UpdateHorsesInRange()
  92.     {
  93.         horsesInRange.Clear();
  94.  
  95.         foreach (GameObject horse in horses)
  96.         {
  97.             if (horse == null)
  98.             {
  99.                 Debug.LogWarning("One of the horses is null!");
  100.                 continue;
  101.             }
  102.  
  103.             float distance = Vector3.Distance(player.transform.position, horse.transform.position);
  104.             if (distance <= mountRange)
  105.             {
  106.                 horsesInRange.Add(horse.transform);
  107.             }
  108.         }
  109.     }
  110.  
  111.     private Transform GetClosestHorse()
  112.     {
  113.         float minDistance = Mathf.Infinity;
  114.         Transform closestHorse = null;
  115.  
  116.         foreach (Transform horse in horsesInRange)
  117.         {
  118.             if (horse == null)
  119.             {
  120.                 Debug.LogWarning("One of the horses is null!");
  121.                 continue;
  122.             }
  123.  
  124.             float distance = Vector3.Distance(player.transform.position, horse.position);
  125.             if (distance < minDistance)
  126.             {
  127.                 minDistance = distance;
  128.                 closestHorse = horse;
  129.             }
  130.         }
  131.  
  132.         return closestHorse;
  133.     }
  134.  
  135.     private void Mount(Transform horse)
  136.     {
  137.         if (horse == null)
  138.         {
  139.             Debug.LogWarning("No horse found to mount!");
  140.             return;
  141.         }
  142.  
  143.         if (player == null || ridingTextUI == null)
  144.         {
  145.             Debug.LogError("Player or Riding Text UI references are not assigned in the inspector!");
  146.             return;
  147.         }
  148.  
  149.         ridingTextUI.GetComponent<Text>().text = "Hit R to Ride or E to unmount";
  150.         mountedHorse = horse;
  151.         mountedHorse.Find("Rider").gameObject.SetActive(true);
  152.         player.SetActive(false);
  153.         playerOffsetFromHorse = player.transform.position - mountedHorse.position;
  154.  
  155.         // Activate HorseMouseMovement script
  156.         HorseMouseMovement horseMouseMovement = mountedHorse.GetComponent<HorseMouseMovement>();
  157.         if (horseMouseMovement != null)
  158.         {
  159.             horseMouseMovement.Activate();
  160.         }
  161.  
  162.         player.transform.position = mountedHorse.position + playerOffsetFromHorse;
  163.  
  164.         // Update the unmounted player position to the current player position
  165.         playerUnmountPosition.transform.position = player.transform.position;
  166.  
  167.         isMounted = true;
  168.     }
  169.  
  170.     private void Dismount()
  171.     {
  172.         if (player == null || ridingTextUI == null)
  173.         {
  174.             Debug.LogError("Player or Riding Text UI references are not assigned in the inspector!");
  175.             return;
  176.         }
  177.  
  178.         player.SetActive(true);
  179.  
  180.         // Deactivate HorseMouseMovement script
  181.         HorseMouseMovement horseMouseMovement = mountedHorse.GetComponent<HorseMouseMovement>();
  182.         if (horseMouseMovement != null)
  183.         {
  184.             horseMouseMovement.Deactivate();
  185.         }
  186.  
  187.         // Adjust player position to match playerUnmountPosition
  188.         player.transform.position = playerUnmountPosition.transform.position;
  189.  
  190.         mountedHorse.Find("Rider").gameObject.SetActive(false);
  191.         isMounted = false;
  192.         isRiding = false;
  193.         isGalloping = false;
  194.         ridingTextUI.GetComponent<Text>().text = "Hit E to Mount";
  195.     }
  196.  
  197.     private void StartRiding()
  198.     {
  199.         if (horseAnimator == null)
  200.         {
  201.             Debug.LogError("Horse Animator reference is not assigned in the inspector!");
  202.             return;
  203.         }
  204.  
  205.         isRiding = true;
  206.         horseAnimator.SetTrigger("Move");
  207.     }
  208.  
  209.     private void StopRiding()
  210.     {
  211.         if (horseAnimator == null)
  212.         {
  213.             Debug.LogError("Horse Animator reference is not assigned in the inspector!");
  214.             return;
  215.         }
  216.  
  217.         isRiding = false;
  218.         isGalloping = false;
  219.         horseAnimator.SetTrigger("Idle");
  220.     }
  221.  
  222.     private void StartGallop()
  223.     {
  224.         if (horseAnimator == null)
  225.         {
  226.             Debug.LogError("Horse Animator reference is not assigned in the inspector!");
  227.             return;
  228.         }
  229.  
  230.         isGalloping = true;
  231.         horseAnimator.SetTrigger("Run");
  232.     }
  233.  
  234.     private void StopGallop()
  235.     {
  236.         if (horseAnimator == null)
  237.         {
  238.             Debug.LogError("Horse Animator reference is not assigned in the inspector!");
  239.             return;
  240.         }
  241.  
  242.         isGalloping = false;
  243.         horseAnimator.SetTrigger("Move");
  244.     }
  245.  
  246.     private void Jump()
  247.     {
  248.         if (horseAnimator == null)
  249.         {
  250.             Debug.LogError("Horse Animator reference is not assigned in the inspector!");
  251.             return;
  252.         }
  253.  
  254.         isJumping = true;
  255.         horseAnimator.SetTrigger("Jump");
  256.         StartCoroutine(ResetJumpFlag(1.333f)); // Adjust duration as needed
  257.     }
  258.  
  259.     private IEnumerator ResetJumpFlag(float animationDuration)
  260.     {
  261.         yield return new WaitForSeconds(animationDuration);
  262.         isJumping = false;
  263.  
  264.         if (Input.GetKey(KeyCode.LeftShift))
  265.         {
  266.             StartGallop();
  267.         }
  268.         else
  269.         {
  270.             horseAnimator.SetTrigger("Move");
  271.         }
  272.     }
  273.  
  274.     private void FixedUpdate()
  275.     {
  276.         if (isMounted)
  277.         {
  278.             RaycastHit hit;
  279.  
  280.             // Cast a ray downwards to detect the terrain beneath the horse
  281.             if (Physics.Raycast(mountedHorse.position, Vector3.down, out hit, Mathf.Infinity, terrainLayer))
  282.             {
  283.                 // Adjust the horse's position to match the terrain height
  284.                 Vector3 targetPosition = hit.point + Vector3.up * 0.5f; // Offset to avoid clipping into terrain
  285.                 mountedHorse.position = Vector3.Lerp(mountedHorse.position, targetPosition, Time.deltaTime * 5f);
  286.             }
  287.         }
  288.     }
  289. }
  290.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement