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 Noztradamuz · Apr 01, 2014 at 02:46 AM · shotcannonsinparabolic

Calculate parabolic shot...

Hi, Im having some troubles, not actually coding (maybe) but perhaps math problems, Here's the thing. I have some kind of AI who makes shoots at you in a parabolic trayectory, now, I dont know how to succesfully calculate the force neded to apply into te proyectile in order to reach the target, I only know the distance betwen the shoter and the target with Vector3 target; Vector3 distanceFromTarget = target - transform.position; im giving some "randomish" angle to shot, based on the obstacles the AI have on his way but giving some angles more than 0 and below than 90, i cannot calculate the initial velocity. Im using Unity Physix for the bullets, when I shoot them it works well, I can manually calculate my trayectory and do some "Test and error" shots but, the AI must be capable of calculate the same parameters with a formula. Here's the code i have right now:

     void GetElevationDirection()
     {
         targetPosition = currenTarget.transform.position;
         vectorResultante = targetPosition - cannonPlace;
         estimatedElevation = Random.Range(30, 50);
         float finalAngle = estimatedElevation * 2;
         finalAngle = Mathf.Sin(finalAngle * Mathf.Deg2Rad);  
         float gx = 9.81F * vectorResultante.magnitude;
         estimatedSpeed = Mathf.Sqrt(gx / finalAngle);
         //estimatedSpeed *= 1.25F;
         estimatedElevation *= -1;
     }

I need to multiply at the end the "estimatedElevation" variable because when I rotate the 3D model of the cannon, it have inverted X axis and in that way the cannon points up and not down, but the estimated speed, is to low, even with the 25% increment, and somethimes I get an error on the Matfh.Sqrt because the "gx / finalAngle" division gets negative. I hope that someone can help me...

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

Answer by KrisSchnee · Apr 01, 2014 at 07:10 AM

I dont know how to succesfully calculate the force neded to apply into te proyectile in order to reach the target, I only know the distance betwen the shoter and the target... i cannot calculate the initial velocity.

It sounds like you should look up the math of projectile motion. The relevant equations should be available in physics textbooks on paper or the Web. You'll want to look at the situation like this, I think:

If you have a cannon firing with initial velocity described as Vx and Vy (horizontal and vertical axes), then the distance traveled horizontally at time T is X = (Vx) * T. The current height Y = (Vy)*T - (.5)gT^2, where "g" is the acceleration of gravity (usually estimated as 9.81 m/s^2 near Earth's surface, but you'll have to translate to Unity units) and "T^2" is T squared.

To find the initial velocity required to hit some point XY, not considering any obstacles in the way, you need to solve this "system of equations" for Vx and Vy. I would define a cannon's power not as Vx and Vy directly, but Vo (original velocity) and an angle Theta up from the ground. (0-180 degrees or 0 to pi radians.) Then, you can find Vx and Vy as "Vo cos Theta" and "Vo sin Theta". Now for any given velocity, you can just solve for Theta, or vice versa. One option then is to check several possible velocities and see if any Theta value will hit the target at that velocity.

If you're looking to calculate force rather than initial velocity, the math gets a bit more complex, but figure that F = ma, and you're applying this force over some time period (realistically, the time that the cannonball is flying out of the cannon) to get enough acceleration over time to get the velocity you want.

As for angles going negative, can you take the absolute value of the result?

Comment
Add comment · Show 2 · 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 Noztradamuz · Apr 01, 2014 at 03:41 PM 0
Share

Hi, thanks for the fast answer, i do have a formula that I get trough internet, that calculates the initial velocity given by the cannon at the shot momentum, knowing that the Vy will be Zero because reaching the target will mean that the proyectile did all the trayectory, the only problems i have are: Calculate the initial velocity of the proyectile neded to reach the X$$anonymous$$ax knowing the Theta angle and the Xmax, and second, Solve and uknown error on the function wich gives me negative values somethimes on the division, givin me an error on the $$anonymous$$atfh.Sqrt function. Thanks

avatar image Noztradamuz · Apr 01, 2014 at 03:47 PM 0
Share

If you need to, i can post the full script here, which calculates the Y rotation of the cannon in order to point the target, and then throw a random angle and then calculate the initial velocity then shot, i can post it as soon as i get home later, just one thing, Im from $$anonymous$$exico and the scrip would have some english and some spanish terms if you don't $$anonymous$$d so, i can upload the C# script :)

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

21 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

Related Questions

how to make the cannon fire yourself 1 Answer

Shot is activated only when I walk. 2 Answers

How Do I make it harder to move rigid body? 1 Answer

How to reverse a sin animation? 2 Answers

mecanim gun shot frame 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