Sorry for my bad english!
I’m new to unity and coding and recently made a fps movement script. I really liked how it turned out but I have a problem that I can’t solve. My mid air movement is too snappy. When im in the air my movement is too snappy. It instantly changed direction in the air but I want it to be like minecraft mid air movement, not snappy but you still have some control in the air.
here is my code (It’s messy sorry):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class PlayerMovement : MonoBehaviour
{
//Playercontrollerthings
public CharacterController controller;
private Rigidbody rb;
//moving
public float SpeedMove = 12f;
float x, z;
Vector3 move;
//gravity
Vector3 velocity;
public float gravity = -9.81f;
//Checkifimonground
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
bool isGrounded;
//jump
public float jumpHeight = 1f;
//Crouching
private Vector3 crouchScale = new Vector3(1, 0.7f, 1);
private Vector3 playerScale = new Vector3(0.8f, 1.24f, 0.8f);
private float crouchy = 1f;
private bool readyToJump = true;
void Awake()
{
rb = GetComponent<Rigidbody>();
}
//keeps updating the voids I guess
void Update()
{
GetInput();
DecideWhatDirection();
Movement();
Gravity();
CheckIfGrounded();
VelocityFix();
Jump();
}
//Checks my input
void GetInput()
{
x = Input.GetAxis("Horizontal");
z = Input.GetAxis("Vertical");
if (Input.GetKeyDown(KeyCode.LeftControl))
StartCrouch();
if (Input.GetKeyUp(KeyCode.LeftControl))
StopCrouch();
}
//Starts crouching
void StartCrouch()
{
transform.localScale = crouchScale;
crouchy = 0.2f;
readyToJump = false;
}
//Stops crouching
void StopCrouch()
{
transform.localScale = playerScale;
crouchy = 1f;
readyToJump = true;
}
//Decides what way im going
void DecideWhatDirection()
{
move = transform.right * x + transform.forward * z;
}
//moves the playercontroller
void Movement()
{
if (isGrounded)
{
controller.Move(move * SpeedMove * Time.deltaTime * crouchy);
}
if (!isGrounded)
{
controller.Move(move * SpeedMove * Time.deltaTime * 0.5f);
}
}
//gravity
void Gravity()
{
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
//checks if im on the ground or not
void CheckIfGrounded()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
}
//Fixes velocity falling
void VelocityFix()
{
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
}
//jump
void Jump()
{
if (Input.GetButtonDown("Jump") && isGrounded && readyToJump)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
}
}
}