Advertisement
Guest User

FImpossibleDragon.cs

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