I followed a Brackeys tutorial for First Person Movement (
) and it all works except the jumping function. The jumping function does not work with my current amount to change the velocity.y but when I change velocity.y = 50 it sends me flying into the air. When velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity) and I jump the only thing that happens is that the camera raises up 1 pixel and then returns to normal. Thanks in advance for any help!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
float speed = 12f;
float gravity = -19.62f;
float jumpHeight = 3f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
public Vector3 velocity;
public bool isGrounded;
// Start is called before the first frame update
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded)
{
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);
}
if(Input.GetKey(KeyCode.LeftShift))
{
speed = 24f;
}
else speed = 12f;
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}