- Home /
GetKey have delay when relesed [SOLVED]
I have a problem Im making a 2 player shooter game on one keyboard everything is fine but when i press key like W to go forward it starts immediately but when i relese it it have like 0.5 sec delay. Here is my script:
public GameObject PlayerOne;
public GameObject PlayerTwo;
private bool groundedPlayerOne;
private bool groundedPlayerTwo;
public CharacterController P1controller;
public CharacterController P2controller;
public float P1speed = 2.0f;
public float P2speed = 2.0f;
private Vector3 P1Velocity;
private Vector3 P2Velocity;
public float P1jumpHeight = 1.0f;
public float P2jumpHeight = 1.0f;
private float gravityValue = -9;
private void Start()
{
P1controller = PlayerOne.AddComponent<CharacterController>();
P2controller = PlayerTwo.AddComponent<CharacterController>();
}
void Update()
{
groundedPlayerOne = P1controller.isGrounded;
if (groundedPlayerOne && P1Velocity.y < 0)
{
P1Velocity.y = 0f;
}
groundedPlayerTwo = P2controller.isGrounded;
if (groundedPlayerTwo && P2Velocity.y < 0)
{
P2Velocity.y = 0f;
}
Vector3 p1move = new Vector3(Input.GetAxis("p1_horizontal"), 0, Input.GetAxis("p1_vertical"));
P1controller.Move(p1move.normalized * Time.deltaTime * P1speed);
if (Input.GetKey("w") || Input.GetKey("a") || Input.GetKey("s") || Input.GetKey("d"))
{
p1move = new Vector3(Input.GetAxis("p1_horizontal"), 0, Input.GetAxis("p1_vertical"));
p1move = transform.TransformDirection(p1move);
p1move *= P1speed;
transform.rotation = Quaternion.LookRotation(p1move);
}
Vector3 p2move = new Vector3(Input.GetAxis("p2_horizontal"), 0, Input.GetAxis("p2_vertical"));
P2controller.Move(p2move.normalized * Time.deltaTime * P2speed);
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.RightArrow))
{
p2move = new Vector3(Input.GetAxis("p2_horizontal"), 0, Input.GetAxis("p2_vertical"));
p2move = transform.TransformDirection(p2move);
p2move *= P2speed;
transform.rotation = Quaternion.LookRotation(p2move);
}
if (Input.GetButtonDown("Jump") && groundedPlayerOne)
{
P1Velocity.y += Mathf.Sqrt(P1jumpHeight * -3.0f * gravityValue);
}
if (Input.GetButtonDown("Jump") && groundedPlayerTwo)
{
P2Velocity.y += Mathf.Sqrt(P2jumpHeight * -3.0f * gravityValue);
}
P1Velocity.y += gravityValue * Time.deltaTime;
P1controller.Move(P1Velocity * Time.deltaTime);
P2Velocity.y += gravityValue * Time.deltaTime;
P2controller.Move(P2Velocity * Time.deltaTime);
}
Answer by Eno-Khaon · Oct 30, 2021 at 06:13 PM
By default, the Input Manager's settings for Axis controls uses acceleration/smoothing both in and out of the keyboard keypresses (Sensitivity toward the keypress, Gravity back toward neutral). This means that it *IS* affecting when you're initially pressing the keys as well, but just not in the way you're expecting and comparing.
To note: This (basically) only applies to keyboard input. This is done to simulate gradually pressing or releasing the analog stick(s) on a controller. Similarly, mouse movement input is expected to be described in pixel quantities, so it also won't have smoothing applied.
Depending on the context in which your input is desired, you could just use GetAxisRaw() instead, to skip factoring in Sensitivity and Gravity and work only with the raw [-1, 0, 1] values.
Additionally/alternatively, you can also change how you're utilizing input to rotate your character(s), and base it on whether input's being provided:
//if(Input.GetKey("w") || Input.GetKey("a") || Input.GetKey("s") || Input.GetKey("d"))
if(p1move.sqrMagnitude > minimumRotationThreshold)
{
// You don't need to GetAxis() a second time here. You already have
// the variables assembled
//p1move = new Vector3(Input.GetAxis("p1_horizontal"), 0, Input.GetAxis("p1_vertical"));
// ...
}