hey, sorry for the silly question, im new to C# and Unity.
basically im trying to make a sprint button but the way im doing it requires me to change a float variable, the code so far is:
public float movementSpeed = 5.0f;
// Movement
if(Input.GetButtonDown("Sprint")){
float movementSpeed = 7.7f;
}
float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
float sideSpeed = Input.GetAxis("Horizontal") * movementSpeed;
that comes up with the error: ‘movementSpeed’ conflicts with a declaration in a child block.
anybody have an easy fix for this? thanks in advance
Kray-C
2
Just remove the second float because you already declared your variable above the if statement.
public float movementSpeed = 5.0f;
// Movement
if(Input.GetButtonDown("Sprint")){
movementSpeed = 7.7f;
}
float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
float sideSpeed = Input.GetAxis("Horizontal") * movementSpeed;
thanks for the reply, that got rid of the error but didnt actually increase my players speed. but i did end up finding a solution:
// Movement
if(Input.GetButtonDown("Sprint")){
movementSpeed += 3.5f;
}
if(Input.GetButtonUp("Sprint")){
movementSpeed = 4.5f;
}
float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
float sideSpeed = Input.GetAxis("Horizontal") * movementSpeed;