- Home /
rigidbody2D.velocity returns zero values
Hello,
I'm trying to make a 2D-platformer using the 2D tools in unity 4.3.4. I have successfully created a player object and a level, I can walk to the left and right etc, but I am having the hardest time making my character jump. One of the problems I'm having is that when I get rigidbody2D.velocity.x or y, the y has a negative value when the game starts (character spawns in the air), but after a few frames it changes to 0, even while falling. The same applies to the x of velocity, when I start moving my character it will shortly display a value, but then changes back to 0 even while moving.
Also my jumping is really buggy for some reason, no matter what I set the jumpForce to beyond 3000, it will never go beyond a certain height... My project's gravity is set to 30.
This is my player's object script:
using UnityEngine;
using System.Collections;
using System;
using System.Diagnostics;
[RequireComponent(typeof(Animator))]
public class Player : MonoBehaviour {
const float maxSpeed = 1000f;
const float jumpForce = 2000f;
private Animator animator;
bool onGround = false;
public Transform GroundCheck;
public LayerMask GroundLayers;
const float groundCheckRadius = 1f;
bool facingLeft = false;
void Start ()
{
animator = GetComponent<Animator>();
}
void Update()
{
bool oldOnGround = onGround;
onGround = Physics2D.OverlapCircle(GroundCheck.position, groundCheckRadius, GroundLayers);
if (onGround != oldOnGround)
animator.SetBool("Ground", onGround);
}
void FixedUpdate()
{
float inputX = Input.GetAxis("Horizontal");
float inputY = Input.GetAxis("Vertical");
animator.SetFloat("hSpeed", Math.Abs(inputX));
animator.SetFloat("vSpeed", rigidbody2D.velocity.y);
float speedX = inputX * maxSpeed;
rigidbody2D.AddForce(Vector2.right * speedX);
print("x: " + rigidbody2D.velocity.x + "y: " + rigidbody2D.velocity.y);
if (onGround && inputY > 0)
rigidbody2D.AddForce(Vector2.up * jumpForce);
if ((inputX < 0 && !facingLeft) || (inputX > 0 && facingLeft))
Flip();
}
void Flip()
{
// Switch the way the player is labelled as facing.
facingLeft = !facingLeft;
// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void OnCollisionEnter2D(Collision2D info)
{
}
void OnCollisionExit2D(Collision2D info)
{
}
}
I'm experiencing this issue too. I've got an object that jumps and falls by applying a force on the jump command. This triggers the fall animation and blocks any future jumps until the object has landed. I was testing for this by checking when the y velocity is at (or close to) 0
I wrote this code to see what velocity I was getting from rigidbody2d.velocity.y
: var jump:$$anonymous$$eyCode; var jumpForce:float = 5000f;
private var bIsJumping:boolean;
function Update () {
if(bIsJumping) {
print($$anonymous$$athf.Abs(rigidbody2D.velocity.y));
} else print("-----");
}
function FixedUpdate() {
if(!bIsJumping&&Input.Get$$anonymous$$ey(jump)) {
rigidbody2D.AddForce(new Vector2(0f,jumpForce));
bIsJumping = true;
}
}
The output was a mixture of 3.8s, 3.6s and the randomly interjected 0s, before hittings the 1E-7s which are the usual physics jitters for landing on a surface.
Anyone know why this is?
I use:
rigidbody.velocity.sqr$$anonymous$$agnitude;
It always seems to give good results. Although it won't just give y or x results individually
Answer by pareinjeanphilippe · Oct 19, 2016 at 11:30 AM
rigidbody.velocity.sqrMagnitude; RETURN 0 for me for x ?
Your answer
Follow this Question
Related Questions
Rigidbody2D.velocity out of controll 3 Answers
Smoothly move continuous moving object 1 Answer
Stuttering/jerky movement of kinematic rigidbody 2d moving by velocity in 2D game on iOS 0 Answers
Displaying the y value score,Displaying the score of the Y value 1 Answer
Make object stay inside circle 1 Answer