so i tried implementing jump buffering using a tut on youtube (that uses old input and rigidbody) and got only coyote time working. tho the jump buffering doesnt work no matter whatever came on my mind to do.
previous threads that i found either are rigidbody or are older input plus i started coding like 2 weeks ago and still am getting things figured out so sorry for my possibly bad code
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
public bool canMove = true;
public CharacterController controller;
private Vector2 moveInput;
public float speed = 5f;
public Vector3 velocity;
public float gravity = -9.81f;
public float jumpHeight = 3f;
public Transform playerCamera;
public float mouseSensitivity = 100f;
private float xRotation = 0f;
private Vector2 lookInput;
public bool isSprinting = false;
private Camera playerCameraalt;
private float targetfov;
public float fovmin = 60f;
public float fovmax = 90f;
private float fovSpeed = 10f;
[SerializeField] private float coyoteTime = 0.2f;
private float coyoteTimer;
[SerializeField] private float JumpBufferTime = 0.2f;
private float jumpBufferTimer;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
Cursor.lockState = CursorLockMode.Locked;
playerCameraalt = playerCamera.GetComponent<Camera>();
targetfov = fovmin;
}
void OnMove(InputValue value)
{
moveInput = value.Get<Vector2>();
}
void OnJump(InputValue value)
{
if (value.isPressed)
{
jumpBufferTimer = JumpBufferTime;
}
}
void OnLook(InputValue value)
{
lookInput = value.Get<Vector2>();
}
void OnSprint(InputValue value)
{
isSprinting = value.isPressed;
if (isSprinting)
{
speed = 10f;
targetfov = fovmax;
}
else
{
speed = 5f;
targetfov = fovmin;
}
}
void Update()
{
if (!canMove)
{
return;
}
if (controller.isGrounded)
{
coyoteTimer = coyoteTime;
if (velocity.y < 0f)
{
velocity.y = -2f;
}
}
if (jumpBufferTimer > 0f && coyoteTimer > 0f)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
jumpBufferTimer = 0f;
coyoteTimer = 0f;
}
Vector3 move = transform.right * moveInput.x + transform.forward * moveInput.y;
controller.Move(move * speed * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
if (controller.isGrounded && jumpBufferTimer > 0f)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
jumpBufferTimer = 0f;
coyoteTimer = 0f;
}
if (jumpBufferTimer > 0f)
{
jumpBufferTimer -= Time.deltaTime;
}
if (coyoteTimer > 0f)
{
coyoteTimer -= Time.deltaTime;
}
float mouseX = lookInput.x * mouseSensitivity * Time.deltaTime;
float mouseY = lookInput.y * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
playerCamera.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
transform.Rotate(Vector3.up * mouseX);
float currentfov = playerCameraalt.fieldOfView;
playerCameraalt.fieldOfView = Mathf.Lerp(currentfov, targetfov, fovSpeed * Time.deltaTime);
}
}
