Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 36.89 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. //Signature Enumeration Data
  4. public enum Signature
  5. {
  6.     Pure = 0,
  7.     Echo = 1,
  8.     Reverb = 2,
  9.     PerfectCrescendo = 3
  10. }
  11.  
  12. [RequireComponent(typeof(Rigidbody))]
  13. [RequireComponent(typeof(CapsuleCollider))]
  14. [RequireComponent(typeof(Animator))]
  15.  
  16. public class SymphonicBehaviour : MonoBehaviour {
  17.  
  18.     //Character  
  19.     [Header("Character Info")]
  20.     public Vector3 moveDirection;
  21.     public float inputMag;
  22.     public Vector3 targetHeading = Vector3.up;
  23.     public float moveAcceleration = 20;//Base force value used to accelerate the character    
  24.     public float angularAccelerationBase = 10;//Base force value used to rotate the character  
  25.  
  26.     public float playerHeight = 2;
  27.     public float colliderRadiusMin = .25f;//Used for standing
  28.     public float colliderRadiusMax = .4f;//Used for flight Curl
  29.  
  30.     public bool grounded = false;
  31.     public bool groundCheckHit = false;
  32.     public float groundedCheckLenght = 1.8f;
  33.     public float jumpBuffer = 0;
  34.     public float jumpBufferMax = 1;
  35.     public float jumpPower = 5;
  36.     public bool dashing = false;
  37.     public float dashSpeed = 50;
  38.     public float dashTime = 0;
  39.     public float dashLength = .2f;
  40.     public float dashInterupt = .05f;
  41.     public float dashWindup = .02f;
  42.     public bool accelerator = false;
  43.     public float acceleratorTime = 0;
  44.     public float acceleratorWindup = 3;
  45.     public float groundTopSpeed = 10;
  46.     public float groundTopSpeedBase = 10;
  47.     public bool groundLocomotionInterupt = false;
  48.     public float spiderWalkScaler = 0;
  49.     public bool useCoyoteTime = false;
  50.     public float coyoteTime = 0;
  51.     public float coyoteTimeMax = .1f;
  52.     public bool intangible = false;
  53.     public float landingType = 0;
  54.     public float landingLagTime = 100;
  55.     public float landingLagTimeMax = 2;
  56.  
  57.  
  58.     public bool flight = true;
  59.    
  60.     public float airTopSpeed = 180;
  61.  
  62.     private bool insideVolume = false;
  63.     public float previousMediumDensity = 0;
  64.     public float currentMediumDensity = 0;
  65.  
  66.     public float powerLevel = 1f;
  67.     public float charging = 0;    
  68.     public Vector4 emissionColor = Color.white;
  69.  
  70.     private Vector3 camLocalOffset = Vector3.zero;
  71.     private float camZoomDistanceMin = 4;
  72.     private float camZoomDistanceMax = 10;
  73.  
  74.     //References
  75.     [Header("Componets")]
  76.     public Animator animator;
  77.     public Rigidbody rb;
  78.     public CapsuleCollider capsuleCollider;
  79.     public DynamicBone[] dynamicBones;  
  80.     public TrailRenderer trailRendererLeft;
  81.     public TrailRenderer trailRendererRight;
  82.     public CameraControl cameraControl;
  83.     public Object particleExplosion;
  84.     public Material[] material;
  85.     public Material emissionMaterial;
  86.     public ChargeParticleSystem chargeParticleSystem;
  87.  
  88.     [ColorUsage(true, true)] public Color[] signatureColors = new Color[]
  89.     {
  90.         new Vector4(0, 5.146699f, 9.082411f, 1),
  91.         new Vector4(0, 9.082411f, 9.082411f, 1),
  92.         new Vector4(9.082411f, 0.7568676f, 0, 1),
  93.         new Vector4(29.64f, 10.56273f, 0, 1)
  94.     };
  95.  
  96.  
  97.  
  98.     //Physics        
  99.     [Header("Physics")]
  100.     public Vector3 netForce = Vector3.zero;
  101.     public float rbVelocityMagnatude;
  102.     public Vector3 rbVelocityNormalized;
  103.     public Vector3 localAngularVelocity;
  104.     private float lateralDrag;
  105.     public float feetGrip = 1;
  106.     public Vector3 groundNormal = Vector3.up;
  107.     public Vector3 angularGain = new Vector3(1, 1, 1);
  108.     public float liftScale = 1;
  109.     public float rollSpinUp = 0;
  110.  
  111.     [Header("Gravity")]
  112.     public float enableGravity;
  113.     public GravityManager gravityManager;
  114.     public Vector3 gravity = Vector3.down;
  115.  
  116.     //Input data      
  117.     [Header("Input Data")]
  118.     public float thrust = 0;
  119.     public bool thrustAsButtion = false;
  120.     public bool canRun = false;
  121.     public float rollAxisInput = 0;
  122.     public float pitchAxisInput = 0;
  123.     public float yawAxisInput = 0;
  124.     public float focus = 0;
  125.     private float strafe = 0;
  126.     private int dPad = 0;
  127.    
  128.  
  129.     private void Reset()
  130.     {
  131.         gameObject.layer = 10;
  132.         gameObject.tag = "Player";
  133.  
  134.         animator = GetComponent<Animator>();
  135.         animator.applyRootMotion = false;
  136.         animator.runtimeAnimatorController = Resources.Load<RuntimeAnimatorController>("Symphonic/Animation/SymphonicController");
  137.  
  138.         rb = GetComponent<Rigidbody>();    
  139.         rb.useGravity = false;
  140.         rb.angularDrag = 5;
  141.         rb.drag = 0.1f;
  142.         rb.collisionDetectionMode = CollisionDetectionMode.Continuous;        
  143.  
  144.         particleExplosion = Resources.Load<Object>("Prefabs/Explosion1");        
  145.  
  146.         material[0] = Resources.Load<Material>("Symphonic/Mat_Symphonic");
  147.         material[1] = Resources.Load<Material>("Symphonic/Face");
  148.  
  149.         emissionMaterial = Resources.Load<Material>("Symphonic/Mat_Emission");
  150.  
  151.         capsuleCollider = GetComponent<CapsuleCollider>();        
  152.         capsuleCollider.radius = .25f;//.5 for sphere mode
  153.         capsuleCollider.height = playerHeight;//0 for sphere mode
  154.         capsuleCollider.direction = 1;//Y-Axis                    
  155.     }
  156.  
  157.     void Start()
  158.     {
  159.         cameraControl = Camera.main.GetComponent<CameraControl>();              
  160.         targetHeading = Camera.main.transform.forward;
  161.         gravityManager = FindObjectOfType<GravityManager>();
  162.         rb.maxAngularVelocity = 20;
  163.         rb.centerOfMass = Vector3.zero;
  164.     }
  165.  
  166.     private void Update()
  167.     {
  168.         foreach(DynamicBone bone in dynamicBones)
  169.         {
  170.             bone.m_Stiffness = powerLevel;// Mathf.InverseLerp(3, 20, Mathf.Abs(localAngularVelocity.y));
  171.             bone.UpdateParameters();
  172.         }
  173.  
  174.         //JumpBuffer Update
  175.         {
  176.             jumpBuffer += Time.deltaTime;
  177.         }
  178.  
  179.         //Input magnatude, used for checking if a player is pressing in a direction
  180.         {
  181.             if (grounded)
  182.                 inputMag = Mathf.Clamp01(Mathf.Abs(pitchAxisInput) + Mathf.Abs(rollAxisInput));
  183.             else
  184.                 inputMag = 0;
  185.         }
  186.  
  187.         //DashTime update
  188.         {
  189.             if (dashing)
  190.             {
  191.                 dashTime += Time.deltaTime;
  192.             }
  193.  
  194.             if (dashTime >= dashLength)
  195.             {
  196.                 dashTime = 0;
  197.                 dashing = false;
  198.             }
  199.  
  200.             //Disable Dashing if no input after groundLocomotionInterupt is finished.
  201.             if (dashing && !groundLocomotionInterupt && inputMag == 0)
  202.             {
  203.                 dashing = false;
  204.             }
  205.         }
  206.        
  207.         //canRun reset
  208.         {
  209.             if (inputMag == 0) canRun = false;
  210.         }    
  211.  
  212.         //AcceleratorTime Update
  213.         {
  214.             if (canRun)
  215.                 acceleratorTime += Time.deltaTime;
  216.  
  217.             if (inputMag < .5f)
  218.                 acceleratorTime = 0;
  219.  
  220.             acceleratorTime = Mathf.Clamp(acceleratorTime, 0, acceleratorWindup + 1);
  221.         }
  222.  
  223.  
  224.  
  225.         //Accelerator Calulation        
  226.         {
  227.             if (acceleratorTime > acceleratorWindup && canRun)
  228.             {
  229.                 accelerator = true;
  230.             }
  231.             else
  232.             {
  233.                 accelerator = false;
  234.             }
  235.         }
  236.  
  237.         //Coyote Time Extend
  238.         {            
  239.             switch (accelerator)
  240.             {
  241.                 case true:
  242.                     coyoteTimeMax = Mathf.Min(coyoteTimeMax + 2 * Time.deltaTime,2);                    
  243.                     break;
  244.                 case false:
  245.                     coyoteTimeMax = Mathf.Max(coyoteTimeMax - 2 * Time.deltaTime, .1f);
  246.                     break;
  247.             }
  248.         }
  249.  
  250.         //Power Level charge and decharge
  251.         {
  252.  
  253.             if (Mathf.Abs(localAngularVelocity.y) > 10 || accelerator)
  254.             {                            
  255.                 powerLevel = Mathf.Min(powerLevel + Time.deltaTime, 1);
  256.                          
  257.                 charging = Mathf.Min((powerLevel + 2 * Time.deltaTime) * (1 - thrust), 1);
  258.  
  259.                 chargeParticleSystem.state = 1;
  260.                 if (powerLevel == 1)
  261.                     chargeParticleSystem.state = 0;
  262.             }
  263.             else if (Mathf.Abs(localAngularVelocity.y) < 2 && !accelerator)
  264.             {
  265.                 powerLevel = Mathf.Max(powerLevel - Time.deltaTime, 0);
  266.  
  267.                 charging = Mathf.Max((powerLevel - 2 * Time.deltaTime) * (1 - thrust), 0);
  268.  
  269.                 chargeParticleSystem.state = 0;                            
  270.             }
  271.  
  272.             emissionColor = signatureColors[0];
  273.  
  274.             if (material[0] != null)
  275.             {
  276.                 foreach (Material material in material)
  277.                 {
  278.                     material.SetColor("_EmissiveColor", emissionColor);
  279.                     material.SetFloat("_ShellPower", ((1 - powerLevel) * 5) + 1);
  280.                 }
  281.             }
  282.  
  283.             if (emissionMaterial != null)
  284.                 emissionMaterial.SetColor("_EmissiveColor", emissionColor * 1);
  285.  
  286.         }
  287.  
  288.         //Trail Control
  289.         {
  290.             float widthMultiplier;
  291.  
  292.             float beginTrailSpeed = 30;
  293.             float trailGrowRange = 30;
  294.  
  295.             float velScale = Mathf.InverseLerp(beginTrailSpeed, beginTrailSpeed + trailGrowRange, rbVelocityMagnatude);
  296.             float widthScale = 1;// + (Mathf.Clamp01(Mathf.Abs(localAngularVelocity.y) / 20) * 4);
  297.             float angxDisable = 1 - Mathf.Clamp01(Mathf.Abs(localAngularVelocity.x / 5));// if pitch speed is 0.2 or greater disable width.              
  298.  
  299.             widthMultiplier = velScale * angxDisable * widthScale * (1-charging);
  300.  
  301.             widthMultiplier *= Mathf.Clamp01((Vector3.Dot(rbVelocityNormalized, transform.up) * 2) - 1);
  302.  
  303.             if (trailRendererLeft != null)
  304.             {
  305.                 trailRendererLeft.widthMultiplier = 0.05f * widthMultiplier;
  306.                 trailRendererLeft.time = widthMultiplier == 0 ? 0 : 0.2f;
  307.             }
  308.  
  309.             if (trailRendererRight != null)
  310.             {
  311.                 trailRendererRight.widthMultiplier = 0.05f * widthMultiplier;
  312.                 trailRendererRight.time = widthMultiplier == 0 ? 0 : 0.2f;
  313.             }
  314.         }
  315.  
  316.         //adjust how the player should appear on screen.  
  317.         {
  318.             Vector3 headHeight = new Vector3(0, .7f, 0);
  319.  
  320.             if (!grounded)
  321.             {
  322.                 camLocalOffset = Vector3.zero;
  323.                 camZoomDistanceMin = 4;
  324.                 if (powerLevel == 1 && rbVelocityMagnatude < 20)
  325.                     camZoomDistanceMin = 1;
  326.                 camZoomDistanceMax = 10;
  327.             }
  328.             else
  329.             {
  330.                 camLocalOffset = headHeight;
  331.                 camZoomDistanceMin = 5;
  332.                 camZoomDistanceMax = 5;
  333.             }
  334.  
  335.             //cameraControl.lerpSpeed = Mathf.Lerp(20,50,Mathf.InverseLerp());
  336.             cameraControl.localOffset = Vector3.Slerp(cameraControl.localOffset, camLocalOffset, Time.deltaTime * 2);
  337.             cameraControl.zoomDistanceMin = Mathf.Lerp(cameraControl.zoomDistanceMin, camZoomDistanceMin, Time.deltaTime);
  338.             cameraControl.zoomDistanceMax = Mathf.Lerp(cameraControl.zoomDistanceMax, camZoomDistanceMax, Time.deltaTime);
  339.  
  340.         }
  341.  
  342.         //Set animator values
  343.         if (animator != null)
  344.         {
  345.             animator.SetFloat("Speed", rbVelocityMagnatude, 0.1f, Time.deltaTime);
  346.             animator.SetFloat("VerticalSpeed", rbVelocityMagnatude * Vector3.Dot(rbVelocityNormalized, -gravity.normalized), 0.1f, Time.deltaTime);            
  347.             animator.SetFloat("RotZ", Mathf.Abs(localAngularVelocity.z), 0.1f, Time.deltaTime);
  348.             animator.SetFloat("RotX", Mathf.Abs(localAngularVelocity.x), 0.1f, Time.deltaTime);
  349.             animator.SetFloat("RotY", Mathf.Abs(localAngularVelocity.y), 0.1f, Time.deltaTime);
  350.             animator.SetFloat("Drag", rb.drag / .2f, 0.3f, Time.deltaTime);
  351.             animator.SetFloat("DragDir", lateralDrag, 0.1f, Time.deltaTime);
  352.             animator.SetFloat("Charge", charging, 0.1f, Time.deltaTime);                      
  353.             animator.SetFloat("LandingType", landingType);
  354.             animator.SetFloat("GroundForwardSpeed", rbVelocityMagnatude * Vector3.Dot(rbVelocityNormalized, transform.forward), 0.1f, Time.deltaTime);
  355.             animator.SetFloat("GroundLateralSpeed", rbVelocityMagnatude * Vector3.Dot(rbVelocityNormalized, transform.right), 0.1f, Time.deltaTime);
  356.             animator.SetBool("Grounded", grounded);
  357.             animator.SetBool("CanFly", flight);
  358.             if (grounded)
  359.                 animator.speed = Mathf.InverseLerp(12, 30, rbVelocityMagnatude) * 1f + 1;
  360.             else
  361.                 animator.speed = 1;
  362.         }
  363.  
  364.     }
  365.  
  366.     void FixedUpdate()
  367.     {    
  368.         netForce = Vector3.zero;        
  369.  
  370.         //Maintain Spin if inside of an object
  371.         //if (insideVolume)
  372.         {
  373.             //rb.angularVelocity = rb.angularVelocity.normalized * 20;
  374.         }
  375.  
  376.         //Intangibility
  377.         {
  378.             if (intangible)                
  379.             {
  380.                 capsuleCollider.isTrigger = true;
  381.                 cameraControl.clippingAllowed = true;
  382.             }
  383.  
  384.             if (Mathf.Abs(localAngularVelocity.y) < 10)
  385.             {
  386.                 capsuleCollider.isTrigger = false;
  387.                 cameraControl.clippingAllowed = false;
  388.             }
  389.         }  
  390.  
  391.         //Update values
  392.         {
  393.             rbVelocityMagnatude = rb.velocity.magnitude;
  394.             rbVelocityNormalized = rb.velocity.normalized;
  395.             localAngularVelocity = transform.InverseTransformDirection(rb.angularVelocity);
  396.         }
  397.  
  398.         //Gravity Calulation    
  399.         {                                  
  400.             if (gravityManager != null)
  401.             {
  402.                 gravity = gravityManager.ReturnGravity(transform);
  403.  
  404.                 if (spiderWalkScaler == 1)
  405.                 {
  406.                     gravity = Vector3.Lerp(gravity, gravity.magnitude * -groundNormal, spiderWalkScaler);
  407.                     //Camera upvector modification
  408.                     {
  409.                         cameraControl.manualUpVectorScaler = spiderWalkScaler;
  410.                         cameraControl.manualUpVector = -gravity.normalized;
  411.                     }
  412.                 }
  413.                 else
  414.                 {
  415.                     cameraControl.manualUpVectorScaler = 0;
  416.                 }
  417.             }
  418.             else
  419.             {
  420.                 gravity = Vector3.Lerp(gravity, Vector3.zero, Time.deltaTime);
  421.             }
  422.                                        
  423.         }
  424.  
  425.         // Main Behaviour function
  426.         {
  427.             RaycastHit hit;
  428.             Ray raycastRay = new Ray(transform.position, Vector3.Lerp(gravity, gravity.magnitude * -groundNormal, spiderWalkScaler));  
  429.  
  430.             groundCheckHit = Physics.Raycast(raycastRay, out hit, 1000, Physics.DefaultRaycastLayers, QueryTriggerInteraction.Collide);
  431.  
  432.             //check to make sure we have not left the ground
  433.             {
  434.                 if (!dashing)
  435.                 {
  436.                    
  437.                     if (groundCheckHit)        
  438.                     {
  439.                         if (hit.distance <= groundedCheckLenght)
  440.                         {
  441.                             groundCheckHit = true;
  442.                             groundNormal = hit.normal;
  443.                         }
  444.                         else
  445.                         {
  446.                             groundCheckHit = false;
  447.                         }
  448.  
  449.                         if (hit.distance > groundedCheckLenght * 2)
  450.                         {
  451.                             useCoyoteTime = true;
  452.                         }
  453.                         else
  454.                         {
  455.                             useCoyoteTime = false;
  456.                         }
  457.                     }  
  458.                     else
  459.                     {
  460.                         useCoyoteTime = true;
  461.                     }
  462.  
  463.                 }
  464.  
  465.                 //check if we have exausted our coyoteTime
  466.                 if (coyoteTime >= coyoteTimeMax)
  467.                 {
  468.                     grounded = false;
  469.                     spiderWalkScaler = 0;
  470.                 }
  471.  
  472.                 //CoyoteTime increment happens after checking its value
  473.                 if (!groundCheckHit)
  474.                 {
  475.                     coyoteTime += Time.deltaTime;
  476.                 }
  477.  
  478.             }
  479.  
  480.             if (grounded)
  481.             {                        
  482.                 //Passive Ground adjustments and calulations
  483.                 {
  484.                     //Ground Effects
  485.                     {    
  486.  
  487.                     }
  488.  
  489.                     //Reset Movement Values for when on ground          
  490.                     {
  491.                         rb.angularDrag = 1000;
  492.                         moveAcceleration = 40;
  493.                         rb.drag = 1f;
  494.                     }
  495.  
  496.                     //Calulate the move direction, based on camera direction and input
  497.                     {
  498.                         Vector3 movez = Vector3.Cross(Camera.main.transform.right, groundNormal);
  499.                         Vector3 movex = -Vector3.Cross(Camera.main.transform.forward, groundNormal);
  500.  
  501.                         moveDirection = movez * pitchAxisInput + movex * rollAxisInput;
  502.                        
  503.  
  504.                         //Vector3 project = Vector3.ProjectOnPlane(Camera.main.transform.forward, groundNormal);
  505.                         //Debug.DrawRay(hit.point,project,Color.blue);
  506.                         //Vector3 cross = Vector3.Cross(groundNormal, project);
  507.                         //Debug.DrawRay(hit.point, cross, Color.red);
  508.  
  509.                         //moveDirection = project * pitchAxisInput + cross * rollAxisInput;
  510.  
  511.                         //Debug.DrawRay(hit.point + groundNormal * .5f, movez, Color.black);
  512.                         //Debug.DrawRay(hit.point + groundNormal * .5f, movex, Color.black);
  513.  
  514.  
  515.                     }
  516.  
  517.                     //Set rotation on ground.
  518.                     {
  519.                         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.Cross(transform.right, -gravity), -gravity), Time.deltaTime * 10);
  520.                         rb.angularVelocity = Vector3.zero;
  521.                     }
  522.  
  523.                     //Adjust Capsule Collider
  524.                     {
  525.                         capsuleCollider.radius = colliderRadiusMin;
  526.                         capsuleCollider.height = playerHeight;
  527.                     }
  528.                 }
  529.  
  530.                 //increment landing lag
  531.                 {
  532.                     landingLagTime += Time.deltaTime;
  533.                 }
  534.  
  535.                 //calulate locomotion input interruption
  536.                 {
  537.                     groundLocomotionInterupt = false;
  538.  
  539.                     if (dashTime < dashInterupt && dashing)
  540.                         groundLocomotionInterupt = true;
  541.  
  542.                     if (landingLagTime < landingLagTimeMax)
  543.                         groundLocomotionInterupt = true;
  544.  
  545.                 }  
  546.  
  547.                 //Dash abblity
  548.                 if (!groundLocomotionInterupt)
  549.                 {
  550.                     if (thrustAsButtion)
  551.                     {
  552.                         thrustAsButtion = false;
  553.                         if (inputMag > .1f)
  554.                         {                          
  555.                             rb.velocity = moveDirection.normalized * dashSpeed;
  556.                             rbVelocityNormalized = rb.velocity.normalized;
  557.                             transform.rotation = Quaternion.LookRotation(moveDirection, -gravity);
  558.                             powerLevel = 1;
  559.                             dashing = true;
  560.                             dashTime = 0;
  561.                             canRun = true;
  562.                             acceleratorTime = acceleratorWindup;
  563.                             accelerator = true;
  564.                             groundTopSpeed = dashSpeed;
  565.                             groundLocomotionInterupt = true;
  566.                         }
  567.                        
  568.                     }
  569.                 }
  570.  
  571.                 //Maintain Dash
  572.                 if (dashing)
  573.                 {
  574.                     rb.velocity = rbVelocityNormalized * dashSpeed;
  575.                 }
  576.                            
  577.                 //Player Driven locomotion
  578.                 if (!groundLocomotionInterupt)
  579.                 {
  580.                     //Character faces the move direction            
  581.                     {
  582.                         if (inputMag > .1f)
  583.                             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(moveDirection, -gravity), Time.deltaTime * 10);
  584.                     }
  585.  
  586.                     //Jumping requires instant velocity change. Cant use netForce field.                
  587.                     if (jumpBuffer <= jumpBufferMax)
  588.                     {
  589.                         jumpBuffer = Mathf.Infinity;
  590.                         animator.SetTrigger("Jump");                        
  591.                         grounded = false;
  592.                         rb.AddForce(groundNormal * jumpPower, ForceMode.VelocityChange);
  593.                         Debug.Log("Jump!");
  594.                         //spiderWalkScaler = 0;
  595.                         coyoteTime = coyoteTimeMax;
  596.                     }
  597.  
  598.                     //Ground Top Speed Control
  599.                     {
  600.                         if (accelerator)
  601.                         {
  602.                             groundTopSpeed += dashSpeed * .1f * Time.deltaTime;
  603.                             groundTopSpeed = Mathf.Min(groundTopSpeed, dashSpeed);
  604.                         }
  605.                         else
  606.                         {
  607.                             groundTopSpeed -= dashSpeed * 1f * Time.deltaTime;
  608.                             float offset = 0;
  609.                             if (canRun) offset = groundTopSpeedBase;
  610.                             groundTopSpeed = Mathf.Max(groundTopSpeed, groundTopSpeedBase + offset);
  611.                         }
  612.                            
  613.                     }
  614.  
  615.                     //Calulate Feet Grip
  616.                     {
  617.                         feetGrip = Mathf.Clamp01(Mathf.Clamp01(Vector3.Dot(rbVelocityNormalized, transform.forward)) + .2f);
  618.                     }
  619.  
  620.                     //Forward Acceleration on ground
  621.                     {
  622.                         netForce += moveDirection * moveAcceleration * feetGrip;
  623.                     }
  624.                 }
  625.  
  626.                 //Control Drag
  627.                 {
  628.  
  629.                     //Grip
  630.                     float disable = Mathf.Clamp01(Vector3.Dot(rbVelocityNormalized, moveDirection));
  631.  
  632.                     //feetGrip = disable;
  633.  
  634.                     //max velocity adjustment
  635.                     float highEndDrag = 0;
  636.  
  637.                     if (!dashing)
  638.                         highEndDrag = Mathf.Lerp(0, 10, Mathf.InverseLerp(groundTopSpeed - 1, groundTopSpeed, rbVelocityMagnatude));
  639.  
  640.                     float stopDrag = (1 - inputMag) * 4;
  641.  
  642.                     rb.drag =  (stopDrag + highEndDrag) * feetGrip;
  643.  
  644.                    // if (!dashing)
  645.                        // rb.velocity = Vector3.ClampMagnitude(rb.velocity, groundTopSpeed);
  646.  
  647.                     if (dashing)
  648.                     {
  649.                         rb.drag = 0;
  650.                     }
  651.                 }
  652.  
  653.                 //Lift force
  654.                 if (!groundLocomotionInterupt)
  655.                 {            
  656.                     RedirectForce(moveDirection, feetGrip);
  657.                 }
  658.  
  659.             }
  660.             else if (flight)
  661.             {
  662.                 //reset these values for flight
  663.                 {
  664.                     rb.angularDrag = 5;
  665.                 }
  666.  
  667.                 //adjust Capsule Collider
  668.                 {
  669.                     float t = Mathf.InverseLerp(3.9f, 4, Mathf.Abs(localAngularVelocity.x));
  670.                     capsuleCollider.radius = Mathf.Lerp(colliderRadiusMin, colliderRadiusMax, t);
  671.                     capsuleCollider.height = Mathf.Lerp(playerHeight, 0, t);
  672.                 }
  673.  
  674.                 //The character flies "upwards" from their walking orientation normally
  675.                 Vector3 flightForward = transform.up;
  676.                 Vector3 flightUp = -transform.forward;
  677.  
  678.                 //determin TargetHeading
  679.                 {
  680.                     Vector3 focusedHeading = Camera.main.transform.forward;
  681.  
  682.                     Vector3 signedVelocityNormal = rbVelocityNormalized * Mathf.Sign(Vector3.Dot(rbVelocityNormalized, flightForward));
  683.                     Vector3 unfocusedHeading = Vector3.Lerp(signedVelocityNormal, flightForward, thrust);//Lerp between using the Velocity and the characters flight forward for no rotation effect. Stabalizes forward flight at low thrust levels
  684.                     unfocusedHeading = Vector3.Lerp(flightForward, unfocusedHeading, rbVelocityMagnatude);//extra scale based on velocity for when stationary in air
  685.  
  686.                     targetHeading = Vector3.Lerp(unfocusedHeading, focusedHeading, focus);//Lerp between using the unfocusedHeading, and using the camera.
  687.                 }
  688.  
  689.                 //Align towards the target heading.                    
  690.                 {
  691.                     Vector3 axis = Vector3.Cross(flightForward, targetHeading);//Get the axis to turn around.
  692.  
  693.                     float input = Mathf.Clamp01(Mathf.Abs(pitchAxisInput * 2) + Mathf.Abs(rollAxisInput * 2));//Scalar to negate auto turning                                              
  694.  
  695.                     rb.AddTorque(axis * (1 - input) * 10, ForceMode.Acceleration);
  696.                 }
  697.  
  698.                 //Set Spinup values
  699.                 {
  700.                     if (Mathf.Abs(rollAxisInput) == 1)
  701.                     {
  702.                         rollSpinUp += Mathf.Lerp(1, 3, Mathf.InverseLerp(80, airTopSpeed, rbVelocityMagnatude)) * Time.deltaTime;
  703.                     }
  704.                     else
  705.                     {
  706.                         rollSpinUp -= 3 * Time.deltaTime;
  707.                     }
  708.  
  709.                     rollSpinUp = Mathf.Clamp01(rollSpinUp);
  710.                 }
  711.  
  712.                 //Calulate angular acceleration force gain.
  713.                 {
  714.                     angularGain = new Vector3(5 - (4 * thrust), 4 * (1 + rollSpinUp * Mathf.Lerp(1, 2, Mathf.InverseLerp(30, airTopSpeed, rbVelocityMagnatude))), 1);
  715.                 }
  716.  
  717.                 //Angular Acceleration              
  718.                 {
  719.                     //Roll - y
  720.                     rb.AddTorque(-flightForward * rollAxisInput * angularAccelerationBase * angularGain.y, ForceMode.Acceleration);
  721.                     //Pitch - x
  722.                     rb.AddTorque(transform.right * pitchAxisInput * angularAccelerationBase * angularGain.x, ForceMode.Acceleration);
  723.                     //Yaw - z
  724.                     rb.AddTorque(flightUp * yawAxisInput * angularAccelerationBase * angularGain.z, ForceMode.Acceleration);
  725.                 }
  726.  
  727.                 //Forward Acceleration            
  728.                 {
  729.                     float t = Mathf.InverseLerp(10, 20, Mathf.Abs(localAngularVelocity.y));
  730.  
  731.                     moveAcceleration = Mathf.Lerp(10, 20, t);
  732.                     netForce += flightForward * thrust * moveAcceleration;
  733.  
  734.                     if (cameraControl != null)
  735.                         cameraControl.shakeMagnatude += .05f * thrust * t * Mathf.InverseLerp(20, 70, rbVelocityMagnatude);
  736.                 }
  737.  
  738.                 //Player Activated Boost
  739.                 if (thrustAsButtion)
  740.                 {
  741.                     thrustAsButtion = false;
  742.  
  743.                     //Boosting                    
  744.                     {
  745.  
  746.                         intangible = true;
  747.  
  748.                         //rb.AddForce(flightForward * 80 * powerLevel * (1 + rollSpinUp), ForceMode.VelocityChange);
  749.  
  750.                         rb.velocity = Vector3.ClampMagnitude(rb.velocity + flightForward * 80 * powerLevel * (1 + rollSpinUp), airTopSpeed);
  751.                      
  752.                         CauseCameraShake(powerLevel);
  753.  
  754.                         charging = 0;
  755.  
  756.                         if (chargeParticleSystem != null) chargeParticleSystem.state = 0;
  757.  
  758.                         if (powerLevel == 1)
  759.                         {
  760.                             if (particleExplosion != null) Instantiate(particleExplosion, transform.position, transform.rotation);
  761.                             ShockWave(transform.position);
  762.                         }
  763.  
  764.                         //The boost engergy has been consumed
  765.                         powerLevel = 0;
  766.                     }
  767.  
  768.                 }
  769.  
  770.                 //Control Drag
  771.                 {
  772.                     //drag along the characters flight up vector, signed. Ignoring side axis drag.
  773.                     lateralDrag = Vector3.Dot(rbVelocityNormalized, flightUp);
  774.  
  775.                     //drag along the characters flight forward vector, only using force along the negative forward axis.
  776.                     float backwardsDrag = Mathf.Clamp01(Vector3.Dot(rbVelocityNormalized, -flightForward));
  777.  
  778.                     //Disable drag while summersaulting
  779.                     float disable = 1 - Mathf.Clamp01(Mathf.Abs(localAngularVelocity.x / 2));
  780.  
  781.                     //max velocity adjustment
  782.                     float highEndDrag = Mathf.Lerp(0, 10, Mathf.InverseLerp(airTopSpeed - 20, airTopSpeed, rbVelocityMagnatude));
  783.  
  784.                     // backwards + lateral + highEnd
  785.                     rb.drag = Mathf.Lerp(0, 0.2f, backwardsDrag * disable) + Mathf.Lerp(0, 1f, Mathf.Abs(lateralDrag) * disable) + highEndDrag;
  786.                 }
  787.  
  788.                 //Lift force
  789.                 {                  
  790.                     liftScale = Mathf.Clamp01(liftScale + 1 * Time.deltaTime ) * thrust;                    
  791.                     RedirectForce(flightForward, liftScale);
  792.                 }
  793.  
  794.             }
  795.             else
  796.             {
  797.                 rb.drag = 0;
  798.             }
  799.  
  800.         }
  801.  
  802.         //Apply Gravity
  803.         {
  804.  
  805.             enableGravity = 1;
  806.  
  807.             //enableGravity = enableGravity * (1 - Mathf.InverseLerp(0, 7, Mathf.Abs(localAngularVelocity.y)));
  808.  
  809.             if (focus == 1)
  810.                 enableGravity = 0;
  811.  
  812.             if (coyoteTime < coyoteTimeMax && useCoyoteTime)
  813.                 enableGravity = 0;
  814.  
  815.             netForce += gravity * enableGravity;
  816.         }
  817.  
  818.         //apply all net forces acting on object caused by character behaviour
  819.         {
  820.             rb.AddForce(netForce, ForceMode.Acceleration);
  821.         }
  822.  
  823.         //PID CONTROL
  824.         {
  825.  
  826.  
  827.         }
  828.  
  829.     }
  830.  
  831.     //A callback for calculating IK
  832.     void OnAnimatorIK()
  833.     {
  834.         if (animator && cameraControl)
  835.         {
  836.             float t = Mathf.InverseLerp(10, 0, localAngularVelocity.magnitude);
  837.             animator.SetLookAtWeight(t, .2f, .8f, 1, .5f);
  838.             animator.SetLookAtPosition(cameraControl.characterTargetingPosition);          
  839.         }
  840.     }
  841.  
  842.     private void RedirectForce(Vector3 axis, float scale)
  843.     {
  844.         //Lift Force
  845.         Vector3 forceVector = Vector3.Cross(axis, -rbVelocityNormalized).normalized;
  846.         Vector3 crossVector = Vector3.Cross(forceVector, axis);
  847.         //rb.AddForce(crossVector * rbVelMag * Vector3.Dot(crossVector, -rbVelNorm) * 5 * scale);
  848.         netForce += crossVector * rbVelocityMagnatude * Vector3.Dot(crossVector, -rbVelocityNormalized) * 5 * scale;
  849.     }
  850.  
  851.     void Beat(float currentStep)
  852.     {
  853.         float beatScale = 1;
  854.         powerLevel += 1 * beatScale;    
  855.     }
  856.  
  857.     private void OnTriggerEnter(Collider other)
  858.     {
  859.         if (other.tag == "Water")
  860.         {
  861.             previousMediumDensity = currentMediumDensity;
  862.             currentMediumDensity = .1f;            
  863.         }
  864.         CauseCameraShake(rbVelocityMagnatude / 20);
  865.     }
  866.  
  867.     private void OnTriggerExit(Collider other)
  868.     {
  869.         currentMediumDensity = previousMediumDensity;
  870.         previousMediumDensity = 0;
  871.     }
  872.  
  873.     private void OnCollisionEnter(Collision collision)
  874.     {
  875.  
  876.         coyoteTime = 0;
  877.  
  878.         if (!grounded)
  879.         {
  880.             float shake = 0;
  881.             Vector3 hitPoint = collision.GetContact(0).point;
  882.             Vector3 hitNormal = collision.GetContact(0).normal;
  883.  
  884.             groundNormal = hitNormal;                                                                                        
  885.                                
  886.             //bounce if we pressed the jump button and will return to flight          
  887.             if (jumpBuffer <= jumpBufferMax && flight)
  888.             {
  889.                 print("bounce");
  890.  
  891.                 jumpBuffer = Mathf.Infinity;
  892.  
  893.                 //Find Reflection Vector
  894.                 Vector3 reflect = Vector3.Reflect(rbVelocityNormalized, hitNormal);
  895.  
  896.                 //Character Effects
  897.                 {
  898.                     //Reflect Velocity
  899.                     rb.velocity = reflect * rbVelocityMagnatude;
  900.                     //Cancel Angular Velocity Caused By Collission
  901.                     rb.angularVelocity = Vector3.zero;
  902.                     //Reflect Orientation
  903.                     transform.rotation = Quaternion.LookRotation(Vector3.Cross(Vector3.Cross(reflect, rbVelocityNormalized), reflect), hitNormal);
  904.                 }
  905.  
  906.                 //Camera Tracks towards next bounce position if one exists
  907.                 RaycastHit bounceFutureCheck;
  908.                 if (Physics.Raycast(transform.position, reflect, out bounceFutureCheck, 1000, ~((1 << 8) | (1 << 2) | (1 << 10)), QueryTriggerInteraction.Ignore))
  909.                 {
  910.                     cameraControl.cameraTargetingPosition = bounceFutureCheck.point;
  911.                     cameraControl.trackLerpScale = .1f;
  912.                 }
  913.                 else
  914.                 {
  915.                     cameraControl.cameraTargetingPosition = transform.position + rb.velocity * 10;
  916.                     cameraControl.trackLerpScale = .1f;
  917.                 }
  918.             }
  919.             else
  920.             {
  921.                 print("landing");
  922.  
  923.                 //Character Effects
  924.                 {
  925.                     powerLevel = 1;
  926.                     grounded = true;
  927.                     landingType = 0;
  928.  
  929.                     //Check if we should ground the character normally, or if the character should stick to walls
  930.  
  931.                     if (Vector3.Dot(hitNormal, -gravity.normalized) < .5f)
  932.                     {
  933.                         spiderWalkScaler = 1;                      
  934.                     }
  935.  
  936.                     //Land hard if conditions are right
  937.                     float dot = Mathf.Abs(Vector3.Dot(rbVelocityNormalized, hitNormal));
  938.                     if (dot > 0.85f && rbVelocityMagnatude > 40)
  939.                     {
  940.                         landingLagTime = 0;
  941.                         landingType = 2;
  942.                         rb.velocity = Vector3.zero;
  943.                         print("hard!");
  944.                     }
  945.                     else//Adjust accelerator
  946.                     {
  947.                         Vector3 projection = Vector3.ProjectOnPlane(rb.velocity, hitNormal);
  948.                         float mag = projection.magnitude;
  949.                         canRun = true;
  950.                         acceleratorTime += Mathf.Lerp(0, acceleratorWindup, Mathf.InverseLerp(0, 50, mag));
  951.                         groundTopSpeed = mag;//Don't wait for the accelerator to increment top speed, it must be set on touchdown.
  952.                     }
  953.  
  954.                 }
  955.  
  956.                 //Visual Effects
  957.                 {
  958.                     if (particleExplosion != null)
  959.                         Instantiate(particleExplosion, hitPoint + hitNormal, Quaternion.LookRotation(hitNormal) * Quaternion.Euler(90, 0, 0));
  960.  
  961.                     shake = rbVelocityMagnatude / 20;
  962.                 }
  963.  
  964.                 //Explosion force
  965.                 {
  966.                     ShockWave(hitPoint);
  967.                 }
  968.             }
  969.  
  970.             CauseCameraShake(shake);
  971.         }
  972.     }
  973.  
  974.     private void CauseCameraShake(float value)
  975.     {
  976.         if (cameraControl != null)
  977.             cameraControl.shakeMagnatude += value;
  978.     }
  979.    
  980.     void ShockWave(Vector3 location)
  981.     {      
  982.         float power = 25;
  983.         float radius = 50;
  984.         float upForce = 5;
  985.  
  986.         Collider[] colliders = Physics.OverlapSphere(location, radius);
  987.         foreach (Collider collider in colliders)
  988.         {
  989.             Rigidbody colliderRb = collider.GetComponent<Rigidbody>();
  990.             if (colliderRb != null)
  991.             {
  992.                 if (colliderRb != rb)
  993.                     colliderRb.AddExplosionForce(power, location, radius, upForce, ForceMode.VelocityChange);
  994.             }
  995.         }
  996.    
  997.     }
  998.  
  999.     private void RedirectVelocity(Quaternion fromOrientation, Quaternion toOrientation)//No use as of yet
  1000.     {
  1001.         rbVelocityNormalized = toOrientation * (Quaternion.Inverse(fromOrientation) * rbVelocityNormalized);
  1002.         rb.velocity = rbVelocityNormalized * rbVelocityMagnatude;
  1003.     }
  1004. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement