- Home /
Question by
AvienJ · Mar 06, 2021 at 10:13 PM ·
scripting problemrigidbodyvelocityjumping
Unity autojump script glitch
So I'm trying to enable autojumping in my jumping script, but when I change GetButtonDown to GetButton, whenever I jump sometimes it launches me high and sometimes low. Can someone tell me what is going on?
Code: using UnityEngine;
public class Jump : MonoBehaviour
{
[SerializeField]
GroundCheck groundCheck;
Rigidbody rigidbody;
public float jumpStrength = 2;
public event System.Action Jumped;
void Reset()
{
groundCheck = GetComponentInChildren<GroundCheck>();
if (!groundCheck)
groundCheck = GroundCheck.Create(transform);
}
void Awake()
{
rigidbody = GetComponent<Rigidbody>();
}
void LateUpdate()
{
if (Input.GetButton("Jump") && groundCheck.isGrounded)
{
rigidbody.AddForce(Vector3.up * 100 * jumpStrength);
Jumped?.Invoke();
}
}
}
Thanks in Advance!
Comment
Answer by UnityM0nk3y · Mar 07, 2021 at 12:54 PM
Never used this system for Jumping, but I believe it might help if you move your "Jump Code" to FixedUpdate:
void FixedUpdate()
{
if (Input.GetButton("Jump") && groundCheck.isGrounded)
{
rigidbody.AddForce(Vector3.up * 100 * jumpStrength);
Jumped?.Invoke();
}
}
Your answer
Follow this Question
Related Questions
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
Rigidbody flies up instead of just jumping. 1 Answer
Space Shooter tutorial shot velocity issues 0 Answers
When I try to save the velocity of a rigidbody and use it later it doesn't work? 1 Answer
Rigidbody.velocity giving weird result 0 Answers