Limit forward velocity of gameobject causes flying
Hi, I have a ball as a gameobject and it is rolling forward but when I limit velocity of ball it starts to keep flying to up. This codes run in FixedUpdate()
if (app.model.ballModel.ballRigidBody.velocity.z > app.model.ballModel.velocityLimit) {
//Debug.Log("Speed Limit: " + app.model.ballModel.ballRigidBody.velocity.z + " " + app.model.ballModel.ballTransform.position.y);
float brakeSpeed = app.model.ballModel.ballRigidBody.velocity.z - app.model.ballModel.velocityLimit;
app.model.ballModel.ballRigidBody.AddForce(Vector3.back * brakeSpeed);
}
else
{
app.model.ballModel.ballRigidBody.AddForce(Vector3.forward * app.model.ballModel.speed);
}
Answer by Winterblood · Feb 13, 2016 at 07:05 PM
Need more context. You are only monitoring the z component of the ball's velocity, and applying forces along the world's Z axis. I think you are trying to work in the ball's local coordinate space, but that's going to be hard to track because the ball is rolling so it's coordinate space will be tumbling.
Try this (may have typos, I haven't compiled it):
float velmag = app.model.ballModel.ballRigidBody.velocity.magnitude;
if (velmag > app.model.ballModel.velocityLimit)
{
float brakeSpeed = app.model.ballModel.velocityLimit - velmag;
app.model.ballModel.ballRigidBody.AddForce(app.model.ballModel.ballRigidBody.velocity * brakeSpeed);
}
This is not working, when I try to limit velocity ball goes up.
Answer by abalta · Feb 14, 2016 at 01:39 PM
I think right way is checking drag to limit velocity. This should seems to work now.
if (app.model.ballModel.ballRigidBody.velocity.z > app.model.ballModel.velocityLimit) {
app.model.ballModel.ballRigidBody.drag = 0.1f;
}
else
{
app.model.ballModel.ballRigidBody.drag = 0.0f;
app.model.ballModel.ballRigidBody.AddForce(Vector3.forward * app.model.ballModel.speed);
}