- Home /
character(ball) rolls with less speed when I build it
So I've been working on a ball roller game and it works fine till I build it. when I build it the ball rolls at a slower pace than it does in game. How do I get it for them to be the same? Code source:
public class movement : MonoBehaviour
{
public Rigidbody rigid;
public float horizontal;
public float vertical;
public float speed = 1f;
// Start is called before the first frame update
void Start()
{
rigid = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
Vector3 direction = new Vector3(horizontal, 0.0f, vertical);
rigid.AddForce(direction * speed);
}
}
Answer by Ady_M · Oct 16, 2020 at 05:17 AM
Use FixedUpdate() when dealing with Rigidbodies.
Keep in mind that you should NOT use Time.deltaTime inside FixedUpdate when calling Physics methods (Force / Torque).
To add a little more detail to this, here's how AddForce() functions are utilized:
public KeyCode actionKey = KeyCode.W;
private bool actionTaken = false;
void Update()
{
if(Input.GetKeyDown(actionKey))
{
actionTaken = true;
}
}
void FixedUpdate()
{
// Apply a continuous force, X units per second acceleration
// Factors in mass (greater mass = less push)
// Intended to be used regularly in FixedUpdate cycle
// Force$$anonymous$$ode.Force automatically applied when not specified
rigid.AddForce(direction * speed * rigid.mass, Force$$anonymous$$ode.Force);
// This applies the same force as:
// Apply a continuous force, X units per second acceleration
// Ignores mass (no need to multiply mass in)
// Intended to be used regularly in FixedUpdate cycle
rigid.AddForce(direction * speed, Force$$anonymous$$ode.Acceleration);
if(actionTaken)
{
// Apply an instantaneous force of X units velocity change
// Factors in mass (greater mass = less push)
// Intended to be used once at a time
// (Can generally be safely used from Update(),
// but FixedUpdate() is still a more reliable location)
rigid.AddForce(direction * speed * rigid.mass, Force$$anonymous$$ode.Impulse);
// This applies the same force as:
// Apply an instantaneous force of X units velocity change
// Ignores mass (no need to multiply mass in)
// Intended to be used once at a time
// (Can generally be safely used from Update(),
// but FixedUpdate() is still a more reliable location)
rigid.AddForce(direction * speed, Force$$anonymous$$ode.VelocityChange);
actionTaken = false; // Enforce a single physics update trigger
}
}
To note, the Force and Acceleration Force$$anonymous$$odes can be effectively (albeit inefficiently) converted to Impulse and VelocityChange respectively by dividing by Time.fixedDeltaTime (or, by extension, Time.deltaTime, since it returns the same value when checked in FixedUpdate()).
Conversely, Impulse and VelocityChange can be converted to Force and Acceleration by multiplying by Time.fixedDeltaTime instead.
Again, those conversions are not worth making, but the information itself can aid in understanding what the difference is between the Force$$anonymous$$ode choices.
Edit: Typo
Answer by axlxi1 · Oct 15, 2020 at 03:03 PM
use time.deltatime:
rigid.AddForce(direction * speed * Time.deltatime);
As @Ady_$$anonymous$$ mentions, this is terrible advice. This decreases the provided force by 1 / Time.deltaTime
times (by default, a 50x decrease) and, as a result, varies in the event that the physics timesteps are modified.
Your answer
Follow this Question
Related Questions
I have a movement script, but how can i make it so i can move half the speed while in the air. 0 Answers
My Character is moving fine horizontally and vertically, but moves way too fast diagonally 2 Answers
Strange Character Controller Behavior Caused by Simulated Gravity and Ground Check 0 Answers
How to make fish movement with keyboard arrows look natural? 1 Answer
Limiting Speed on Two Axis 1 Answer