hello unity forms, this is my first post and i am new to unity
i have been trying to learn unity and i have done a little progress. i made a charecter controller with brackyes code on fps controller and i tried implimenting new functions to it like sprinting crouching etc however i tried to add sprinting with a fov effect (with GD,tweening) and eveything is fine that only thing is that i want it to only work when it its grounded , kind of like toggling to sprint, here is how i would like it to work:-
when that charecter is grounded and pressing w he can sprint and jump wile sprinting BUT
if that character is in mid air he cannot start sprinting.here are my codes
using UnityEngine;
public class player_movement_script : MonoBehaviour
{
public CharacterController controller;
public float speed = 10f;
public float SprintSpeed = 15f;
public float gravity = -30f;
public float jumpheight = 3f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
public mouse_control Cam;
public Vector3 velocity;
bool isGrounded;
// Update is called once per frame
void Update()
{
isGrounded = controller.isGrounded;
if (isGrounded && velocity.y < 0){
velocity.y = -2f;
}
float player_positionX = Input.GetAxis("Horizontal");
float player_positionY = Input.GetAxis("Vertical");
Vector3 move = transform.right * player_positionX + transform.forward * player_positionY;
//default
Cam.DoFOV(60);
if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W))
{
controller.Move(move * SprintSpeed * Time.deltaTime);
Cam.DoFOV(80);
}
else
{
controller.Move(move * speed * Time.deltaTime);
Cam.DoFOV(60);
}
if (!isGrounded){
velocity.y += gravity * Time.deltaTime;
}
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpheight * -2f * gravity);
}
controller.Move(velocity * Time.deltaTime);
}
}
note(fov funtion is referenced to a script in main camera)
please help me i am stuck and i serched everywhere for help