• 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 thevengeous7 · Jan 27 at 10:08 PM · projectiletrajectorytrigonometry

Artillery angle of reach prediction

Hello! I'm trying to predict the angle of reach for my artillery gun for an RTS im making, however i've come across a problem. I'm not sure why, but the angle is always wrong, it either shoots short or too far. Here is my code:

 float angle = 0.5f * (Mathf.Acos ((-Physics.gravity.y * Vector3.Distance(muzzle.transform.position, target)) / (velocity * velocity)) * Mathf.Rad2Deg);
 Debug.Log("Angle =" + angle * Mathf.Rad2Deg);
 
 gameObject.transform.rotation = Quaternion.Euler(0, 0, (angle * Mathf.Rad2Deg));
 
 if(Input.GetMouseButtonUp(0)){
     Instantiate(bullet, muzzle.transform.position, muzzle.transform.rotation);
 }

I am attempting to use the steep angle of reach formula from the projectile motion wiki page. Thanks in advance!

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

Answer by kylecat6330 · Jan 28 at 12:28 AM

Alright if you are just trying to make it look accurate here is what you can do.

So you have your two points, the gun position and the target position. You can set the gun to fire at whatever angle you think is best. Then using the angle you get a position some distance out from the gun.

 curvePoint = gun.position + Vector3(Cos(angleInRadians), Sin(angleInRadians), Zdirection?).normalize * someDistance
 

Once you have that point you can make some lerps.

 upwardMotionPoint = Vector3.Lerp(gun.position, curvePoint, speed * Time.deltaTime);
 DownwardMotionPoint = Vector3.Lerp(curvePoint, target.position, speed * Time.deltaTime);
 projectile.position = Vector3.Lerp(upwardMotionPoint, DownwardMotionPoint, speed * Time.deltaTime);

Note that this is sorta pseudo code or just lazy code. So don't expect to copy it exactly and it works. It is just to give you general idea of a way to do it.

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 thevengeous7 · Jan 28 at 12:38 AM 0
Share

Thank you!

avatar image kylecat6330 · Jan 28 at 12:39 AM 0
Share

You can make it look good just may take some adjusting. It may do well to make an empty game object to be able to set as the curvePoint just make sure you can keep it in line with the direction of fire.

avatar image
0

Answer by kylecat6330 · Jan 27 at 10:23 PM

I may not be following this correctly, but from what I am seeing you have "angle" in degrees, and then you try to convert it into degrees again when you preform your rotation.

Comment
Add comment · Show 9 · 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 thevengeous7 · Jan 27 at 10:44 PM 0
Share

If i do not convert it into degrees again, the angle is completely off

avatar image kylecat6330 thevengeous7 · Jan 27 at 10:50 PM 0
Share

How far off and in what way? Is it in the opposite direction? Way too far? Way too close?

avatar image thevengeous7 kylecat6330 · Jan 27 at 10:55 PM 0
Share

In the direction of the target, but it always overshoots and barely adjusts the angle.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

133 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

Related Questions

simple trajectory for rigidbody 2 Answers

Making a trail renderer keep it's form of arc when object moves (Keep it in local space) -1 Answers

Trajectory on mouse up 0 Answers

I want to find out how to make a projectile home in on a target with an "arc". 1 Answer

Interpolated Projectile in vertical arc and horizontal (blendable) 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