Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class CharacterJump : MonoBehaviour
- {
- private CharacterController characterController;
- private float movementSpeed = 5.0f;
- private float horizontalInput;
- private Vector3 movement;
- // Jump
- private float jumpHeight = 0.5f;
- private float gravity = 0.045f;
- private bool jump = false;
- private void Start()
- {
- characterController = GetComponent<CharacterController>();
- }
- private void Update()
- {
- horizontalInput = Input.GetAxis("Horizontal");
- if (!jump && Input.GetButtonDown("Jump"))
- {
- jump = true;
- }
- }
- private void FixedUpdate()
- {
- movement.x = horizontalInput * movementSpeed * Time.deltaTime;
- // Check if characterController is not grounded
- // and add gravity
- if (!characterController.isGrounded)
- {
- if (movement.y > 0)
- {
- // As we go up, apply normal gravity
- movement.y -= gravity;
- }
- else
- {
- // As we go down, apply "faster" gravity
- movement.y -= gravity * 1.5f;
- }
- }
- else
- {
- movement.y = 0;
- }
- // Setting jumpHeight to movement.y
- if (jump)
- {
- movement.y = jumpHeight;
- jump = false;
- }
- // Check if characterController exists and
- // then move player according to input
- if (characterController)
- {
- characterController.Move(movement);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment