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 /
avatar image
0
Question by notijaz · Apr 29, 2020 at 10:04 PM · physicsgravitybullettime.deltatimerealistic

Aim helper in realistic sniper game

I'm making a sniper game with gravity and wind influence on bullet. I'm moving bullet with constant velocity in forward direction and adding physics.gravity*Time.FixedDeltatime. So if there is a target 300 units away my bullet with take 36 frames to reach it(300/velocity*Time.FixedDeltatime) which we will call predictedFrames, when i multiply predictedFrames with gravity it should give mei the y position of bullet after 36 frames but it gives me the wrong position. I did Debug.Log(physics.gravity*Time.FixedDeltatime) and it give me -0.2 which should be the gravity per frame but it's not, after the first frame bullet y position is 0.0011 something. I did everything but nothing worked. plz help

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
0
Best Answer

Answer by icehex · Apr 29, 2020 at 11:20 PM

Hi @notijaz you're really close but remember that acceleration is a change in velocity, i.e. a change in distance over time, over time. predictedFrames * Time.fixedDeltaTime represents the amount of total travel time your bullet has before it reaches the target at 300 units. The equation to calculate fall distance under acceleration is:

 y_fall = 0.5 * acceleration * time * time;     //this is the time over time portion for distance travelled

Check this out: https://en.wikipedia.org/wiki/Equations_for_a_falling_body


And here's some (updated) code that should work for you:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BulletController : MonoBehaviour
 {
     float initial_velocity = 417f;
     float distance_to_target = 300f;
     float time_to_target;
     int predictedFrames;
     float bullet_drop;
     Vector3 bulletvelocity;
 
     // Start is called before the first frame update
     void Start()
     {
         bulletvelocity = initial_velocity * (transform.rotation * new Vector3(1, 0, 0)); // +x velocity
         Debug.Log(bulletvelocity);
 
         time_to_target = distance_to_target / initial_velocity;
         Debug.Log("Time to reach target: " + time_to_target);
 
         predictedFrames = (int) ((distance_to_target / initial_velocity) / Time.fixedDeltaTime);
         Debug.Log("Frames to reach target: " + predictedFrames);
 
         bullet_drop = -Physics.gravity.y * 0.5f * time_to_target * time_to_target;
         Debug.Log("Distance to drop: " + bullet_drop);
 
         bullet_drop = -Physics.gravity.y * 0.5f * predictedFrames * Time.fixedDeltaTime * predictedFrames * Time.fixedDeltaTime;
         Debug.Log("Distance to drop: " + bullet_drop);
 
         Debug.Log(Physics.gravity);
 
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         bulletvelocity += Physics.gravity * Time.fixedDeltaTime;
         transform.position += bulletvelocity * Time.fixedDeltaTime;
     }
 }
Comment
Add comment · Show 6 · 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 icehex · Apr 29, 2020 at 11:23 PM 0
Share

Just a note, when converting float back to int using (int), it rounds down I think. So when you calculate predictedFrames make sure to round up your float first, that way your calculation isn't missing frames. Or you can have your predictedFrames be a float and your calculations will work fine.

avatar image notijaz · Apr 30, 2020 at 10:13 AM 0
Share

Thank you sir for your time.I will implement this later and will let you know

avatar image notijaz · Apr 30, 2020 at 10:32 AM 0
Share

sorry sir but there is one problem I'm not using rigidbody. $$anonymous$$y code looks like

 void start()
 {
    bulletvelocity=transform.forward*speed;
    point1=transform. position;
 }
 
 void update()
 {
    bulletvelocity+=physics.gravity *Time.FixedDeltatime;
    point2=point1+bulletvelocity* Time.FixedDeltatime ;
    //raycast stuff for checking hit
    point1=point2;
    transform.position =point1;
 }
avatar image icehex notijaz · Apr 30, 2020 at 04:22 PM 0
Share

No worries - just change rigidbody.velocity calls to your bulletvelocity. I've updated my answer to show that.

avatar image notijaz icehex · Apr 30, 2020 at 05:59 PM 0
Share

thank you sir, it worked

avatar image notijaz · Apr 30, 2020 at 05:26 PM 0
Share

thank you sir. I'll let you know when i implement it later

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

212 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 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 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 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 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 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 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 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

How to make telephone wires 1 Answer

Problems making a glider 3 Answers

How to create ricochet effect on bullets (2D) 0 Answers

Model tips over for no reason 1 Answer

Rigidbody doesn't fall when platform below it shrinks to nothing. 1 Answer


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