I followed a basic movement and first person-camera video by Brackeys. I’ve gotten here so far, and I want to be able to make a script that speeds up the player when they hold shift. I’ve already got some other scripts that I need to use.
Here’s the “code” that I attempted to make myself based on the script I followed.
public class Sprint : MonoBehaviour
{
public CharacterController controller;
//public float speed;
public float sprintZoom = 5f;
public float sprintSpeed = 1.3f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
void Update()
{
// isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
// if (Input.GetButtonDown("Jump") && isGrounded)
{
// controller.Move(speed * sprintSpeed);
}
}
}
I turned the parts that I wasn’t very sure on into comments.
Here’s the script that Brackeys made:
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 12f;
public float gravity = -9.81f;
public float jumpHeight = 3f;
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;
}
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)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
Once again, I installed Unity just yesterday, so please try to keep it easy! Thank you!