Guest User

FImpossibleDragon

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