Guest User

Untitled

a guest
Jan 19th, 2024
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.94 KB | None | 0 0
  1. using FIMSpace;
  2. using FIMSpace.Basics;
  3. using FIMSpace.FLook;
  4. using FIMSpace.FSpine;
  5. using FIMSpace.FTail;
  6. using FIMSpace.GroundFitter;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10.  
  11. public class FImpossibleDragon : FGroundFitter_MovementLook
  12. {
  13.     #region Looking Stuff
  14.  
  15.     public LayerMask lookLayerMask = 1 << 0;
  16.     private FLookAnimator look;
  17.     private float smoothBackBonesOffset;
  18.     private Vector3 targetLookPos;
  19.  
  20.     #endregion
  21.  
  22.     private FSpineAnimator spine;
  23.     private List<TailAnimator2> wings;
  24.     private FBasic_TPPCameraBehaviour tpp;
  25.  
  26.     private float doubleClickTimer = 0f;
  27.  
  28.     private bool flying = false;
  29.     private Quaternion targetFlyRotation;
  30.     private float verticalFlySpeed = 0f;
  31.     private bool startFlyBoosting = false;
  32.  
  33.     protected override void Start()
  34.     {
  35.         base.Start();
  36.  
  37.         look = GetComponent<FLookAnimator>();
  38.         smoothBackBonesOffset = look.BackBonesAddOffset.z;
  39.  
  40.         spine = GetComponent<FSpineAnimator>();
  41.         wings = FTransformMethods.FindComponentsInAllChildren<TailAnimator2>(transform);
  42.         tpp = Camera.main.GetComponent<FBasic_TPPCameraBehaviour>();
  43.  
  44.         clips.AddClip("JumpUp");
  45.         clips.AddClip("FallToLanding");
  46.         clips.AddClip("ToFly");
  47.  
  48.         clips.AddClip("FlyStationary");
  49.         clips.AddClip("flyNormal");
  50.         clips.AddClip("glide");
  51.     }
  52.  
  53.  
  54.     protected override void Update()
  55.     {
  56.         base.Update();
  57.  
  58.         #region Looking Stuff
  59.  
  60.         if (targetOfLook)
  61.         {
  62.             targetOfLook.position = Vector3.Lerp(targetOfLook.position, targetLookPos, Time.deltaTime * 8f);
  63.         }
  64.  
  65.         // - 70 to 90
  66.         RaycastHit lookHit;
  67.         Ray lookRay = new Ray(look.LookBones[look.BackBonesCount - 1].Transform.position, Quaternion.Euler(0f, fitter.UpAxisRotation, 0f) * Vector3.forward);
  68.         Physics.Raycast(lookRay, out lookHit, 6f, lookLayerMask, QueryTriggerInteraction.Ignore);
  69.  
  70.         if (lookHit.transform)
  71.         {
  72.             float dist = Vector3.Distance(lookRay.origin, lookHit.point);
  73.             smoothBackBonesOffset = Mathf.Lerp(smoothBackBonesOffset, Mathf.Lerp(-70f, 90f, Mathf.InverseLerp(6f, 3f, dist)), Time.deltaTime * 4f);
  74.         }
  75.         else
  76.         {
  77.             smoothBackBonesOffset = Mathf.Lerp(smoothBackBonesOffset, -70f, Time.deltaTime * 2.5f);
  78.         }
  79.  
  80.         look.BackBonesAddOffset = new Vector3(0f, 0f, smoothBackBonesOffset);
  81.  
  82.         #endregion
  83.  
  84.         if (inAir)
  85.             foreach (var wing in wings) wing.TailAnimatorAmount = Mathf.Lerp(wing.TailAnimatorAmount, 0.3f, Time.deltaTime * 4f);
  86.         else
  87.             foreach (var wing in wings) wing.TailAnimatorAmount = Mathf.Lerp(wing.TailAnimatorAmount, 1f, Time.deltaTime * 1f);
  88.  
  89.         if (flying)
  90.         {
  91.             tpp.FollowingOffset = new Vector3(0f, Mathf.Lerp(tpp.FollowingOffset.y, 5.31f, Time.deltaTime * 2f));
  92.  
  93.             if (MoveVector == Vector3.zero)
  94.                 spine.GoBackSpeed = Mathf.Lerp(spine.GoBackSpeed, 0.25f, Time.deltaTime * 4f);
  95.             else
  96.                 spine.GoBackSpeed = Mathf.Lerp(spine.GoBackSpeed, 0.0f, Time.deltaTime * 4f);
  97.         }
  98.         else
  99.         {
  100.             tpp.FollowingOffset = new Vector3(0f, Mathf.Lerp(tpp.FollowingOffset.y, 1.8f, Time.deltaTime * 2f));
  101.         }
  102.  
  103.         doubleClickTimer -= Time.deltaTime;
  104.     }
  105.  
  106.  
  107.     protected override void HandleAnimations()
  108.     {
  109.         if (!flying)
  110.         {
  111.             if (inAir)
  112.             {
  113.                 if (YVelocity < -3f) CrossfadeTo("FallToLanding", 0.7f);
  114.             }
  115.             else
  116.             {
  117.                 if (ActiveSpeed > 0.15f)
  118.                 {
  119.                     if (Sprint)
  120.                         CrossfadeTo("Run", 0.25f);
  121.                     else
  122.                         CrossfadeTo("Walk", 0.25f);
  123.                 }
  124.                 else
  125.                 {
  126.                     CrossfadeTo("Idle", 0.25f);
  127.                 }
  128.             }
  129.  
  130.             FAnimatorMethods.LerpFloatValue(animator, "AnimationSpeed", MultiplySprintAnimation ? (ActiveSpeed / BaseSpeed) : Mathf.Min(1f, (ActiveSpeed / BaseSpeed)));
  131.         }
  132.         else
  133.         {
  134.             if (!startFlyBoosting)
  135.             {
  136.                 float targetAnimSpeed = 1f;
  137.  
  138.                 if (MoveVector != Vector3.zero)
  139.                 {
  140.                     CrossfadeTo("flyNormal");
  141.                 }
  142.                 else
  143.                 {
  144.                     if (ActiveSpeed > SprintingSpeed) CrossfadeTo("glide");
  145.                     else
  146.                         if (ActiveSpeed > BaseSpeed) CrossfadeTo("flyNormal");
  147.                     else
  148.                         CrossfadeTo("FlyStationary");
  149.                 }
  150.  
  151.                 targetAnimSpeed = Mathf.Clamp(ActiveSpeed / (SprintingSpeed * 1.5f), 1f, 1.8f);
  152.                 FAnimatorMethods.LerpFloatValue(animator, "AnimationSpeed", targetAnimSpeed, 3f);
  153.             }
  154.         }
  155.     }
  156.  
  157.     protected override void HandleGravity()
  158.     {
  159.         if (!flying)
  160.         {
  161.             base.HandleGravity();
  162.         }
  163.         else
  164.         {
  165.             if (!startFlyBoosting)
  166.             {
  167.                 RaycastHit groundHit = fitter.CastRay();
  168.  
  169.                 if (groundHit.transform)
  170.                 {
  171.                     if (Mathf.Abs(groundHit.point.y - transform.position.y) < 0.25f)
  172.                     {
  173.                         flying = false;
  174.                         RefreshHitGroundVars(groundHit);
  175.                         YVelocity = -ActiveSpeed * transform.rotation.eulerAngles.normalized.y;
  176.                         fitter.UpAxisRotation = transform.rotation.eulerAngles.y;
  177.                     }
  178.                 }
  179.             }
  180.         }
  181.     }
  182.  
  183.  
  184.     protected override void HandleTransforming()
  185.     {
  186.         if (!flying)
  187.         {
  188.             base.HandleTransforming();
  189.         }
  190.         else
  191.         {
  192.             fitter.enabled = false;
  193.             inAir = true;
  194.  
  195.             targetFlyRotation = Camera.main.transform.rotation;
  196.             targetFlyRotation = Quaternion.Euler(targetFlyRotation.eulerAngles.x - 25f, targetFlyRotation.eulerAngles.y + RotationOffset, targetFlyRotation.eulerAngles.z);
  197.  
  198.             if (MoveVector != Vector3.zero)
  199.             {
  200.                 if (!Sprint)
  201.                     ActiveSpeed = Mathf.Lerp(ActiveSpeed, BaseSpeed * 3f, delta * AccelerationSpeed * 0.5f);
  202.                 else
  203.                     ActiveSpeed = Mathf.Lerp(ActiveSpeed, SprintingSpeed * 3f, delta * AccelerationSpeed * 0.35f);
  204.             }
  205.             else
  206.             {
  207.                 ActiveSpeed = Mathf.Lerp(ActiveSpeed, -0.05f, Time.deltaTime * 0.75f);
  208.                 if (ActiveSpeed < 0f) ActiveSpeed = 0f;
  209.             }
  210.  
  211.             transform.rotation = Quaternion.Slerp(transform.rotation, targetFlyRotation, Time.deltaTime * 3f);
  212.             transform.position += transform.forward * ActiveSpeed * delta;
  213.  
  214.             if (Input.GetKey(KeyCode.LeftControl)) ActiveSpeed = Mathf.Lerp(ActiveSpeed, 0f, Time.deltaTime * 1f);
  215.  
  216.             if (Input.GetKey(KeyCode.Space)) verticalFlySpeed = Mathf.Lerp(verticalFlySpeed, 1f, Time.deltaTime);
  217.             else
  218.             if (Input.GetKey(KeyCode.Z)) verticalFlySpeed = Mathf.Lerp(verticalFlySpeed, -1f, Time.deltaTime);
  219.             else
  220.                 verticalFlySpeed = Mathf.Lerp(verticalFlySpeed, 0f, Time.deltaTime * 3f);
  221.  
  222.             transform.position += Vector3.up * verticalFlySpeed * 20f * delta;
  223.  
  224.             SetLookAtPosition(transform.position + Camera.main.transform.forward * 10f + Vector3.up);
  225.         }
  226.     }
  227.  
  228.  
  229.     public override void Jump()
  230.     {
  231.         base.Jump();
  232.         CrossfadeTo("JumpUp", 0.5f);
  233.  
  234.         if (doubleClickTimer > 0f) StartFlying();
  235.  
  236.         doubleClickTimer = 0.5f;
  237.     }
  238.  
  239.  
  240.     private void StartFlying()
  241.     {
  242.         flying = true;
  243.         CrossfadeTo("ToFly", 0.5f);
  244.         StartCoroutine(StartFlyBoost());
  245.     }
  246.  
  247.  
  248.     private IEnumerator StartFlyBoost()
  249.     {
  250.         startFlyBoosting = true;
  251.  
  252.         float time = 0f;
  253.         while (time < 0.5f)
  254.         {
  255.             time += Time.deltaTime;
  256.             transform.position += Vector3.up * delta * 11f;
  257.             yield return null;
  258.         }
  259.  
  260.         startFlyBoosting = false;
  261.     }
  262.  
  263.     protected override void HitGround()
  264.     {
  265.         base.HitGround();
  266.         StartCoroutine(StraightenSpine());
  267.     }
  268.  
  269.  
  270.     private IEnumerator StraightenSpine()
  271.     {
  272.         while (spine.GoBackSpeed < 0.5f)
  273.         {
  274.             spine.GoBackSpeed = Mathf.Lerp(spine.GoBackSpeed, 0.55f, Time.deltaTime * 5f);
  275.             yield return null;
  276.         }
  277.  
  278.         while (spine.GoBackSpeed > 0f)
  279.         {
  280.             spine.GoBackSpeed = Mathf.Lerp(spine.GoBackSpeed, -0.05f, Time.deltaTime * 5f);
  281.             yield return null;
  282.         }
  283.  
  284.         spine.GoBackSpeed = 0f;
  285.     }
  286.  
  287.     protected override void SetLookAtPosition(Vector3 tPos)
  288.     {
  289.         targetLookPos = tPos;
  290.     }
  291. }
Advertisement
Add Comment
Please, Sign In to add comment