- Home /
Question by
JP313 · Apr 22 at 01:56 PM ·
movementfps controllersprinting
How to make sprinting
using UnityEngine;
public class Player_Movement : MonoBehaviour { public CharacterController controller;
public float speed = 12f;
public float sprit = 24f;
public float gravity = -9.81f;
public float jumpHeight = 3f;
public int maxJumpCount = 2;
public int jumpsRemaining = 0;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
jumpsRemaining = maxJumpCount;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
if(Input.GetKeyDown(KeyCode.RightControl))
{
controller.Move(move *sprit * Time.deltaTime);
}
if((Input.GetButtonDown("Jump")) && (jumpsRemaining > 0))
{
velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
jumpsRemaining -= 1;
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
This is a fps movement script the sprinting will not work
Comment
Answer by Caeser_21 · Apr 22 at 02:17 PM
1.Make sure you are pressing the right button... You can do this by adding a Debug statement in the script like :
if(Input.GetKeyDown(KeyCode.RightControl))
{
Debug.Log("Button has been pressed")
controller.Move(move *sprit * Time.deltaTime);
}
Or maybe try just changing the speed
variable in the script like :
if(Input.GetKeyDown(KeyCode.RightControl))
{
speed = sprit;
}
else if(Input.GetKeyUp(KeyCode.RightControl))
{
speed = 12f;
}
Code Not Tested