Hi. I have been making a game which is a parkour game.
I added a button that increases character’s speed.
If it is hold enough, it increases character’s speed more.
But that mechanic came with an issue.
If I hold that speed button with jumping in controller, jumping works as usual.
Same things I did in keyboard, but jumping didn’t work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float Speed;
public float rotationSpeed;
public float jumpSpeed;
public float jumpButtonGP;
private Animator animator;
private CharacterController charController;
private float ySpeed;
private float ogStepOffset;
private float? lastGT;
private float? jumpButtonPT;
private float startTimeSpeed = 0f;
private float holdTimeSpeed = 3.0f;
void Start()
{
animator = GetComponent<Animator>();
charController = GetComponent<CharacterController>();
ogStepOffset = charController.stepOffset;
}
void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
Vector3 moveDir = new Vector3(horizontalInput, 0, verticalInput);
float magnitude = Mathf.Clamp01(moveDir.magnitude) * Speed;
moveDir.Normalize();
ySpeed += Physics.gravity.y * Time.deltaTime;
if (charController.isGrounded)
{
lastGT = Time.time;
}
if (Input.GetButton("Dash"))
{
animator.SetBool("isRunning", true);
if (Input.GetButtonDown("Dash"))
{
Speed = 6;
startTimeSpeed = Time.time;
}
if (startTimeSpeed + holdTimeSpeed <= Time.time)
{
animator.SetBool("isDashing", true);
Speed = 9;
}
}
else
{
animator.SetBool("isRunning", false);
animator.SetBool("isDashing", false);
Speed = 3;
}
if (Input.GetButton("Jump"))
{
jumpButtonPT = Time.time;
}
if (Time.time - lastGT <= jumpButtonGP)
{
charController.stepOffset = ogStepOffset;
ySpeed = -0.5f;
if (Time.time - jumpButtonPT <= jumpButtonGP)
{
ySpeed = jumpSpeed;
jumpButtonPT = null;
lastGT = null;
}
}
else
{
charController.stepOffset = 0;
}
Vector3 velocity = moveDir * magnitude;
velocity.y = ySpeed;
charController.Move(velocity * Time.deltaTime);
if (moveDir != Vector3.zero)
{
animator.SetBool("isWalking", true);
Quaternion toRotation = Quaternion.LookRotation(moveDir, Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
}
else
{
animator.SetBool("isWalking", false);
animator.SetBool("isRunning", false);
animator.SetBool("isDashing", false);
}
}
}
[ICODE][/ICODE]