Why does my character sometimes jump higher than other times?
When I use this code to let my character jump, it sometimes jump higher than other times. I am making a 2D platformer using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour {
private float Speed = 15f;
private float JumpForce = 15000f;
public Rigidbody rb;
public Transform groundedEnd;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
Move();
Raycasting();
}
void FixedUpdate()
{
JumpKey();
}
void Move()
{
float Move = Input.GetAxis("Horizontal") * Speed;
Move *= Time.deltaTime;
transform.Translate(Move, 0, 0);
}
void JumpKey()
{
float JumpHeight = Input.GetAxisRaw("Jump") * JumpForce;
if (Input.GetKeyDown("space"))
{
rb.AddForce(transform.up * JumpHeight * Time.deltaTime);
}
}
void Raycasting()
{
Debug.DrawLine(this.transform.position, groundedEnd.position, Color.green);
}
}
Answer by Nighfox · Feb 02, 2018 at 01:57 PM
Try to change Time.deltaTime
to Time.fixedDeltaTime
in your rb.AddForce
. It may be that the value irregularly oscillates depending on your frame rate which causes fluctuations in actual jump force.
What are you using Input.GetAxis("Jump")
for? You are already using Get$$anonymous$$eyDown
, so you can try removing it and see if that works.
For some reason when I try that I can't jump anymore
thanks for the help. I fixed it with adding a raycast for isgrounded. ;-)