2D Rigidbody Velocity Being Weird
I created a 2D Player movement script and wanted to make it so when velocity on the Y-axis is approximately 0, then the player would be allowed to jump. The script works and the 'DebugVelocity' variable is just there so I could test what the Y velocity was. I then noticed that the character would have problems jumping and that the Y velocity ('DebugVelocity') was randomly changing from -4.5 to 2 and everything in between despite no forces acting on the player in the Y axis. This meant that the player could not jump since the Y velocity was no close to 0. I have checked and the collider of my player and the floor a straight so I'm not sure why this is happening.
The Player Movement Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float speed = 5f;
public int jumpSpeed = 3;
private bool CanJump = true;
public float DebugVelocity;
private Rigidbody2D PlayerRigidbody;
private void Start()
{
PlayerRigidbody = GetComponent<Rigidbody2D>();
}
void Update () {
DebugVelocity = PlayerRigidbody.velocity.y;
//Move Character
if (Input.GetKey(KeyCode.D))
{
gameObject.transform.Translate(speed * Time.deltaTime, 0, 0);
}
if (Input.GetKey(KeyCode.A))
{
gameObject.transform.Translate(-speed * Time.deltaTime, 0, 0);
}
//Detect if player is on floor
if (Mathf.Approximately(PlayerRigidbody.velocity.y, 0))
{
CanJump = true;
}
else
{
CanJump = false;
}
//Jump
if (Input.GetKeyDown(KeyCode.Space) && CanJump == true)
{
PlayerRigidbody.AddForce(transform.up * jumpSpeed * 100);
}
}
}
It's worth noting that $$anonymous$$athf.Approximately is a comparison of that value against floating point epsilon. I assume you want to check only that vertical movement is "small", but is epsilon the "smallness" that you want?
For your speed, Rigidbody velocity might be influenced by your floor's collider trying to push the character out of its bounds and your character wanted to fall into them. Velocity in general can be less than predictable, so that might not be the route you want to take.
This is usually done by just checking if some Trigger-Only hitbox on your player's feet is colliding with the floor.
Answer by scottsniper77 · Sep 20, 2018 at 04:03 PM
@TreyH Was correct but I can't make him the answerer since it was a comment. Thanks man.
TreyH · 18 hours ago 1 It's worth noting that Mathf. Approximately is a comparison of that value against floating point epsilon. I assume you want to check only that vertical movement is "small", but is epsilon the "smallness" that you want?
For your speed, Rigidbody velocity might be influenced by your floor's collider trying to push the character out of its bounds and your character wanted to fall into them. Velocity, in general, can be less than predictable, so that might not be the route you want to take.
This is usually done by just checking if some Trigger-Only hitbox on your player's feet is colliding with the floor.
Yeah next time I'll just try use collision ins$$anonymous$$d of detecting the Velocity!