Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.VFX;
- using Cinemachine;
- public class Conejo : MonoBehaviour {
- [Header("Movement")]
- public float Speed = 12f;
- public float GroundDetectionRadius = 0.6f;
- public float YOffset = -0.3f, ZOffset;
- [HideInInspector]
- public float StartingSpeed;
- //private Vector3 moveDirection = Vector3.zero;
- public float sprintF = 0;
- float StartingHeight;
- Vector3 StartingCenter;
- [Header("Rotation")]
- Quaternion _rotation;
- protected float m_AngleDiff;
- protected Quaternion m_TargetRotation;
- [Header("Input")]
- public string horizontalCtrl = "Horizontal";
- public string verticalCtrl = "Vertical";
- public string Fire3 = "Fire3";
- public string AltFire3 = "Fire3";
- public Joystick L_Joystick;
- float turn;
- float mov;
- Vector2 m_Movement;
- bool usingTriggers = false;
- [Header("Components")]
- ParticleSystem humo;
- Animator anim;
- CharacterController controller;
- Rigidbody body;
- Stamina stamina;
- Collider col;
- //SmoothCameraWithBumper smCam;
- PlayerHealth health;
- PlayerAttack AttackScript;
- public AudioSource source1,source2;
- public AudioClip superspeed, Ears1, Ears2;
- [Header("Physics")]
- //GroundedVars
- public float gravity = 30.0f;
- public LayerMask ground;
- float Startgravity;
- bool isGrounded;
- bool falling;
- //AttackVars
- [HideInInspector]
- public float attackforce = 1;
- public float damageForce = 1;
- //FlyingVars
- float Startflyspeed;
- [HideInInspector]
- public float fly_speed;
- public bool flying;
- public bool canFly;
- //JumpingVars
- public float JumpForce = 11;
- public float ExtraJumpForce = 60;
- float extraJump = 0;
- bool Jumping;
- float nextJump = 0.5f;
- [Header("Camera")]
- public Camera cam;
- float smStartDamping, camStartField1, camStartField2, smStartDistance;
- public VisualEffect myEffect;
- public CameraSettings Cams;
- CinemachineFreeLook Cam1, Cam2;
- bool CamFXstarted, CamFXstarted2;
- float m_DesiredForwardSpeed;
- const float k_GroundAcceleration = 40f;
- const float k_GroundDeceleration = 80f;
- public float minTurnSpeed = 400f;
- public float maxTurnSpeed = 1200f;
- const float k_AirborneTurnSpeedProportion = 4f;
- const float k_InverseOneEighty = 1f / 180f;
- protected float m_ForwardSpeed;
- protected bool IsMoveInput
- {
- get { return !Mathf.Approximately(m_Movement.sqrMagnitude, 0f); }
- }
- [Header("Network")]
- bool isOnline;
- [HideInInspector]
- public bool isRemotePlayer = true;
- public void SetIsRemotePlayer(bool val)
- {
- isRemotePlayer = val;
- }
- [Header("Customizable")]
- public Transform HatPosition;
- public GameObject HatPrefab;
- // Use this for initialization
- void Start ()
- {
- col = GetComponent<Collider>();
- health = GetComponent<PlayerHealth>();
- AttackScript = GetComponent<PlayerAttack>();
- controller = GetComponent <CharacterController>();
- StartingCenter = controller.center;
- StartingHeight = controller.height;
- anim = gameObject.GetComponent<Animator>();
- humo = GetComponent<ParticleSystem>();
- body = GetComponent<Rigidbody>();
- stamina = GetComponent<Stamina>();
- //smCam = cam.GetComponent<SmoothCameraWithBumper>();
- Cam1 = Cams.keyboardAndMouseCamera.GetComponent<CinemachineFreeLook>();
- Cam2 = Cams.controllerCamera.GetComponent<CinemachineFreeLook>();
- camStartField1 = Cam1.m_Lens.FieldOfView;
- camStartField2 = Cam2.m_Lens.FieldOfView;
- StartingSpeed = Speed;
- //smStartDamping = smCam.damping;
- //smStartDistance = smCam.distance;
- Startgravity = gravity;
- canFly = true;
- if (gameObject.GetComponent<NetworkConejo>() == null)
- isOnline = false;
- else
- isOnline = true;
- }
- void Update()
- {
- GroundCheck();
- if (isRemotePlayer && isOnline) return;
- if (Time.timeScale == 0)
- return;
- turn = Mathf.Clamp(L_Joystick.Horizontal + Input.GetAxis(horizontalCtrl) + Input.GetAxis("HorizontalJoystick"), -1, 1) ;
- mov = Mathf.Clamp(L_Joystick.Vertical + Input.GetAxis(verticalCtrl) + Input.GetAxis("VerticalJoystick"), -1, 1) ;
- m_Movement.Set(turn, mov);
- #if UNITY_STANDALONE || UNITY_EDITOR
- if (health.isDead || health.damaged)
- return;
- if (Input.GetButtonDown(Fire3) && !usingTriggers && canFly && !flying && stamina.curStam > 0)
- {
- StartCoroutine(fly());
- }
- if (Input.GetButtonUp(Fire3) && !usingTriggers && flying)
- {
- StopFly();
- }
- if (Input.GetAxis(AltFire3) > 0 && !flying && canFly && stamina.curStam > 0)
- {
- usingTriggers = true;
- StartCoroutine(fly());
- }
- if (Input.GetAxis(AltFire3) == 0 && usingTriggers && flying)
- {
- usingTriggers = false;
- StopFly();
- }
- if (Input.GetButtonDown("Jump") && isGrounded)
- {
- Jumping = true;
- if (Time.time > nextJump)
- {
- nextJump = Time.time + 0.5f;
- StartCoroutine(Jump());
- }
- }
- if (Input.GetButton("Jump") && !isGrounded)
- {
- extraJump += Time.deltaTime * ExtraJumpForce;
- }
- #endif
- }
- private void FixedUpdate()
- {
- Gravity();
- if (isRemotePlayer && isOnline) return;
- CalculateForwardMovement();
- SetTargetRotation();
- if (IsMoveInput)
- UpdateOrientation();
- }
- public void Gravity()
- {
- if (!isGrounded && !flying)
- {
- gravity += Time.deltaTime*2;
- fly_speed -= gravity * Time.deltaTime*2.2f;
- if (fly_speed < -50)
- fly_speed = -50;
- }
- else if (isGrounded && !flying)
- {
- gravity = Startgravity;
- }
- if (gravity > 23)
- {
- gravity = 23;
- }
- }
- void GroundCheck()
- {
- var hitColliders = Physics.OverlapSphere(transform.position - new Vector3(ZOffset, YOffset, 0), GroundDetectionRadius, ground);
- {
- if (hitColliders.Length > 0)
- {
- anim.SetBool("Fall", false);
- StartCoroutine(Delay1());
- falling = false;
- isGrounded = true;
- extraJump = 0;
- }
- else
- {
- isGrounded = false;
- }
- }
- if (!isGrounded && !flying && controller.velocity.y < 0 && !health.damaged && !health.isDead)
- {
- falling = true;
- anim.SetBool("Fall",true);
- }
- if (stamina.isSprinting && !isGrounded)
- {
- falling = true;
- anim.SetBool("Fall", true);
- }
- if (isGrounded && anim.GetCurrentAnimatorStateInfo(0).IsName("Falling") || isGrounded && anim.GetCurrentAnimatorStateInfo(0).IsName("EndFly"))
- {
- canFly = true;
- anim.SetBool("Grounded", true);
- }
- if (falling && flying)
- {
- anim.SetBool("Fall", false);
- }
- if (isGrounded && anim.GetCurrentAnimatorStateInfo(0).IsName("Jump"))
- {
- anim.SetBool("Fall", false);
- anim.SetBool("Grounded", true);
- }
- }
- IEnumerator Delay1()
- {
- yield return new WaitForSeconds(0.1f);
- anim.SetBool("Grounded", false);
- }
- void CalculateForwardMovement()
- {
- Vector2 moveInput = m_Movement;
- m_DesiredForwardSpeed = moveInput.magnitude * Speed + sprintF * attackforce * damageForce;
- float acceleration = IsMoveInput ? k_GroundAcceleration : k_GroundDeceleration;
- m_ForwardSpeed = Mathf.MoveTowards(m_ForwardSpeed, m_DesiredForwardSpeed, acceleration * Time.deltaTime);
- if (!flying && !stamina.isSprinting && !falling)
- anim.SetFloat("Walk", Mathf.Abs(m_DesiredForwardSpeed));
- }
- void OnAnimatorMove()
- {
- if (isRemotePlayer && isOnline) return;
- if (health.isDead || health.damaged)
- return;
- Vector3 movement;
- movement = m_ForwardSpeed * transform.forward * Time.deltaTime;
- movement += fly_speed * damageForce * Vector3.up * Time.deltaTime;
- controller.Move(movement);
- }
- void SetTargetRotation()
- {
- Vector2 moveInput = m_Movement;
- Vector3 localMovementDirection = new Vector3(moveInput.x, 0f, moveInput.y).normalized;
- Vector3 forward = Quaternion.Euler(0f, Cams.Current.m_XAxis.Value, 0f) * Vector3.forward;
- forward.y = 0f;
- forward.Normalize();
- Quaternion targetRotation;
- if (Mathf.Approximately(Vector3.Dot(localMovementDirection, Vector3.forward), -1.0f))
- {
- targetRotation = Quaternion.LookRotation(-forward);
- }
- else
- {
- Quaternion cameraToInputOffset = Quaternion.FromToRotation(Vector3.forward, localMovementDirection);
- targetRotation = Quaternion.LookRotation(cameraToInputOffset * forward);
- }
- Vector3 resultingForward = targetRotation * Vector3.forward;
- float angleCurrent = Mathf.Atan2(transform.forward.x, transform.forward.z) * Mathf.Rad2Deg;
- float targetAngle = Mathf.Atan2(resultingForward.x, resultingForward.z) * Mathf.Rad2Deg;
- m_AngleDiff = Mathf.DeltaAngle(angleCurrent, targetAngle);
- m_TargetRotation = targetRotation;
- }
- void UpdateOrientation()
- {
- if (health.isDead)
- return;
- Vector3 localInput = new Vector3(m_Movement.x, 0f, m_Movement.y);
- float groundedTurnSpeed = Mathf.Lerp(maxTurnSpeed, minTurnSpeed, m_ForwardSpeed / m_DesiredForwardSpeed);
- float actualTurnSpeed = isGrounded ? groundedTurnSpeed : Vector3.Angle(transform.forward, localInput) * k_InverseOneEighty * k_AirborneTurnSpeedProportion * groundedTurnSpeed;
- m_TargetRotation = Quaternion.RotateTowards(transform.rotation, m_TargetRotation, actualTurnSpeed * Time.deltaTime);
- transform.rotation = m_TargetRotation;
- }
- IEnumerator Jump()
- {
- fly_speed = JumpForce;
- anim.SetBool("Jump",true);
- float elapsedTime = 0;
- while (elapsedTime < 0.18f)
- {
- elapsedTime += Time.fixedDeltaTime;
- fly_speed = JumpForce + extraJump;
- yield return new WaitForFixedUpdate();
- }
- Jumping = false;
- yield return new WaitForSeconds(0.5f);
- anim.SetBool("Jump", false);
- }
- public void Helicopter()
- {
- if (!flying && isGrounded)
- {
- StartCoroutine(fly());
- }
- }
- public IEnumerator fly()
- {
- canFly = false;
- flying = true;
- StartCoroutine(SoundFly());
- anim.SetInteger("Hover", 1);
- float elapsedTime = 0;
- while (elapsedTime < 0.5f)
- {
- fly_speed = elapsedTime * 50;
- elapsedTime += Time.fixedDeltaTime;
- yield return new WaitForFixedUpdate();
- }
- fly_speed = 2;
- }
- IEnumerator SoundFly()
- {
- SoundManager.instance.PlaySingle(source2, Ears1);
- yield return new WaitForSeconds(Ears1.length);
- if (flying)
- SoundManager.instance.PlayLoop(source2, Ears2);
- }
- public void StopFly()
- {
- SoundManager.instance.StopLoop(source2, Ears2);
- anim.SetInteger("Hover",0);
- flying = false;
- }
- public void StartSprinting()
- {
- if (health.isDead || health.damaged)
- return;
- stamina.isSprinting = true;
- SoundManager.instance.PlaySingle(source1, superspeed);
- if (!CamFXstarted)
- StartCoroutine(InCamEffect());
- sprintF = 6;
- Speed = 20;
- humo.Play();
- }
- public void StopSprinting()
- {
- if (!CamFXstarted2)
- StartCoroutine(OutCamEffect());
- humo.Stop();
- anim.SetInteger("Sprint", 0);
- sprintF = 0;
- Speed = StartingSpeed;
- stamina.isSprinting = false;
- }
- IEnumerator InCamEffect()
- {
- CamFXstarted = true;
- myEffect.gameObject.SetActive(true);
- myEffect.SetInt("Rate", 1000);
- float et = 0;
- while (et < 0.5f)
- {
- Cam1.m_Lens.FieldOfView += Time.deltaTime * 25;
- Cam2.m_Lens.FieldOfView += Time.deltaTime * 25;
- et += Time.deltaTime;
- yield return new WaitForSeconds(0);
- }
- CamFXstarted = false;
- }
- IEnumerator OutCamEffect()
- {
- CamFXstarted2 = true;
- myEffect.SetInt("Rate", 0);
- float et = 0;
- while (Cam1.m_Lens.FieldOfView > camStartField1)
- {
- Cam1.m_Lens.FieldOfView -= Time.deltaTime * 25;
- Cam2.m_Lens.FieldOfView -= Time.deltaTime * 25;
- et += Time.deltaTime;
- yield return new WaitForSeconds(0);
- }
- Cam1.m_Lens.FieldOfView = camStartField1;
- Cam2.m_Lens.FieldOfView = camStartField2;
- myEffect.gameObject.SetActive(false);
- CamFXstarted2 = false;
- }
- public IEnumerator InCamAttack()
- {
- controller.height /= 2;
- controller.center = new Vector3(0, 1.1f, -0.24f);
- yield return null;
- }
- public IEnumerator OutCamAttack()
- {
- controller.height = StartingHeight;
- controller.center = StartingCenter;
- yield return null;
- }
- private void OnDrawGizmos()
- {
- Gizmos.color = Color.red;
- Gizmos.DrawWireSphere(transform.position - new Vector3(ZOffset, YOffset, 0), GroundDetectionRadius);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement