Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 diddykonga · Mar 14, 2012 at 12:23 AM · rigidbodyvelocityfixedupdatetime.deltatime

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 ^

Comment
Add comment · Show 2
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 syclamoth · Mar 14, 2012 at 02:14 AM 0
Share

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.

avatar image diddykonga · Mar 14, 2012 at 02:59 AM 1
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 4 · 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 diddykonga · Mar 14, 2012 at 01:09 AM 0
Share

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.

avatar image syclamoth · Mar 14, 2012 at 01:40 AM 0
Share

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.

avatar image diddykonga · Mar 14, 2012 at 01:49 AM 0
Share

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?

avatar image syclamoth · Mar 14, 2012 at 01:53 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 1 · 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 diddykonga · Mar 14, 2012 at 01:04 AM 0
Share

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

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


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