rigidbody.velocity <-Why so difficult?

In my mind “rigidbody.velocity.y” should tell me the speed of an object moving up or down.

rigidbody.velocity.y = 0 would be the object sitting there not moving up or down and any other value would mean it’s moving.

But that’s NOT how it works! When my object is sitting there is has a rigidbody.velocity.y value of -7.43242E78J233 or something.

This makes it impossible for me to determine if the object is moving along the y axis or not.

It should not be this difficult, even game maker has a variable for checking the speed of an object based on its axis that returns 0 when there is no movement.

I guess velocity means how much force the object has, not the speed it’s moving? Am I supposed to check momentum also?

Here is my C# code:

if(NOT MOVING ON Y AXIS  Input.GetButtonDown("Jump")){
		rigidbody.AddForce(jumpVelocity, ForceMode.VelocityChange);
		touchingPlatform = false;
		}

Floating point imprecision. That’s basically 0. http://en.wikipedia.org/wiki/Scientific_notation#E_notation

Just check that it’s less than a very small value, not hard at all.

No.

–Eric

I checked if rigidbody.velocity.y < 0.00001 and I can still continuously air jump never coming back down to the ground.

also tried -0.01 etc

Another weird thing is if I check if rigidbody.velocity.y < -0.00001 the jump wont work at all, but if I add more 0s such as rigidbody.velocity.y < -0.0000001, then it can jump and the ability to jump off the air persist.

You could try Mathf.Approximately(0), if that’s really what you need to be doing. And if you want to test for magnitude, you need the absolute value of the number you’re testing. So, I think that something like (Mathf.Abs(rigidbody.velocity.y) < 0.0001f) or (Mathf.Approximately(rigidbody.velocity.y, 0)) might suit your needs better.

But, testing for equality with floating point numbers is almost always a bad idea. It’s not a game engine/tool thing, it’s a combination of the fact that you’re not working with discreet values and the way computers store floating point numbers (small, creeping inaccuracies).

If you tell us what you’re trying to do and how, someone might be able to suggest a more reliable approach that doesn’t involve floating point equality tests in the first place. I’d stringly suggest doing some kind of grounding test rather than using velocity. If you just test for 0 then won’t you be able to re-jump at the apex of your jumps anyway?

Well, I was trying to create a side-scroller movement system and I am not using the character controller as when I attempted to add it the character just started moving down through the floor instead of walking normally. So not using the character controller I haven’t been able to use the isGrounded check.

And yes the last point about checking for 0 would cause a double jump if that worked anyway, so I guess was even further from a solution than I thought.

Here’s my full script;

using UnityEngine;

public class Player_SideScroll_Move_Script : MonoBehaviour {
	 
	public static float distanceTraveled;
	
	public float acceleration;
	public Vector3 jumpVelocity;
	
	private bool touchingPlatform;
	
	void Update () {
		
		print(rigidbody.velocity.y);
		//if(touchingPlatform  Input.GetButtonDown("Jump")){
		if(Input.GetButtonDown("Jump")){
		rigidbody.AddForce(jumpVelocity, ForceMode.VelocityChange);
		touchingPlatform = false;
		}
		
		if(Input.GetKey("d")){
		transform.Translate(5f * Time.deltaTime, 0f, 0f);
		distanceTraveled = transform.localPosition.x;
		}
		
		if(Input.GetKey("a")){
		transform.Translate(-5f * Time.deltaTime, 0f, 0f);
		distanceTraveled = transform.localPosition.x;
		}
		
	}
	
	void FixedUpdate () {
		if(touchingPlatform){
			rigidbody.AddForce(acceleration, 0f, 0f, ForceMode.Acceleration);
		}
	}

	void OnCollisionEnter () {
		touchingPlatform = true;
	}

	void OnCollisionExit () {
		touchingPlatform = false;
	}
	
}

*EDIT: I removed the acceleration parts and now when landing on the ground I get a value of 0 for rigidbody.velocity.y, but when I move right or left the value changes to something like -7.43242E78J233 again, so I can’t run and jump. Also I noticed no double jumping.

One other thing, I don’t use the “OnCollisionEnter” to tell if I am grounded because it bugs out and prevents me from being able to jump if the character stutters against a wall or obstacle.

SOLVED (via workaround)

I simply went back to the condition for jumping “touchingPlatform”, then in order to fix the issue of it causing the player to not be able to jump after stuttering against something I added this line of code to the update event:

//Fix bug after stuttering.
if (touchingPlatform == false  rigidbody.velocity.y == 0){touchingPlatform = true;}