I should say that I have very very little C# and Unity experience. I got most of this code from a Brackeys tutorial. So, this code originally just made the player move at a set speed. So, I added a new public float set to a higher speed and set an if statement to change the value of the float that controls the speed (the float is literally named speed). I added a Debug.Log to display if the if statement is triggered/what the value of the speed float is. This log entry always displays once the Lshift key is pressed (default control axes have Fire3 set to Lshift inside of the unity project settings). I think my issue is that my speed is somehow decreasing before the player even moves. Additionally, this is probably the worst way to implement a sprint function, so if anyone has any better ideas, please let me know!
Clarification: The player can move. My intended effect is for the sprint key (Lshift) to be held down to achieve the sprinting effect. I did intend for the speed to reset every frame, so that as soon as the Lshift key is let go, the speed of the player will drop down to the normal state (12f). Currently I have a Debug.Log in place that displays the value of the "speed" float whenever the Lshift key is pressed. This always returns 18 in the console, which is the intended effect. However, even though this number is displayed in the console, there is no actual movement speed change in the player.
public CharacterController controller;
public float speed = 16f;
public float gravity = -9.81f;
public float jumpHeight = 3f;
public float sprintspeed = 18f;
public Transform groundCheck; //this is where the groundcheck empty object goes
public float groundDistance = 0.4f;
public LayerMask groundMask; //make sure to add a layer into the unity project called "ground"
Vector3 velocity;
bool isGrounded;
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask); //this just creates a sphere and checks it's collision state with any other objects
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f; //sets velocity to a low static number.
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
speed = 12f;
if(Input.GetButtonDown("Fire3")) //theoretically increases the speed float only when left control is pressed down
{
speed = sprintspeed;
Debug.Log(speed);
}
Vector3 move = transform.right * x transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
if(Input.GetButtonDown("Jump") && isGrounded)//jump function below
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y = gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
Thanks in advance!
CodePudding user response:
I would look to see what values are in the inspector since both speed and sprintspeed are public. There is a chance that sprintspeed might be set to 12f and therefore not change the speed. Also, keep in mind that with your implementation you can't change the speed variable from the inspector since you're updating it back to 12f every frame.
If that doesn't help with anything, perhaps some clarification is needed. Is your player moving at all? Is it just the sprinting that isn't working? What value does your Debug.Log give you? What are the values in the inspector?
CodePudding user response:
Can you first check if manually changing the default speed to a slower or faster value in the inspector affects the character's perceived speed?