• 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 Simon 10 · Feb 02, 2011 at 06:03 AM · projectiletrajectorycannonball

Projectile trajectory

Hi,

Im new to unity and im trying to make a game quite simple. i got a tower in the middle of the screen on which there a cannon. i need to calculate the trajectory of a cannon ball which i want to land where i click on the field (like a RTS view or D2 view).

i tried :

Vector3 Calculate(Vector3 p1, Vector3 p2) { float initialSpeed = 10.0f; float range = (p2 - p1).magnitude; float gravity = 9.8f;

 float angle = 0.5f * (Mathf.Asin(gravity * range / (initialSpeed * initialSpeed)) ;

 Vector3 horizontalDirection = (p2 - p1).normalized;

 Vector3 verticalDirection = Vector3.up;

 Vector3 initialVelocityHorizontal = initialSpeed * Mathf.Cos(angle) * horizontalDirection;

 Vector3 initialVelocityVertical = initialSpeed * Mathf.Sin(angle) * verticalDirection;

 Vector3 initialVelocity = initialVelocityHorizontal + initialVelocityVertical;

 return initialVelocity;

}

void FireBullet() { GameObject Bullet = Instantiate(CannonBall, transform.position, transform.rotation) as GameObject;

 RaycastHit hit = new RaycastHit();

 Ray ray = Camera.allCameras[0].ScreenPointToRay(Input.mousePosition);

 if (Physics.Raycast(ray, out hit, 10000))
 {

     float Vx, Vz;

     Vx = (hit.point.x - transform.position.x) / 0.11111f;
     Vz = (hit.point.z - transform.position.z) * 9.0003f;
     Debug.Log(Vx + " :: " + Vz);
     Debug.Log(hit.point.x.ToString() + " :: " + hit.point.z.ToString());

     Bullet.rigidbody.velocity = Calculate(transform.position, hit.point); ;

 }

}

but i'm getting angle always = to NaN... so nothing work at all

Hope you can help

Comment
Add comment · Show 3
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 zannghast · Feb 02, 2011 at 06:41 AM 0
Share

Let me get this straight, you want to see a trajectory-like thing spurting out from the tower/cannon so that you can see where the cannon ball will eventually land. Is this correct?

avatar image Simon 10 · Feb 02, 2011 at 02:59 PM 0
Share

No i only want to know what force or what velocity i have to apply to the ball so it will land where i clicked.

avatar image Simon 10 · Feb 07, 2011 at 11:29 PM 0
Share

Anyone can help me out ?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by yosh 1 · Feb 25, 2011 at 01:42 AM

i think ur question same like my question..

http://answers.unity3d.com/questions/43083/lobbing-to-certain-position

to get lobing certain position is worked now.. but its doesnt have physic. maybe u can edit my code..

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.

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

No one has followed this question yet.

Related Questions

Basic projectile trajectory with transform 1 Answer

Projectile Trajectory Velocity, with predefined Start and End Positions 0 Answers

how to draw trajectory of rocket for faux gravity in unity???? 0 Answers

How to check if a trajectory is blocked? 1 Answer

Artillery angle of reach prediction 2 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