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);
}
Thank you soo much for your help it works perfectly
– Mevy