- Home /
Movement of increased certain speed type not registering.
So I created two kinds of increased speed, sprinting AND power sprint. sprinting is activated by holding down left shift (commonly) while the power sprint is activated by toggling it (F key).
The weird thing is, when holding down left shift the player movement speed is increased/updated from walkspeed of value to a new value/sprint speed in the inspector but the player doesn't actually run in that speed but still stays in the walking speed. Even though in the inspector, movement speed updated from walkspeed to sprint speed by holding it down so it updates all the time for each frame but in the actual gameplay it doesn't actually sprint to that speed and only stays the same for the walkspeed.
heres my player movement script (not the whole of it), the power sprint works fine though. Note: if you find it hard to understand I will explain again and will show all my script in the comments section.
public float sprintSpeed;
public float walkSpeed;
[SerializeField] private KeyCode sprintKey;
private void Start()
{
canSprint = true;
sprintSpeed = 11.8f;
walkSpeed = 4.0f;
}
private void Update()
{
CharMovement();
}
private void CharMovement()
{
if (canMove == true)
{
float horizInput = Input.GetAxis(horizontalInput) * movementSpeed;
float vertInput = Input.GetAxis(verticalInput) * movementSpeed;
Vector3 forwardMovement = transform.forward * vertInput;
Vector3 rightMovement = transform.right * horizInput;
charController.SimpleMove(forwardMovement + rightMovement);
JumpInput();
SprintInput();
SprintBurst();
CrouchInput();
}
}
private void SprintInput()
{
if (Input.GetKey(sprintKey) && canSprint == true)
{
Debug.Log("shift is being held");
canCrouch = false;
movementSpeed = sprintSpeed;
}
else if (Input.GetKeyUp(sprintKey))
{
canSprint = false;
canCrouch = true;
movementSpeed = walkSpeed;
}
}
I used your code to move a character controller and works fine for me. can we see SprintBurst() please.? may be there's an bug there. Also you are setting canSprint=false when you release sprintkey but i dont see where you set it true again. You can make SprintInput and SprintBurst a single function so one wont override the other one and you have full control of movementSpeed
private void SprintBurst()
{
if (Input.Get$$anonymous$$eyDown(power1$$anonymous$$ey) && canSprintBurst == true)
{
isSprinting = !isSprinting;
}
}
thanks for noticing that, after trying your advices however it didn't do anything. also if your wondering where the canSprint=false is set to true again is on this function CheckPlayer$$anonymous$$ovement(). Which checks the player's movements at all times. so the speed is not increased or sprint power will remain not active if the player stood still.
public void CheckPlayer$$anonymous$$ovement()
{
if (old_Pos != transform.position)
{
// do something now player DOES move
canSprint = true;
canSprintBurst = true;
}
else if (old_Pos == transform.position)
{
// do something now player DOESN'T move
canSprint = false;
canSprintBurst = false;
}
old_Pos = transform.position;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612195437im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Get Key(KeyCode)??? 1 Answer
Movement speed doesn't change with variable C# 1 Answer
When I jump then walk, my character suddenly starts walking mid-air... 1 Answer