Hello everyone. I’m learning the ropes on C#, and while making a test game I’ve encountered a nice jump script to work with my idea, but the thing is: the jump is too smooth while going up and down. I’ve tried to fiddle with it by multiplying the _gravity by a float number instead of time.deltatime, but I was shocked to see that I plummeted to the ground like a rock.
Since I’m only using a charactercontroller, should I put a rigidbody component to apply the jumpforce on the y axis, instead of the raw multiplication, or there is anything I can do inside the original script to make it a sharp, consistent jump?
Here is the original script joined with mine (only the part in the jump method):
private CharacterController _controller;
[SerializeField]
private float _speed = 10f;
[SerializeField]
private float _jumpForce = 9f;
[SerializeField]
private float _gravity = 5f;
private Vector3 _jump = Vector3.zero;
// Start is called before the first frame update
void Start()
{
_controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
Movement();
Jump();
}
private void Movement()
{
float horizontalInput = Input.GetAxis("Horizontal");
Vector3 direction = new Vector3(horizontalInput, 0, 0);
Vector3 velocity = direction * _speed;
velocity.y -= _gravity;
_controller.Move(velocity * Time.deltaTime);
}
private void Jump()
{
if(_controller.isGrounded && Input.GetKeyDown(KeyCode.Space))
{
_jump.y = _jumpForce;
}
_jump.y -= _gravity * Time.deltaTime;
_controller.Move(_jump * Time.deltaTime);
}
I’ve found out that multiplying the _jump.y and the _controller.Move method by 2 /2.5f makes the jump “sharper”, but I’m open to suggestions. Also, is there any way to make the jump pressure sensitive? Like, depending on how long the space key is pressed, the shorter or higher the jump.
Thank you for your time.