Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by $$anonymous$$ · Feb 07, 2014 at 10:10 PM · rigidbody2dvelocityvalueszero

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)
     {
 
     }
 }
 
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image solpyro · Mar 06, 2014 at 06:56 PM 0
Share

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?

avatar image highpockets · Mar 06, 2014 at 08:09 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by pareinjeanphilippe · Oct 19, 2016 at 11:30 AM

rigidbody.velocity.sqrMagnitude; RETURN 0 for me for x ?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges