- Home /
Shift BHOP in Character Controller
Hi, I want to make a character controller with SHIFT BHOPING.
If you don't know what shift bhoping is, it's a very simple concept but very difficult to code. Basically it's like normal bunny hopping (moving and jumping at the same time) but if the character is in the air, you can press shift to increase your speed a little. You can only do this once in a row in the air. You can also press shift a little time on the ground, but if you hold it too long, it will become a crouch.
I might not explain it enough, but there is one game that supports this kind of bhop: Krunker.
Here is a link that shows an example of shift bhoping (notice the 180 turns as well): https://www.youtube.com/watch?v=Yy6Rn-B32eo --- In the video, don't mind the gun or arm, I'm not implementing guns into my game.
This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public GameObject raycaster;
public float norSpeed = 12f;
[SerializeField] float speed;
public float gravity = -9.81f;
public float jumpHeight;
Vector3 velocity;
bool isGrounded = false;
public float groundDistance;
//Shift bhop variables
[SerializeField] float time = 0f;
public float timeMin = 0.7f;
public float crouchSpeed;
public float shiftAdd;
public float maxSpeed;
[SerializeField] bool airShift;
private void Awake() {
time = 0f;
airShift = false;
controller = GetComponent<CharacterController>();
}
private void Update() {
GroundRay();
ShiftBhop();
HorizontalMovement();
VerticalMovement();
}
void GroundRay() {
RaycastHit hit;
Ray downRay = new Ray(raycaster.transform.position, -Vector3.up);
if (Physics.Raycast(downRay, out hit)) {
if (hit.distance >= groundDistance) { isGrounded = false; }
else { isGrounded = true; }
}
}
void HorizontalMovement() {
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
}
void VerticalMovement() {
if (isGrounded && velocity.y < 0) { velocity.y = -0.5f; }
if (Input.GetButton("Jump") && isGrounded) {
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
airShift = true;
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
void ShiftBhop() {
if (isGrounded) {time += Time.deltaTime;}
else{time = 0f;}
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) {
if (time <= timeMin) {
if (airShift) {
speed += shiftAdd;
airShift = false;
}
}
else { speed = crouchSpeed; }
}
else { speed = norSpeed; }
speed = Mathf.Clamp(speed, 0f, maxSpeed);
}
}
I have a method at the bottom for shift bhoping, can you help me change it?
Here is my Inspector view:
Please don't do anything too complicated because I am only 9 years old.