• 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 SVGK · Apr 29, 2014 at 07:46 PM · rigidbodyvelocitygravityslowfalling

Rigidbody y velocity is stuck on 0, gravity is not turned off.

Greetings, I've come across a pretty serious problem, the rigidbody player falls at an impossibly slow speed when close to the ground, the fault is of this code:

     Vector3 forwardVel = movementBase.transform.forward * speed * moveVertical;
     Vector3 horizontalVel = movementBase.transform.right * speed * moveHorizontal;
     
     if(Physics.Raycast(transform.position, -Vector3.up, out hit, distanceToGround + 0.1f))
     {
         if (hit.collider.rigidbody)
         {
             isGrounded = true;

             rigidbody.velocity = forwardVel + horizontalVel + hit.collider.rigidbody.velocity;
         }
         else
         {
             isGrounded = true;
             
             rigidbody.velocity = forwardVel + horizontalVel;
         }
     }
     else
     {
         isGrounded = false;

         var yVel = rigidbody.velocity;
         yVel.y = rigidbody.velocity.y;
         
         rigidbody.velocity = forwardVel / 20 + horizontalVel / 20 + yVel;
     }

Sadly, I can't find a way to fix this, the issue is that when the ray hits the ground, I set the velocity, this is necessary for the game to work, my solution of setting the yVel in midair doesn't work with the ground because if this happens, then any velocity settings are just accelerated far too much, I suspect this is because the yVel starts out from rigidbody.velocity, which results in just adding more and more velocity over time.

I've already tried using AddForce instead, same problem, except a little worse.

How can I fix this?, all the code here is in FixedUpdate.

Comment
Add comment · Show 7
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 Jeff-Kesselman · Apr 29, 2014 at 07:49 PM 0
Share

We need to know what the value of speed is.

Yo uphold also print your calculated velocity as a Debug.Log and see fi it is what you expect it to be.

avatar image tanoshimi · Apr 29, 2014 at 07:53 PM 0
Share

Also "falling" at a slow speed rather suggests you're talking about the effect of gravity (i.e. movement in the up/down y-axis). The code you've shown is for forward/backward and side (i.e. z-/x- axis).

$$anonymous$$ost common reason for gravity to appear too weak is that you have increased the scale of the model on import.

avatar image SVGK · Apr 29, 2014 at 07:55 PM 0
Share

Speed is a simple float that equals 6, changing it makes no difference, I checked the velocity, apparently there is absolutely no velocity on the y axis.

The gravity is perfectly normal without me assigning the velocity here:

 rigidbody.velocity = forwardVel + horizontalVel;

So I have some doubts about it being because of the model.

avatar image vvkkumar06 · May 31, 2014 at 08:00 AM 0
Share

Try to apply force ins$$anonymous$$d of velocity.

avatar image SVGK · May 31, 2014 at 08:03 AM 0
Share

Already tried that, it has the same problems as setting the Y velocity myself, except slightly worse.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
-2

Answer by Kamuiyshirou · Apr 30, 2014 at 09:12 PM

Hi dude! LIKE MY answer >

     using UnityEngine;
     using System.Collections;

     public class AddSpeedRigidBody : MonoBehaviour {

     public float speed = 10.0F;
     public float rotationSpeed = 100.0F;

     void Update() {
     float moveVertical = Input.GetAxis("Vertical") * speed;
     float moveHorizontal = Input.GetAxis("Horizontal") * rotationSpeed;
     moveVertical *= Time.deltaTime;
     moveHorizontal *= Time.deltaTime;
     transform.Translate(0, 0, moveVertical);
     transform.Rotate(0, moveHorizontal, 0);

     }
 }
Comment
Add comment · Show 2 · 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
avatar image Kamuiyshirou · Apr 30, 2014 at 09:21 PM 0
Share

Solution is this CODE!

avatar image meat5000 ♦ · May 31, 2014 at 05:16 PM 0
Share

Dude, you don't Translate rigidbodies in that way. $$anonymous$$esses with Physics and Collision detection.

AddForce to rigidbodies.

I didn't -1 this, just so you know :D

avatar image
0

Answer by tanoshimi · Apr 29, 2014 at 08:27 PM

Assuming you're calling this code every frame in FixedUpdate(), you're setting the rigidbody.velocity to be a sum of two Vector3s - forwardVel and horizontalVel - neither of which have a y component. Without a y component to a rigidbody's velocity, it won't fall.

Comment
Add comment · Show 6 · 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
avatar image SVGK · Apr 29, 2014 at 08:32 PM 0
Share

Hmm, I see what you mean, but how should I add it?, adding Physics.gravity to the velocity makes it fall far too fast, with no speed buildup.

avatar image tanoshimi · Apr 29, 2014 at 10:25 PM 0
Share

The "correct" answer is that you shouldn't be setting rigidbody.velocity directly at all (as noted at http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-velocity.html) - ins$$anonymous$$d you should apply forces to the rigidbody.

avatar image SVGK · Apr 30, 2014 at 10:43 AM 0
Share

I would do that, but using forces makes controls incredibly slippery, which isn't good for a platformer game.

avatar image SVGK · Apr 30, 2014 at 08:23 PM 0
Share

Wait, would using physics materials with my force rigidbody allow me to stop things being slippery unless I want it to?, cause that would be sweet.

avatar image PouletFrit · May 30, 2014 at 03:22 PM 0
Share

Yes you could, physics materials have frictions and bounciness attributes

Show more comments

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

27 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

OnMouseDown() tracking bad at 3D box collider 0 Answers

RigitBody and BoxCollider don't affect the object 1 Answer

Mimic Gravity Movement Via Setting Velocity 0 Answers

Check if rigidbody is falling 3 Answers

Object won't fall when I apply horizontal velocity and is colliding with wall. 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