- Home /
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.
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.
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.
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.
Already tried that, it has the same problems as setting the Y velocity myself, except slightly worse.
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);
}
}
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
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.
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.
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.
I would do that, but using forces makes controls incredibly slippery, which isn't good for a platformer game.
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.
Yes you could, physics materials have frictions and bounciness attributes