Reputation: 11
I am very new to c#, unity and coding in general. I am trying to make a controllable 3d character that can crouch, jump and sprint. Sprinting currently does not work because of what i described in the title: the player's speed is updated in the unity inspector but does not actually change in-game.
full player movement script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public float gravity = -9.807f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
public float jumpHeight = 3f;
public float crouchHeight = 0.9f;
public float playerPushPower = 2.0f;
public float speed = 16f;
public float sprintSpeed = 1.5f;
public float crouchSpeed = 0.5f;
private float baseSpeed = 16f;
bool isGrounded;
bool isSprinting = false;
bool isCrouching = false;
bool isJumping = false;
bool isStanding = true;
Vector3 velocity;
void OnControllerColliderHit(ControllerColliderHit hit)
{
Rigidbody body = hit.collider.attachedRigidbody;
playerPushPower = speed / 8;
// no rigidbody
if (body == null || body.isKinematic)
return;
// We dont want to push objects below us
if (hit.moveDirection.y < -0.3f)
return;
// Calculate push direction from move direction,
// we only push objects to the sides never up and down
Vector3 pushDir = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z);
// If you know how fast your character is trying to move,
// then you can also multiply the push velocity by that.
// Apply the push
body.velocity = pushDir * playerPushPower;
}
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
controller.height = 3.8f;
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
if(Input.GetKey("left ctrl") && isGrounded)
{
speed = speed * crouchSpeed;
isCrouching = true;
isStanding = false;
}
else
{
speed = 16f;
isCrouching = false;
isStanding = true;
}
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.GetButtonDown("Jump") && isGrounded && isStanding)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
isJumping = true;
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
if(Input.GetKey("left shift") && isGrounded && isStanding)
{
speed = speed * sprintSpeed;
isSprinting = true;
}
else
{
speed = 16f;
isSprinting = false;
}
}
}
Upvotes: 0
Views: 222
Reputation: 7590
See how the speed is calculate and apply to move the character :
void Update()
{
...
// 1) Reset the speed to 16 if left ctrl isn't pressed
if(Input.GetKey("left ctrl") && isGrounded)
{
speed = speed * crouchSpeed;
isCrouching = true;
isStanding = false;
}
else
{
speed = 16f;
isCrouching = false;
isStanding = true;
}
...
// 2) Move the character according to speed
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
...
// 3) Apply the sprint factor to speed
if(Input.GetKey("left shift") && isGrounded && isStanding)
{
speed = speed * sprintSpeed;
isSprinting = true;
}
else
{
speed = 16f;
isSprinting = false;
}
}
As you see the sprint factor is ignored, because the character is moved before the sprint factor is applied. The solution is to apply the sprint factor before moving the character.
void Update()
{
...
// 1) Reset the speed to 16 if left ctrl isn't pressed
if(Input.GetKey("left ctrl") && isGrounded)
{
speed = speed * crouchSpeed;
isCrouching = true;
isStanding = false;
}
else
{
speed = 16f;
isCrouching = false;
isStanding = true;
}
...
// 2) Apply the sprint factor to speed
if(Input.GetKey("left shift") && isGrounded && isStanding)
{
speed = speed * sprintSpeed;
isSprinting = true;
}
else
{
speed = 16f;
isSprinting = false;
}
...
// 2) Move the character according to speed (included the sprint factor)
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
...
}
Bonus : the speed isn't reinitialised when the key left ctrl is pressed. Then speed decrease each frame. I don't think it's expected.
Upvotes: 2