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 Libelnon · Feb 28, 2013 at 11:33 PM · physicstrajectory

Help with calculating 'Grenade' projectile trajectories

I've gotten myself in a bit deep with Unity already, and I'm only in the second semester of a Computer Games Design course. Because of that, to continue with my idea, I need a hand from someone who's experienced with coding in Unity and also a dab hand at maths.

One of the specific points of the brief in this topic is that there must be some form of projectile existing within the game's structure to demonstrate the ability to use rigidbody components. While this is a simple enough thing to implement in, say, a FPS styled game where you would only have to click a button and the physics engine would do the work. I've decided - with the lecturer's permission - to bend the rules a bit, and am doing a isometric styled RTS/RTT game al la World in Conflict - which causes some issues with projectiles. While I can simply code in pressing a button and launching a projectile, I need to be able to click a location, and the game calculate the power needed to throw the grenade at a 45 degree angle.

I know I need the angle, power and target of the grenade in order to create the formulae to do this. I've got the angle - 45 degrees - and I've got the target - the point in which a raycast from the mouse location intersects the terrain. My problem is how to get the relation between the target and the angle to determine the power required to get the location. Does anyone know the formulae required for this, and how I'd write a script to implement it?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Owen-Reynolds · Mar 01, 2013 at 07:00 AM

If you aren't changing the angle, especially using 45 degrees, it's a lot easier.

To make it simpler, find the forward/up speed (at 45, they are the same,) instead of the total speed. cosine of 45 of 0.707, so you can divide by that to get total speed needed (if you need 10 forward speed, use 10/0.707 total speed at 45 degrees.)

The ball will be in the air for exactly speed/9.8*2 seconds. To see this, suppose you shoot it with up=19.7. It will go up for 2 seconds until gravity kills up speed, then the exact reverse process take 2 seconds to fall (and it will be falling down at the exact speed you fired it up.) If the target isn't on the same level, there are formulas for the time.

The distance is (note that forward speed = up speed): speed*time == speed * speed/4.9. Solving for speed, you get: S*S=D*4.9, or S=(sqrt(D*4.9).)

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 Tarlius · Mar 01, 2013 at 07:39 AM 0
Share

The math seems to check out, but when I built a test game the gravity defaulted to 9.81, which might cause some $$anonymous$$or problems.

OP: Use 4.905, set gravity to 9.8, or use Physics.gravity / 2 if you care about the inaccuracy.

On a side note, deliberately adding inaccuracy and applying damage based on how close the projectile lands could get bonus points (Although to be safe, maybe make two units; one that demonstrates you did it right, and another to demonstrate a non-perfect thrower.)

avatar image
0

Answer by robertbu · Feb 28, 2013 at 11:41 PM

Here's a link to a UnityAnswers post asking questions about something similar. There is some code halfway down the page for calculating a firing solution, but I'd spend some time both understanding the math and the code before I'd use it:

http://answers.unity3d.com/questions/49195/trajectory-of-a-projectile-formula-does-anyone-kno.html

Here are some links to the math:

http://en.wikipedia.org/wiki/Trajectory_of_a_projectile

http://hyperphysics.phy-astr.gsu.edu/hbase/traj.html#tra14

http://en.wikipedia.org/wiki/Trajectory

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 AlucardJay · Mar 01, 2013 at 04:02 AM 1
Share

There is also the cannonball answers by Aldo :

  • http://answers.unity3d.com/questions/148399/shooting-a-cannonball.html

  • http://answers.unity3d.com/questions/248788/calculating-ball-trajectory-in-full-3d-world.html

  • http://answers.unity3d.com/questions/145972/how-to-make-enemy-canon-ball-fall-on-mooving-targe.html

  • http://answers.unity3d.com/questions/296749/display-arc-for-cannons-ball-trajectory.html

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

12 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

Related Questions

AI Turret Aiming 2 Answers

Advance seesaw effect 0 Answers

Aim preview for projectile trajectory 1 Answer

Project physics trajectory 1 Answer

Calculating 3D Physics Prediction of Shot Direction with Moving target and moving gun (inertia) 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