Smooth speed increase while sprinting
Hi I am trying to do a smooth speed increase while using sprint button, but can't figure out how should this work. I have tried to Mathf.SmoothDamp function but had a lot of trouble with that. public CharacterController controller;
public float speed = 3f;
public float gravity = -19.62f;
public float JumpHeight = 1.5f;
public float sprintMultiplier = 3f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
void Start()
{
}
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
if (Input.GetButton("Sprint") && isGrounded)
controller.Move(move * speed * sprintMultiplier * Time.deltaTime);
else
controller.Move(move * speed * Time.deltaTime);
Debug.Log(Input.GetButton("Sprint"));
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(JumpHeight * -2 * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
Comment