- Home /
Button Press/Release delay on custom FPS character
Hi,
I am currently busy doing a Unity FPS tuturial: https://www.youtube.com/playlist?list=PL7AE076AFAFD3C305
My problem is that when I walk and release a movement button there is still a bit of a delay before it stops. And when I press a movement button it has the same delay before it starts moving.
You can see the script (Javascript) I'm using below. Can someone maybe help me to figure out how to shorten this delay or even take it out completely?
SCRIPT:
var walkAcceleration : float = 5; var walkAccelAirRacio : float = 0.1; var walkDeacceleration : float = 5;
var walkDeaccelerationVolx : float; var walkDeaccelerationVolz : float;
var cameraObject : GameObject; var maxWalkSpeed : float = 20; var horizontalMovement : Vector2; var jumpVelocity : float = 20; var grounded : boolean = false; var maxSlope : float = 60;
function Update () { horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z); if (horizontalMovement.magnitude > maxWalkSpeed) { horizontalMovement = horizontalMovement.normalized; horizontalMovement *= maxWalkSpeed; }
rigidbody.velocity.x = horizontalMovement.x; rigidbody.velocity.z = horizontalMovement.y;
if (Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0 && grounded){
rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkDeaccelerationVolx, walkDeacceleration);
rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkDeaccelerationVolz, walkDeacceleration);
}
transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);
if (grounded)
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);
else
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRacio * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRacio * Time.deltaTime);
if (Input.GetButtonDown("Jump") && grounded)
rigidbody.AddForce(0,jumpVelocity,0);
}
function OnCollisionStay (collision : Collision) { for (var contact : ContactPoint in collision.contacts) { if (Vector3.Angle(contact.normal, Vector3.up) < maxSlope) grounded = true; } }
function OnCollisionExit () { grounded = false; }
Your answer
Follow this Question
Related Questions
Problem with while statement. 1 Answer
How can I add a delay between moving objects ? 1 Answer
Movement in cycles? 0 Answers
Gun Animations Aren't Playing Right Away? 2 Answers
How can I make an object follow another's path with some time delay? 2 Answers