Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private bool running, jumping, crouching;
- private float currentSpeed, velocityY, animationSpeedPercent;
- Animator animator;
- ClimbingSystem climbingSystem;
- CharacterController controller;
- [Header("Debug Only")]
- [SerializeField] private float targetSpeed;
- [SerializeField] private float acceleration;
- [Header("Input Strings")]
- [SerializeField] private string sprintButton;
- [SerializeField] private string jumpButton;
- [SerializeField] private string crouchButton;
- [Header("Character Parameters")]
- [SerializeField] private float startWalkSpeed;
- private float walkSpeed;
- [SerializeField] private float maxWalkSpeed;
- [SerializeField] private float startAcceleration;
- [SerializeField] private float maxAcceleration;
- [SerializeField] private float runSpeed;
- [SerializeField] private float crouchSpeed;
- [SerializeField] private float jumpHeight = 1;
- [SerializeField] private float crouchCharacterHeight = 1f;
- [SerializeField] private float normalCharacterHeight = 1.6f;
- [SerializeField] private float jumpCharacterHeight = 1f;
- [SerializeField] [Range(0, 1f)] private float heightSmoothTime = 0.5f;
- [SerializeField] [Range(0, 1f)] private float turnSmoothTime = 0.2f;
- private float turnSmoothVelocity;
- [SerializeField] [Range(0, 1f)] private float speedSmoothTime = 0.1f;
- private float speedSmoothVelocity;
- [SerializeField] private float gravity = -12.0f;
- [SerializeField] [Range(0, 1f)] private float airControlPercent;
- [Header("Climbing System Parameters")]
- [SerializeField] private bool canClimb;
- [SerializeField] private bool canBrace;
- private float yOffset;
- [SerializeField] float bracedYOffset = 1.2f;
- [SerializeField] float hangingYOffset = 1.8f;
- [Header("Other")]
- [SerializeField] private GameObject cameraT;
- void Start ()
- {
- animator = GetComponent<Animator>();
- controller = GetComponent<CharacterController>();
- climbingSystem = GetComponent<ClimbingSystem>();
- }
- void Update ()
- {
- Vector2 input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
- Vector2 inputDir = input.normalized;
- if (Input.GetButtonDown(sprintButton))
- {
- running = !running;
- }
- canClimb = climbingSystem.canClimb;
- canBrace = climbingSystem.canBrace;
- if (!canClimb)
- {
- Move(inputDir, running);
- gravity = -12;
- }
- else
- {
- Climb(inputDir);
- gravity = 0;
- }
- if (Input.GetButtonDown(jumpButton))
- {
- jumping = true;
- crouching = false;
- Jump();
- }
- if (Input.GetButtonDown(crouchButton) && crouching == false)
- {
- crouching = !crouching;
- Vector3 tVel = transform.position;
- transform.position = Vector3.SmoothDamp(transform.position, new Vector3(0, (0 - controller.height) * 0.5f, 0), ref tVel, 0.2f);
- running = false;
- }
- else if (Input.GetButtonDown(crouchButton) && crouching == true)
- {
- crouching = !crouching;
- Vector3 tVel = transform.position;
- transform.position = Vector3.SmoothDamp(transform.position, new Vector3(0, (controller.height), 0), ref tVel, 0.2f);
- }
- animationSpeedPercent = ((running) ? currentSpeed / runSpeed : currentSpeed / walkSpeed * .5f);
- if (animationSpeedPercent < 0.3f)
- {
- walkSpeed = startWalkSpeed;
- running = false;
- }
- animator.SetFloat("GrabbingX", input.x, speedSmoothTime, Time.deltaTime);
- animator.SetFloat("speedPercent", targetSpeed, speedSmoothTime, Time.deltaTime);
- animator.SetBool("Jumping", jumping);
- animator.SetBool("Crouching", crouching);
- animator.SetBool("Climbing", canClimb);
- animator.SetBool("CanBrace", canBrace);
- }
- private void Move(Vector2 inputDir, bool running)
- {
- if (inputDir != Vector2.zero)
- {
- float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraT.transform.eulerAngles.y;
- transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime));
- }
- if (!crouching)
- {
- controller.height = Mathf.Lerp(controller.height, normalCharacterHeight, heightSmoothTime * Time.deltaTime);
- //Gain speed with time
- if (walkSpeed < maxWalkSpeed)
- {
- walkSpeed += Time.deltaTime * acceleration;
- }
- else
- {
- walkSpeed = maxWalkSpeed;
- }
- acceleration = (running) ? maxAcceleration : startAcceleration;
- targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
- }
- else
- {
- controller.height = Mathf.Lerp(controller.height, crouchCharacterHeight, heightSmoothTime);
- targetSpeed = crouchSpeed * inputDir.magnitude;
- }
- currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));
- velocityY += Time.deltaTime * gravity;
- Vector3 velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
- controller.Move(velocity * Time.deltaTime);
- currentSpeed = new Vector2(controller.velocity.x, controller.velocity.z).magnitude;
- if (controller.isGrounded)
- {
- jumping = false;
- controller.height = Mathf.Lerp(controller.height, normalCharacterHeight, heightSmoothTime);
- velocityY = 0;
- }
- }
- private void Climb(Vector2 inputDir)
- {
- gravity = 0;
- currentSpeed = walkSpeed * 0.5f;
- Vector3 velocity = Vector3.zero;
- yOffset = (canBrace) ? bracedYOffset : hangingYOffset;
- Vector3 newPos = transform.position + Vector3.up * yOffset;
- transform.position = newPos;
- velocity = transform.right * currentSpeed * inputDir.x + Vector3.up * gravity;
- controller.Move(velocity * Time.deltaTime);
- if (Input.GetButtonDown(jumpButton))
- {
- velocity = transform.right * 10 * inputDir.normalized.x + Vector3.up * jumpHeight;
- controller.Move(velocity * Time.deltaTime);
- }
- }
- private void Jump()
- {
- if (controller.isGrounded)
- {
- float jumpVelocity = Mathf.Sqrt(-2 * gravity * jumpHeight);
- velocityY = jumpVelocity;
- controller.height = Mathf.Lerp(controller.height, jumpCharacterHeight, heightSmoothTime);
- }
- }
- private float GetModifiedSmoothTime(float smoothTime)
- {
- if (controller.isGrounded)
- {
- return smoothTime;
- }
- if (airControlPercent == 0)
- {
- return float.MaxValue;
- }
- return smoothTime / airControlPercent;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement