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 12boulla · Jan 30, 2018 at 05:18 PM · rigidbody2dforceprojectilerigidbody.addforce

RigidBody2D projectile to target calculation

Hi, I am using this code to shoot a projectile in order to hit a target on the ground. I am applying an impulse force in the x direction, which is calculated from its height above the target. I am working out the force to add by dividing the x distance by the time it will take to fall due to gravity, but this is not seeming to work:

 float xDist = flag.position.x - transform.position.x;
 float yDist = transform.position.y - flag.position.y;

 float xForce = xDist/(Mathf.Sqrt(2*9.81f*yDist)/9.81f);
 
 rigidBody.AddForce((new Vector2(xForce, 0)) * homingForce, ForceMode2D.Impulse);

Can anyone help me? Not sure if my calculations are wrong, or whether the physics is rather different than a real life situation. Any help is greatly appreciated, thanks.

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

Answer by hrishihawk · Jan 30, 2018 at 06:46 PM

When I was in a similar situation I got it working by implementing the equation from this site. And I'm not sure if it's the best solution but it's working perfectly.What I did was to calculate the initial velocity of the projectile by solving Equations for the Horizontal Motion of a Projectile.With these conditions horizontal direction.

  1. Horizontal distance,x (xDist in your case)
  2. Initial Horizontal Velocity,vix (which is the value we want to solve)
  3. Horizontal acceleration,ax (Which is 0 in your situation)

And these vertical conditions

  1. Vertical Distance,y (negative of yDist because the projectile is going downwards)
  2. Intial vertical velocity ( which is 0)
  3. And vertical acceleration (-9.8)

You can solve for time required ,t using

 x = vix•t + 0.5*ax*t*t

Using the value of t solve for vix

 x = vix•t

Now the force to be added on the projectile will be

 m*vix/t

The AddForce() function assumes that the force is applied over the fixed timestep duration set by the Unity engine. By default, the fixed timestep is set to 0.02 seconds, you can change it if you want in project settings.You get the value for fixed time step in code using Time.fixedDeltaTime.Applying these things you final solution for force will be

 force = m*vix/Time.fixedDeltaTime.

Now you can add force to your rigidbody with

 rb.AddForce((new Vector2(force, 0)), ForceMode.Force);
 

Hope this helped :)

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

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

77 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

Related Questions

Trying to launch a rigidbody towards mouseposition but nothing makes it work. 1 Answer

I am a begginer trying to make a simple 2d platformer. Need help with rotation and jump height. 1 Answer

2D AddForce towards direction with CONSTANT FORCE whether the direction is far or near. 1 Answer

Physics based movement (Moving to fast) 1 Answer

Tank AI, 2d rigidbody projectile trajectory prediction 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