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
2
Question by redteardrop · Aug 05, 2012 at 06:46 PM · physicscurve

display arc for cannon's ball trajectory?

I have a cannon that shoots a ball using the physics engine.


mass, ball speed and forward direction are the variables used. I believe that gravity is a constant. I am trying to find a way to display where the shot will curve the way most pool games will show you where the ball will go (except for it not curving). With the gravity and it being a curve it'll have to look like a sin wave starting from the cannon position and ending when it first hits something.


any easy way to do this?


I am fairly new to unity so please keep that in mind for "complicated" answers.


thanks


[edit] sorry for the double post, I didn't see my question up and thought I messed up instead of needing to wait for moderator approval.

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

Answer by redteardrop · Sep 26, 2012 at 01:37 PM

apparently wikipedia and trying to go back to my high school math helped. There also appears to be an issue with addforce. I had to make sure I used ForceMode.Impulse to get an accurate initial velocity. If anyone else wants something like this, check the links below for the wiki math and here is a general rundown.
http://en.wikipedia.org/wiki/Range_of_a_projectile
http://en.wikipedia.org/wiki/Trajectory_of_a_projectile

Things you will need to know to predict the trajectory of a launched object using a single forward axis and no air resistance or wind.
1. initial velocity of item (since f=m*a you can get the velocity by dividing the force applied (use ForceMode.Impulse) by the rigidbody mass)
2. the angle of launch (can be put in a variable in an update function)
3. gravity (should be a constant 9.81m/s^2 (just use 9.81)

Here comes the tricky part. You should be able to google the formuly (I can't find the link right now). What needs to be done is use a linerenderer using enough points and plotting each one based on the y height at a given x. It's a good idea to use a transform variable to point to the world coordinates of your starting position to call in code.

var maxDist;
var velocity;
maxDist = ((velocity * velocity) * ((Mathf.Sin((-angle * Mathf.Deg2Rad) *2))))/9.81;//convert degree angle to radian to use in Sin correctly
this will get you the max distance on a level surface and you can use that minus the initial position of the start point to get a range and a distance for each x of the linerenderer. This won't work for me since I am shooting from the top of a cliff so I just chose a set interval for each of my x positions.
To find Y us this code (I'm using a for loop to get each linerenderer position):
rayY[n] = (0) + (rayX[n] * (Mathf.Tan(-angle * Mathf.Deg2Rad))) -	(	
		(g * (rayX[n]*rayX[n])) /(((Mathf.Cos(-angle * Mathf.Deg2Rad) * velocity)  * (Mathf.Cos(-angle * Mathf.Deg2Rad) * velocity)) *2));
The (0) at the beginning is the initial position of y, mine is kept at 0 here since I have the script attached to a child object and the math is local space so it'll move around the world fine. It should even rotate left or right on the cannon and still work properly this way (haven't tried it) rayX[n] is the x position.
Anyway, hope this helps someone else too. I'll see if I can put it in the asset store at some point just to easily add to a project.

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 nmbileg · Sep 02, 2018 at 06:18 AM 0
Share

nice! you helped me!

avatar image
5

Answer by TowerOfBricks · Aug 05, 2012 at 08:36 PM

Depending on input variables a simple formula can be created which will give the object's position after a given time. See http://en.wikipedia.org/wiki/Trajectory_of_a_projectile

The easiest is probably this one:

 public Vector3 PlotTrajectoryAtTime (Vector3 start, Vector3 startVelocity, float time) {
     return start + startVelocity*time + Physics.gravity*time*time*0.5f;
 }

To extend this you can raycast to see when it first hits:

 public void PlotTrajectory (Vector3 start, Vector3 startVelocity, float timestep, float maxTime) {
     Vector3 prev = start;
     for (int i=1;;i++) {
         float t = timestep*i;
         if (t > maxTime) break;
         Vector3 pos = PlotTrajectoryAtTime (start, startVelocity, t);
         if (Physics.Linecast (prev,pos)) break;
         Debug.DrawLine (prev,pos,Color.red);
         prev = pos;
     }
 }

This will only draw the trajectory in the editor, but you could draw it in the game with for example vectrosity as Fattie suggested. Also, DO NOT try to calculate with air resistance, that is hard (promise).

In case you are wondering, since there is not air resistance, the mass of the object is irrelevant.

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 LeakySink · Dec 13, 2012 at 11:37 PM 0
Share

This solution was really useful to me, I'm trying to write something that will simulate where a ball will land before it's thrown and I nearly have it working but I don't have the balls velocity to put into the equation, can you provide any assistance?

Thanks.

avatar image computas · Oct 11, 2018 at 05:47 AM 0
Share

Thx man, very helpful

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

Navigate Curve at Constant Speed 3 Answers

Make an Object Responding to Physics 1 Answer

how to shot a soccer ball in curve path according to the swipe type 2 Answers

Moving rigidbody along spline, and being able to use physics 1 Answer

2D 360 degress platformer example needed 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