- Home /
[SOLVED] Player is jumping up too fast.
The few lines of code from Brackeys tutorial have relatively solved the jumping issue.
https://www.youtube.com/watch?v=_QajrabyTJc&list=PLXJxfWH3Df9nRh_0-MCNv13Xv78dkuMAK∈dex=15&t=1197s
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{
public Joystick joystick;
public float moveSpeed = 20f;
float rotSpeed = 90;
float rot = 0f;
public float gravity = -9.81f;
public float jumpForce = 10f;
public int currentProgress;
public ProgressBar ProgressBar;
Vector3 moveDir = Vector3.zero;
CharacterController controller;
Animator anim;
Rigidbody rb;
void Start()
{
controller = GetComponent<CharacterController> ();
anim = GetComponent<Animator> ();
rb = GetComponent<Rigidbody> ();
currentProgress = 0;
ProgressBar.SetProgress(0);
}
void Update()
{
//Important
if (controller.isGrounded && moveDir.y > 0)
{
moveDir.y = -2f;
}
if (controller.isGrounded && Input.GetButton("Jump"))
{
moveDir.y = Mathf.Sqrt(jumpForce * -2f * gravity);
}
moveDir.y += gravity * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime);
}
// Update is called once per frame
void FixedUpdate()
{
//joystick controls//
if (joystick.Vertical == 0f)
{
anim.SetBool("isRunning", false);
moveDir = new Vector3(0, 0, 0);
}
else if (joystick.Vertical >= .5f)
{
anim.SetBool("isRunning", true);
moveDir = new Vector3(0, 0, 1);
moveDir *= moveSpeed;
moveDir = transform.TransformDirection(moveDir);
}
//rotation using joystick//
rot += joystick.Horizontal * rotSpeed * Time.deltaTime;
transform.eulerAngles = new Vector3(0, rot, 0);
moveDir.y += gravity * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime);
}
//pick up objects with sound and progress bar//
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pickup"))
{
other.gameObject.SetActive(false);
currentProgress += 1;
ProgressBar.SetProgress(currentProgress);
SoundManager.sndMan.PlayCollectSound();
}
}
public void jumpButton()
{
if (controller.isGrounded)
{
moveDir.y = Mathf.Sqrt(jumpForce * -2f * gravity);
}
moveDir.y += gravity * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime);
}
}
Answer by GreatOscarF · May 09, 2020 at 07:41 AM
@conqueror1 This should work:
public float jumpForce = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDir = Vector3.zero;
void Update() {
if (controller.isGrounded && Input.GetButton("Jump")) {
moveDir.y = jumpForce;
}
moveDir.y -= gravity * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime);
}
And in your whole script I would highly recommend tweaking your gravity and jumpForce a bit more. As a standard I would try 8.0F for the jumpForce and 20.0F for the gravity and then go from there. Also the gravity should no longer be a negative number.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{
public Joystick joystick;
public float moveSpeed = 20f;
float rotSpeed = 90;
float rot = 0f;
public float gravity = -9.81f; // This should NOT be a negative number... I would recommend going for something around 20f and then tweak it - a lot.
public float jumpForce = 10f; // This seems good but maybe you should tweak it a little bit more - for perfection :)
public int currentProgress;
public ProgressBar ProgressBar;
Vector3 moveDir = Vector3.zero;
CharacterController controller;
Animator anim;
Rigidbody rb;
void Start()
{
controller = GetComponent<CharacterController> ();
anim = GetComponent<Animator> ();
rb = GetComponent<Rigidbody> ();
currentProgress = 0;
ProgressBar.SetProgress(0);
}
// Update is called once per frame
void FixedUpdate()
{
//joystick controls//
if (joystick.Vertical == 0f)
{
anim.SetBool("isRunning", false);
moveDir = new Vector3(0, 0, 0);
}
else if (joystick.Vertical >= .5f)
{
anim.SetBool("isRunning", true);
moveDir = new Vector3(0, 0, 1);
moveDir *= moveSpeed;
moveDir = transform.TransformDirection(moveDir);
}
//rotation using joystick//
rot += joystick.Horizontal * rotSpeed * Time.deltaTime;
transform.eulerAngles = new Vector3(0, rot, 0);
moveDir.y += gravity * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime);
}
//pick up objects with sound and progress bar//
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pickup"))
{
other.gameObject.SetActive(false);
currentProgress += 1;
ProgressBar.SetProgress(currentProgress);
SoundManager.sndMan.PlayCollectSound();
}
}
public void jumpButton()
{
if (controller.isGrounded)
{
moveDir.y = jumpForce;
}
moveDir.y -= gravity * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime);
}
}
Thank you for pointing me in the right direction, some of the suggestions worked combined with Brackey's tutorial :)
Your answer
Follow this Question
Related Questions
Camera follow player's rotation 3 Answers
Player movement problem 0 Answers
Virtual joystick problem issue 1 Answer
How to prevent player from moving in air while jumping in place?? 0 Answers
How do I make my character jump? 1 Answer