Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by AngryBurritoCoder · Sep 06, 2015 at 10:45 AM · timetime.deltatimeindependent

Why is this small part of code not frame independent ?

I have this code and been messing around with time.deltatime for quite a long time now, not sure why it isnt frame independant, please help.

 void Update()
     {
 
         Vector3 velocity = bulletSpeed * transform.forward + gravity;
     
         //Debug.Log (transform.position);
         //Debug.Log (velocity);
 
         gravity += new Vector3 (0, -9.8f, 0);
         newPos = velocity * Time.deltaTime + transform.position;
 
         direction = newPos - oldPos;
         float dist = direction.magnitude;
 
         if(Physics.Raycast(oldPos,direction,out hit,dist,layersToCheck))
         {
             //Debug.Log("hit Object name: " + hit.collider.gameObject.name);
             Destroy(gameObject);
 
         }
         else
         {
             oldPos = transform.position;
             transform.position = newPos;
         }

Comment
Add comment
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

1 Reply

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

Answer by wibble82 · Sep 06, 2015 at 10:51 AM

Changing answer after I was corrected.

On closer inspection, this approach is a little curious. I'm guessing what you want is a bullet that is fired outwards in the direction of 'transform.forward' then moves in a curve due to gravity.

However its a little strange for a few reasons. It is kind of half way between doing things parametrically (recalculating the position every frame based off initial position/velocity) and iteratively (tracking velocity/position and incrementing every frame.

You seem to be building up velocity-due-to-gravity every frame, then adding this on to a fixed velocity value.

I would approach it this way:

 Vector3 velocity;
 Vector3 gravity = new Vector3(0,-9.8,0);
     
 void Start()
 {
     velocity = bulletSpeed * transform.forward;
 }
 
 void Update()
 {
     velocity += gravity * Time.deltaTime;
     
     Vector3 new_pos = transform.position + velocity * Time.deltaTime;
 }
Comment
Add comment · Show 7 · 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 AngryBurritoCoder · Sep 06, 2015 at 11:01 AM 0
Share

the line 9 is supposed to add gravity over each frame, so that it creates a curved trajectory rather than a straight falling line. Edit: as in gravity adds up each frame to become bigger, as the object travels forward, this causes the object to fall in a curve

avatar image wibble82 · Sep 06, 2015 at 11:31 AM 0
Share

Hey there.

Yes I just noticed you redefine velocity every frame. This is a slightly odd way to write it. Typically gravity is a fixed acceleration, and you increase velocity every frame.

I guess what you're effectively doing here is storing gravity as a velocity. It is really 'how much velocity I have gained so far due to gravity'.

Either is valid - your is just a little less traditional :) In that case, you'll want to scale that gravity increment by Time.deltaTime each frame. I'll modify my answer above to update it.

avatar image AngryBurritoCoder · Sep 06, 2015 at 12:27 PM 0
Share

yes that works now, but the thing is, it has to be a really low bullet speed otherwise its way too fast in forward direction and barely falls due to gravity, where as when bullet speed is low , you can see the gravity take effect, but its really really slow forward movement

avatar image wibble82 · Sep 06, 2015 at 12:34 PM 0
Share

Well, turn up the gravity then :) You can make it do whatever you like just by playing with the numbers. What that code gives you is simply the curve that a body will undergo due to gravity. If you use -100 ins$$anonymous$$d of -9.8, it'll fall faster.

avatar image AngryBurritoCoder · Sep 06, 2015 at 12:44 PM 0
Share

But because im using real scale models ( 1um = 1m ) , shouldnt there be a way it works properly using real life values, bullet speed is 1500 velocity, while gravity is -9.8 down acceleration. if i use the above code that i posted ( frame dependant one ) it works pretty good ( compared to the other trajectory code i wrote using trajectory equations for non horizontal launched projectiles). But ill probably change the values for gravity and see how it goes.

Show more comments

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

28 People are following this question.

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

Related Questions

Load scene after time 1 Answer

Having trouble increasing the spawning speed of explosions 0 Answers

Farming game Time management 0 Answers

movement speed still changes with FPS; Despite using Time.deltaTime (And GetAxisRaw)! 0 Answers

How is it possible to verify values and is they are correct activate objects ? 0 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