Help with jumping script
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.
Answer by KittyAnn · Apr 12, 2019 at 06:35 PM
@JoeKalango Here is my jump script. It uses the space bar to jump. You have UNLIMITED jumps (can basically fly if you do an animation for it). You need a RigidBody on your "player" & just attach this script to your "player". You can set the force in the 2nd last line: rb.AddForce(new Vector3(0, 5, 0), ForceMode.Impulse); --- I have my force set to 5 (middle number controls the Y axis). Hope this helps! Here ya go:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerJumpScript : MonoBehaviour
{
Rigidbody rb;
public bool isJumping = false;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
isJumping = true;
rb.AddForce(new Vector3(0, 5, 0), ForceMode.Impulse);
GetComponent<Animator>().Play("isJumping");
}
}
}
Answer by JoeKalango · Apr 19, 2019 at 01:41 AM
@KittyAnn actually it kinda did, but the problem is: The bool isJumping never gets deactivated, so the bool is set to true forever, so the player just floats around endlessly...