Varible jump heights
In my 2D game, I'm trying to add variable jump heights but I'm having trouble how to achieve this effect.
Here is my script:
using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour
{
private CharacterController controller;
private float verticalVelocity;
private float movement = 1.0f;
public bool runControl = false;
public float gravity = 60.0f;
public float speed = 30.0f;
public float jumpForce = 32.0f;
public float shortJumpForce = 10.0f;
public float soundVolume = 0.75f;
private AudioSource source;
public AudioClip jump;
public static bool moving = true;
void Start()
{
source = GetComponent<AudioSource>();
controller = GetComponent<CharacterController> ();
}
void Update()
{
if (controller.isGrounded)
{
verticalVelocity = -gravity * Time.deltaTime;
if (Input.GetMouseButtonDown (0))
{
source.PlayOneShot (jump, soundVolume);
verticalVelocity = jumpForce;
}
}
else
{
verticalVelocity -= gravity * Time.deltaTime;
}
if (moving)
movement = 1;
else
movement = 0;
Vector2 moveVector = new Vector2(0,verticalVelocity);
if (runControl)
{
moveVector.x = movement * speed;
}
controller.Move(moveVector * Time.deltaTime);
}
}
Thank you for the help.
Do you want to make the character to be able to change the force of the jump? You can achieve this by changing the 'jumpForce' variable.
Could you describe your problem?
Yes, we will need some more information before correctly answering... Are you trying to make it that the player jumps the base amount (`jumpForce') plus a random amount to add variation?
You know in most platformers how long you press the jump button and when you release it affects the height of the jump (example: $$anonymous$$ario), I've been trying to do this and I need some help on how.
Ah, I understand. Hang on for a $$anonymous$$ute.
Answer by NinjaISV · Jan 10, 2017 at 09:31 PM
All you need to do is check if the player is holding down the jump input and make the player jump during that time with a cap on it; here is a fully commented example:
using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour {
private CharacterController controller;
private float verticalVelocity;
private float movement = 1.0f;
public bool runControl = false;
public float gravity = 60.0f;
public float speed = 30.0f;
//This variable is for how high/powerful the jump is.
public float jumpForce = 10.0f;
//This variable is for how long the player can hold down jump for.
public float maxJumpHeight = 0.5f;
public float soundVolume = 0.75f;
private float jumpInputTime = 0f;
private AudioSource source;
public AudioClip jump;
public static bool moving = true;
void Start()
{
source = GetComponent<AudioSource>();
controller = GetComponent<CharacterController> ();
}
void Update() {
//Checks to see if the player is holding down the mouse button and that the player is on the ground or already jumping.
if ((controller.isGrounded == true || jumpInputTime > 0f) && Input.GetMouseButton(0) && jumpInputTime < maxJumpHeight) {
//Increases the time that the player is holding down the mouse input.
jumpInputTime += Time.deltaTime;
}
//Resets the jump input time if the player is not holding down the mouse button.
else
jumpInputTime = 0f;
//Ensures that the player is jumping.
if (jumpInputTime > 0f) {
source.PlayOneShot (jump, soundVolume);
verticalVelocity += jumpForce;
}
else {
verticalVelocity -= gravity * Time.deltaTime;
}
if (moving)
movement = 1;
else
movement = 0;
Vector2 moveVector = new Vector2(0,verticalVelocity);
if (runControl)
{
moveVector.x = movement * speed;
}
controller.Move(moveVector * Time.deltaTime);
}
}
To be honest, the first time I added the code to my player, it shot up like a rocket ship when I clicked to jump and the sound it made with the height it was made me laugh a lot.
I tried fiddling round with the script to stop this from happening when I realized that with the sensitivity of the mouse button and due to my game being a running platformer set to music, I decided that it was best to leave out variable jump heights as a whole for game design reasons.
I'm really sorry for wasting your time and thanks for $$anonymous$$ching me that some mechanics and systems aren't always necessary for a certain genre. At least you have your new insanely hilarious rocket ship script (good effort, by the way).
Haha, thanks. I didn't get a chance to tweak those values as I didn't have Unity nearby. If you set the jumpForce way down, that should help with the rocket issue. Like I'd set it to something like 0.1f or even lower, just try it.
In fact the 'jumpForce' was probably still at 32 as that's what you had originally had it at on your script.
Answer by SBrooks75 · Feb 09, 2017 at 03:20 PM
I have created a Platformer game as well and I utilized a GUI Button for jumping and added this script to handle the Input. This is a simple version of the script Im running. I can send you that as well if you like. It pretty straight forward. When the button is pressedDown it jumps and if it is held it will jump higher depending on what you set the jumpTime to. And if you only Quick Click the button it will only jump to the set height. If I need to better explain this and how to implement please let me know. This was as close as I could get to a Mario like jump.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;
public class UIButtonPress : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler
{
public static UIButtonPress instance;
public PlayerScript thePlayer;
public float jumpForce;
public float jumpTime;
public float jumpTimeCounter;
[HideInInspector]
public bool Pressed = false;
public Rigidbody2D playerRigidbody;
public UnityEvent onPressed;
public bool isJumping;
public void OnPointerDown(PointerEventData eventData)
{
Pressed = true;
}
public void OnPointerEnter(PointerEventData eventData)
{
Pressed = true;
}
public void OnPointerExit(PointerEventData eventData)
{
Pressed = false;
}
public void OnPointerUp(PointerEventData eventData)
{
Pressed = false;
}
void Start(){
playerRigidbody = FindObjectOfType<Rigidbody2D>();
jumpTimeCounter = jumpTime;
}
void FixedUpdate()
{
if (Pressed)
{
// onPressed.Invoke();
playerRigidbody.velocity = new Vector2 (playerRigidbody.velocity.x, jumpForce);
thePlayer.stoppedJumping = false;
isJumping = true;
}
if(Pressed)
{
//and your counter hasn't reached zero...
if(jumpTimeCounter > 0)
{
//keep jumping!
playerRigidbody.velocity = new Vector2 (playerRigidbody.velocity.x, jumpForce);
jumpTimeCounter -= Time.deltaTime;
}
}
if(!Pressed){
jumpTimeCounter = 0;
isJumping = false;
}
}
void CreateInstance(){
if(instance == null){
instance = this;
}
}
}
Your answer
Follow this Question
Related Questions
[2D Platform Game] Precise Jumping Without Holding Down Directional Button. 0 Answers
2D Character controller jump code is broken. any help would be appreciated 0 Answers
How do I disable Jumping while on my JumpPad? 1 Answer
Jump from the inclined surfaces 0 Answers
2D Side Scroller Help. I have a picture to show you! 0 Answers