- Home /
Rigidbody and Time.deltaTime
void FixedUpdate () {
ApplyFrictionExp();
if(Controller != null && Controller.isKinematic == false) {
Controller.velocity = Velocity * Time.deltaTime;
}
if(Velocity.x == 0 && Velocity.y == 0)
{
Controller.isKinematic = true;
}
else
{
Controller.isKinematic = false;
}
}
This is the code im using, might not be the best but....... My problem is for one, the velocity is massively slow since adding this, another problem is even after upping the amount that im adding the the Velocity varaible it still seems innacurate when switching graphic settings?
protected void ApplyJumpLogic() {
if(CurrentJumps > 0)
{
if(Input.GetKeyDown(KeyCode.Space))
{
CurrentJumps--;
Velocity.y = Jump;
}
}
}
Jump Code ^
protected void ApplyMovementLogic() {
if(Input.GetKey(KeyCode.RightArrow))
{
Velocity.x += Speed;
AttackDirection = Direction.Right;
HorizontalAttackDirection = HorizontalDirection.Right;
}
else if(Input.GetKey(KeyCode.LeftArrow))
{
Velocity.x -= Speed;
AttackDirection = Direction.Left;
HorizontalAttackDirection = HorizontalDirection.Left;
}
else if(Input.GetKey(KeyCode.UpArrow))
{
AttackDirection = Direction.Up;
}
else if(Input.GetKey(KeyCode.DownArrow))
{
AttackDirection = Direction.Down;
}
}
Movement Code ^
protected void ApplyGravity() {
Velocity.y -= Gravity;
}
Gravity Code ^
protected void ApplyFrictionExp() {
if(Velocity.x >= 0.1 || Velocity.x <= -0.1)
{
Velocity.x *= FrictionExp;
}
else
{
Velocity.x = 0;
}
Velocity.y = Mathf.Clamp(Velocity.y, -MaxFallSpeed, Velocity.y);
}
Friction Code ^
Hvaing read your entire code, I think you should just use inbuilt rigidbody physics. You are overcomplicating things trying to override velocity like that (I can see several places where you're likely to run into problems with deltaTime issues), and it provides you with no real advantages. Just reimplement it using forces and friction, and let the inbuilt physics engine handle the rest.
Ok yah i just went ahead and decided to use the built in physics, the jumps are a little bolty and my character sorta sticks to the sides of platforms but its fine for now
Answer by syclamoth · Mar 14, 2012 at 12:35 AM
I'm not sure why you feel the need to multiply rigidbody.Velocity by Time.deltaTime. This is pretty much guaranteed to have strange results, because it will make your objects move faster when the framerate is low, and slower when the framerate is high! In this case, you should only be multiplying the value by Time.deltaTime in two cases:
1: When trying to model a velocity in terms of distance moved-
transform.Translate(velocity * Time.deltaTime);
2: When trying to model acelleration-
velocity += acelleration * Time.deltaTime;
In your case, you are doing neither- you are directly setting the velocity, and this causes your problem.
Thats the point i want them to move faster when the framerate is slow and slower when the framerate is high, that way i can have movement that is not affected by user framerate.
No, you're missing the point. Rigidbody.velocity already takes care of that. Also, you are changing the velocity the wrong way- you're actually making them move faster, not just making them move further per frame! If you were implementing your own physics system this would be appropriate, but because you're plugging that value straight into Unity's internal engine, you shouldn't be modifying the values by deltaTime.
Well without any Time.deltaTime somewhere the movement is slower/faster based on the different graphics settings.
Should i just not be setting the velocity directly? and if not what are some other good ways of doing this?
Are you sure about that? It's possible that the problem is elsewhere in your code. Certainly doing what you are doing in your question is the wrong way to go about it.
In any case, the script reference specifically warns against setting velocity directly. If you are doing rigidbody manipulation, you should be using AddForce to apply motion to the object, and only directly incrementing Velocity in situations where you need an instantaneous boost- jumping, for example.
Where are you setting Velocity in your script? I think the real problem is almost certainly there, in the parts of your script that you haven't shown us. You definitely do need to have some factor of Time.deltaTime in there somewhere, it's just that multiplying the resultant Velocity by it is the wrong way to go.
Answer by rutter · Mar 14, 2012 at 12:42 AM
What are you trying to do? Hard to diagnose a problem that hasn't been described.
Time.deltaTime
is generally useful for values you want to change over time. It looks like you're setting approximately the same velocity on every frame.
void FixedUpdate() { //increase x by 5 units per second x += 5f * Time.deltaTime;
//this does NOT increase y by 5 units per second
//instead, it just sets y to a very small value
//notice the key difference between "+=" and "+" operators
y = 5f * Time.deltaTime;
}
Depending on what exactly you're trying to do, you may instead want to translate your object manually, or try calling AddForce()
on your rigidbody.
Well Velocity is a variable in my script, which is changed when the player moves or jumps or etc....
The Velocity is not the same most of the time
Your answer
Follow this Question
Related Questions
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
How to make an object moving in a certain direction, move in specific steps like 0.1f 1 Answer
Rigidbody velocity limiter 0 Answers
Setting a delay before dashing. 1 Answer
Moving an object in Start() vs moving an object in Update() with Time.deltaTime 2 Answers