Advertisement
rdgorodrigo

Untitled

Nov 13th, 2020
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.04 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.VFX;
  4. using Cinemachine;
  5.  
  6. public class Conejo : MonoBehaviour {
  7.  
  8.     [Header("Movement")]
  9.     public float Speed = 12f;
  10.     public float GroundDetectionRadius = 0.6f;
  11.     public float YOffset = -0.3f, ZOffset;
  12.     [HideInInspector]
  13.     public float StartingSpeed;
  14.     //private Vector3 moveDirection = Vector3.zero;
  15.     public float sprintF = 0;
  16.     float StartingHeight;
  17.     Vector3 StartingCenter;
  18.  
  19.     [Header("Rotation")]
  20.     Quaternion _rotation;
  21.     protected float m_AngleDiff;
  22.     protected Quaternion m_TargetRotation;
  23.  
  24.     [Header("Input")]
  25.     public string horizontalCtrl = "Horizontal";
  26.     public string verticalCtrl = "Vertical";
  27.     public string Fire3 = "Fire3";
  28.     public string AltFire3 = "Fire3";
  29.     public Joystick L_Joystick;
  30.     float turn;
  31.     float mov;
  32.     Vector2 m_Movement;
  33.     bool usingTriggers = false;
  34.  
  35.     [Header("Components")]
  36.     ParticleSystem humo;
  37.     Animator anim;
  38.     CharacterController controller;
  39.     Rigidbody body;
  40.     Stamina stamina;
  41.     Collider col;
  42.     //SmoothCameraWithBumper smCam;
  43.     PlayerHealth health;
  44.     PlayerAttack AttackScript;
  45.     public AudioSource source1,source2;
  46.     public AudioClip superspeed, Ears1, Ears2;
  47.  
  48.     [Header("Physics")]
  49.     //GroundedVars
  50.     public float gravity = 30.0f;
  51.     public LayerMask ground;
  52.     float Startgravity;
  53.     bool isGrounded;
  54.     bool falling;
  55.  
  56.     //AttackVars
  57.     [HideInInspector]
  58.     public float attackforce = 1;
  59.     public float damageForce = 1;
  60.  
  61.     //FlyingVars
  62.     float Startflyspeed;
  63.     [HideInInspector]
  64.     public float fly_speed;
  65.     public bool flying;
  66.     public bool canFly;
  67.  
  68.     //JumpingVars
  69.     public float JumpForce = 11;
  70.     public float ExtraJumpForce = 60;
  71.     float extraJump = 0;
  72.     bool Jumping;
  73.     float nextJump = 0.5f;
  74.  
  75.     [Header("Camera")]
  76.     public Camera cam;
  77.     float smStartDamping, camStartField1, camStartField2, smStartDistance;
  78.     public VisualEffect myEffect;
  79.     public CameraSettings Cams;
  80.     CinemachineFreeLook Cam1, Cam2;
  81.     bool CamFXstarted, CamFXstarted2;
  82.  
  83.     float m_DesiredForwardSpeed;
  84.     const float k_GroundAcceleration = 40f;
  85.     const float k_GroundDeceleration = 80f;
  86.     public float minTurnSpeed = 400f;        
  87.     public float maxTurnSpeed = 1200f;
  88.     const float k_AirborneTurnSpeedProportion = 4f;
  89.     const float k_InverseOneEighty = 1f / 180f;
  90.     protected float m_ForwardSpeed;
  91.     protected bool IsMoveInput
  92.     {
  93.         get { return !Mathf.Approximately(m_Movement.sqrMagnitude, 0f); }
  94.     }
  95.  
  96.     [Header("Network")]
  97.     bool isOnline;
  98.     [HideInInspector]
  99.     public bool isRemotePlayer = true;
  100.  
  101.     public void SetIsRemotePlayer(bool val)
  102.     {
  103.         isRemotePlayer = val;
  104.     }
  105.  
  106.  
  107.     [Header("Customizable")]
  108.     public Transform HatPosition;
  109.     public GameObject HatPrefab;
  110.     // Use this for initialization
  111.     void Start ()
  112.     {
  113.         col = GetComponent<Collider>();
  114.         health = GetComponent<PlayerHealth>();
  115.         AttackScript = GetComponent<PlayerAttack>();
  116.         controller = GetComponent <CharacterController>();
  117.         StartingCenter = controller.center;
  118.         StartingHeight = controller.height;
  119.         anim = gameObject.GetComponent<Animator>();
  120.         humo = GetComponent<ParticleSystem>();
  121.         body = GetComponent<Rigidbody>();
  122.         stamina = GetComponent<Stamina>();
  123.         //smCam = cam.GetComponent<SmoothCameraWithBumper>();
  124.         Cam1 = Cams.keyboardAndMouseCamera.GetComponent<CinemachineFreeLook>();
  125.         Cam2 = Cams.controllerCamera.GetComponent<CinemachineFreeLook>();
  126.         camStartField1 = Cam1.m_Lens.FieldOfView;
  127.         camStartField2 = Cam2.m_Lens.FieldOfView;
  128.         StartingSpeed = Speed;
  129.         //smStartDamping = smCam.damping;
  130.         //smStartDistance = smCam.distance;
  131.         Startgravity = gravity;
  132.         canFly = true;
  133.         if (gameObject.GetComponent<NetworkConejo>() == null)
  134.             isOnline = false;
  135.         else
  136.             isOnline = true;
  137.     }
  138.  
  139.     void Update()
  140.     {
  141.         GroundCheck();
  142.         if (isRemotePlayer && isOnline) return;
  143.         if (Time.timeScale == 0)
  144.         return;
  145.  
  146.         turn = Mathf.Clamp(L_Joystick.Horizontal + Input.GetAxis(horizontalCtrl) + Input.GetAxis("HorizontalJoystick"), -1, 1) ;
  147.         mov = Mathf.Clamp(L_Joystick.Vertical + Input.GetAxis(verticalCtrl) + Input.GetAxis("VerticalJoystick"), -1, 1) ;
  148.         m_Movement.Set(turn, mov);
  149.  
  150. #if UNITY_STANDALONE || UNITY_EDITOR
  151.  
  152.         if (health.isDead || health.damaged)
  153.             return;
  154.  
  155.         if (Input.GetButtonDown(Fire3) && !usingTriggers && canFly && !flying && stamina.curStam > 0)
  156.         {
  157.             StartCoroutine(fly());
  158.         }
  159.  
  160.         if (Input.GetButtonUp(Fire3) && !usingTriggers && flying)
  161.         {
  162.             StopFly();
  163.         }
  164.  
  165.         if (Input.GetAxis(AltFire3) > 0 && !flying && canFly && stamina.curStam > 0)
  166.         {
  167.             usingTriggers = true;
  168.             StartCoroutine(fly());
  169.         }
  170.  
  171.         if (Input.GetAxis(AltFire3) == 0 && usingTriggers && flying)
  172.         {
  173.             usingTriggers = false;
  174.             StopFly();
  175.         }
  176.  
  177.         if (Input.GetButtonDown("Jump") && isGrounded)
  178.         {
  179.             Jumping = true;
  180.             if (Time.time > nextJump)
  181.             {
  182.                 nextJump = Time.time + 0.5f;
  183.                 StartCoroutine(Jump());
  184.             }
  185.         }
  186.         if (Input.GetButton("Jump") && !isGrounded)
  187.         {
  188.             extraJump += Time.deltaTime  * ExtraJumpForce;
  189.         }
  190. #endif
  191.     }
  192.  
  193.  
  194.     private void FixedUpdate()
  195.     {
  196.         Gravity();
  197.         if (isRemotePlayer && isOnline) return;
  198.         CalculateForwardMovement();
  199.         SetTargetRotation();
  200.         if (IsMoveInput)
  201.             UpdateOrientation();
  202.     }
  203.  
  204.     public void Gravity()
  205.     {
  206.         if (!isGrounded && !flying)
  207.         {
  208.             gravity += Time.deltaTime*2;
  209.             fly_speed -= gravity * Time.deltaTime*2.2f;
  210.             if (fly_speed < -50)
  211.                 fly_speed = -50;
  212.         }
  213.         else if (isGrounded && !flying)
  214.         {
  215.             gravity = Startgravity;
  216.         }
  217.         if (gravity > 23)
  218.         {
  219.             gravity = 23;
  220.         }
  221.     }
  222.  
  223.     void GroundCheck()
  224.     {
  225.         var hitColliders = Physics.OverlapSphere(transform.position - new Vector3(ZOffset, YOffset, 0), GroundDetectionRadius, ground);
  226.         {
  227.             if (hitColliders.Length > 0)
  228.             {
  229.                 anim.SetBool("Fall", false);
  230.                 StartCoroutine(Delay1());
  231.                 falling = false;
  232.                 isGrounded = true;
  233.                 extraJump = 0;
  234.             }
  235.             else
  236.             {
  237.                 isGrounded = false;
  238.             }
  239.         }
  240.         if (!isGrounded && !flying && controller.velocity.y < 0 && !health.damaged && !health.isDead)
  241.         {
  242.             falling = true;
  243.             anim.SetBool("Fall",true);
  244.         }
  245.  
  246.         if (stamina.isSprinting && !isGrounded)
  247.         {
  248.             falling = true;
  249.             anim.SetBool("Fall", true);
  250.         }
  251.  
  252.         if (isGrounded && anim.GetCurrentAnimatorStateInfo(0).IsName("Falling") || isGrounded && anim.GetCurrentAnimatorStateInfo(0).IsName("EndFly"))
  253.         {
  254.             canFly = true;
  255.             anim.SetBool("Grounded", true);
  256.         }
  257.  
  258.         if (falling && flying)
  259.         {
  260.             anim.SetBool("Fall", false);
  261.         }
  262.  
  263.         if (isGrounded && anim.GetCurrentAnimatorStateInfo(0).IsName("Jump"))
  264.         {
  265.             anim.SetBool("Fall", false);
  266.             anim.SetBool("Grounded", true);
  267.         }
  268.  
  269.     }
  270.  
  271.     IEnumerator Delay1()
  272.     {
  273.         yield return new WaitForSeconds(0.1f);
  274.         anim.SetBool("Grounded", false);
  275.     }
  276.  
  277.     void CalculateForwardMovement()
  278.     {
  279.  
  280.         Vector2 moveInput = m_Movement;
  281.  
  282.         m_DesiredForwardSpeed = moveInput.magnitude * Speed + sprintF * attackforce * damageForce;
  283.  
  284.         float acceleration = IsMoveInput ? k_GroundAcceleration : k_GroundDeceleration;
  285.  
  286.         m_ForwardSpeed = Mathf.MoveTowards(m_ForwardSpeed, m_DesiredForwardSpeed, acceleration * Time.deltaTime);
  287.  
  288.         if (!flying && !stamina.isSprinting && !falling)
  289.             anim.SetFloat("Walk", Mathf.Abs(m_DesiredForwardSpeed));
  290.     }
  291.  
  292.     void OnAnimatorMove()
  293.     {
  294.         if (isRemotePlayer && isOnline) return;
  295.         if (health.isDead || health.damaged)
  296.             return;
  297.         Vector3 movement;
  298.  
  299.         movement = m_ForwardSpeed * transform.forward * Time.deltaTime;
  300.  
  301.         movement += fly_speed * damageForce * Vector3.up * Time.deltaTime;
  302.  
  303.         controller.Move(movement);
  304.     }
  305.  
  306.  
  307.     void SetTargetRotation()
  308.     {
  309.  
  310.         Vector2 moveInput = m_Movement;
  311.         Vector3 localMovementDirection = new Vector3(moveInput.x, 0f, moveInput.y).normalized;
  312.  
  313.         Vector3 forward = Quaternion.Euler(0f, Cams.Current.m_XAxis.Value, 0f) * Vector3.forward;
  314.         forward.y = 0f;
  315.         forward.Normalize();
  316.  
  317.         Quaternion targetRotation;
  318.  
  319.         if (Mathf.Approximately(Vector3.Dot(localMovementDirection, Vector3.forward), -1.0f))
  320.         {
  321.             targetRotation = Quaternion.LookRotation(-forward);
  322.         }
  323.         else
  324.         {
  325.             Quaternion cameraToInputOffset = Quaternion.FromToRotation(Vector3.forward, localMovementDirection);
  326.             targetRotation = Quaternion.LookRotation(cameraToInputOffset * forward);
  327.         }
  328.  
  329.         Vector3 resultingForward = targetRotation * Vector3.forward;
  330.  
  331.  
  332.         float angleCurrent = Mathf.Atan2(transform.forward.x, transform.forward.z) * Mathf.Rad2Deg;
  333.         float targetAngle = Mathf.Atan2(resultingForward.x, resultingForward.z) * Mathf.Rad2Deg;
  334.  
  335.         m_AngleDiff = Mathf.DeltaAngle(angleCurrent, targetAngle);
  336.         m_TargetRotation = targetRotation;
  337.     }
  338.  
  339.     void UpdateOrientation()
  340.     {
  341.         if (health.isDead)
  342.             return;
  343.         Vector3 localInput = new Vector3(m_Movement.x, 0f, m_Movement.y);
  344.         float groundedTurnSpeed = Mathf.Lerp(maxTurnSpeed, minTurnSpeed, m_ForwardSpeed / m_DesiredForwardSpeed);
  345.         float actualTurnSpeed = isGrounded ? groundedTurnSpeed : Vector3.Angle(transform.forward, localInput) * k_InverseOneEighty * k_AirborneTurnSpeedProportion * groundedTurnSpeed;
  346.         m_TargetRotation = Quaternion.RotateTowards(transform.rotation, m_TargetRotation, actualTurnSpeed * Time.deltaTime);
  347.  
  348.         transform.rotation = m_TargetRotation;
  349.     }
  350.  
  351.  
  352.     IEnumerator Jump()
  353.     {
  354.         fly_speed = JumpForce;
  355.         anim.SetBool("Jump",true);
  356.         float elapsedTime = 0;
  357.         while (elapsedTime < 0.18f)
  358.         {
  359.             elapsedTime += Time.fixedDeltaTime;
  360.             fly_speed = JumpForce + extraJump;
  361.             yield return new WaitForFixedUpdate();
  362.         }
  363.         Jumping = false;
  364.         yield return new WaitForSeconds(0.5f);
  365.         anim.SetBool("Jump", false);
  366.     }
  367.  
  368.     public void Helicopter()
  369.     {
  370.         if (!flying && isGrounded)
  371.         {
  372.             StartCoroutine(fly());
  373.         }
  374.     }
  375.  
  376.     public IEnumerator fly()
  377.     {
  378.         canFly = false;
  379.         flying = true;
  380.         StartCoroutine(SoundFly());
  381.         anim.SetInteger("Hover", 1);
  382.         float elapsedTime = 0;
  383.         while (elapsedTime < 0.5f)
  384.         {
  385.             fly_speed = elapsedTime * 50;
  386.             elapsedTime += Time.fixedDeltaTime;
  387.             yield return new WaitForFixedUpdate();
  388.         }
  389.         fly_speed = 2;
  390.     }
  391.  
  392.     IEnumerator SoundFly()
  393.     {
  394.         SoundManager.instance.PlaySingle(source2, Ears1);
  395.         yield return new WaitForSeconds(Ears1.length);
  396.         if (flying)
  397.             SoundManager.instance.PlayLoop(source2, Ears2);
  398.     }
  399.  
  400.     public void StopFly()
  401.     {
  402.         SoundManager.instance.StopLoop(source2, Ears2);
  403.         anim.SetInteger("Hover",0);
  404.         flying = false;
  405.     }
  406.  
  407.     public void StartSprinting()
  408.     {
  409.         if (health.isDead || health.damaged)
  410.             return;
  411.         stamina.isSprinting = true;
  412.         SoundManager.instance.PlaySingle(source1, superspeed);
  413.         if (!CamFXstarted)
  414.             StartCoroutine(InCamEffect());
  415.         sprintF = 6;
  416.         Speed = 20;
  417.         humo.Play();
  418.     }
  419.  
  420.     public void StopSprinting()
  421.     {
  422.         if (!CamFXstarted2)
  423.             StartCoroutine(OutCamEffect());
  424.         humo.Stop();
  425.         anim.SetInteger("Sprint", 0);
  426.         sprintF = 0;
  427.         Speed = StartingSpeed;
  428.         stamina.isSprinting = false;
  429.     }
  430.  
  431.     IEnumerator InCamEffect()
  432.     {
  433.         CamFXstarted = true;
  434.         myEffect.gameObject.SetActive(true);
  435.         myEffect.SetInt("Rate", 1000);
  436.         float et = 0;
  437.         while (et < 0.5f)
  438.         {
  439.             Cam1.m_Lens.FieldOfView += Time.deltaTime * 25;
  440.             Cam2.m_Lens.FieldOfView += Time.deltaTime * 25;
  441.             et += Time.deltaTime;
  442.             yield return new WaitForSeconds(0);
  443.         }
  444.         CamFXstarted = false;
  445.     }
  446.  
  447.     IEnumerator OutCamEffect()
  448.     {
  449.         CamFXstarted2 = true;
  450.         myEffect.SetInt("Rate", 0);
  451.         float et = 0;
  452.         while (Cam1.m_Lens.FieldOfView > camStartField1)
  453.         {
  454.             Cam1.m_Lens.FieldOfView -= Time.deltaTime * 25;
  455.             Cam2.m_Lens.FieldOfView -= Time.deltaTime * 25;
  456.             et += Time.deltaTime;
  457.             yield return new WaitForSeconds(0);
  458.         }
  459.         Cam1.m_Lens.FieldOfView = camStartField1;
  460.         Cam2.m_Lens.FieldOfView = camStartField2;
  461.         myEffect.gameObject.SetActive(false);
  462.         CamFXstarted2 = false;
  463.     }
  464.  
  465.     public IEnumerator InCamAttack()
  466.     {
  467.         controller.height /= 2;
  468.         controller.center = new Vector3(0, 1.1f, -0.24f);
  469.         yield return null;
  470.     }
  471.  
  472.     public IEnumerator OutCamAttack()
  473.     {
  474.         controller.height = StartingHeight;
  475.         controller.center = StartingCenter;
  476.         yield return null;
  477.     }
  478.  
  479.     private void OnDrawGizmos()
  480.     {
  481.         Gizmos.color = Color.red;
  482.         Gizmos.DrawWireSphere(transform.position - new Vector3(ZOffset, YOffset, 0), GroundDetectionRadius);
  483.     }
  484.  
  485. }  
  486.  
  487.  
  488.  
  489.  
  490.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement