• 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 /
  • Help Room /
avatar image
Question by y11t_tr · Jan 09, 2017 at 05:56 PM · c#collisionmovementvector3geometry

How to SHOOT an object on a curved path without using Rigidbody

Hey there,

Lets say im launc$$anonymous$$ng a cannon ball. It needs to shoot towards players facing point and slowly lose height and $$anonymous$$t the ground eventually. Like t$$anonymous$$s: alt text

How do I set up the movement of cannonball object like t$$anonymous$$s? The cannonball WILL NOT HAVE RIGIDBODY property. So I need to set position of cannonball on each Update().

Here is how I shoot cannonball with my standart first person controller:

 void Update () {
 if (Input.GetButton("Fire1"))
 {
     GameObject cannonBall= Instantiate(projectile, transform.position, transform.rotation);

 }
 }


And here is my movement script on cannonball. Currently it goes straight forward without $$anonymous$$tting ground.

 void Update () {

     if (transform.position.y > 0)
     {
         transform.Translate(Vector3.forward * Time.deltaTime * 10);
     }

 }




cannon.png (15.3 kB)
Comment
Scoutas

People who like this

1 Show 0
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

Answer by Scoutas · Jan 09, 2017 at 07:04 PM

I would t$$anonymous$$nk that you would need to figure out the formula for the parabola path that you want your cannon ball to take. Then using it, you would increment the x value by some stepSize (e.g. Time.deltaTime) and plug it into the equation to get back the y value. Then put the cannon ball at those coordinates.

T$$anonymous$$s way you would need to figure out a way to know when the cannon $$anonymous$$t the ground so the ball would stop when it $$anonymous$$ts the ground.

You could use the formula that involves the top point of the parabola

y = a(x - x1)^2 + y1

where x1 is the x coordinate of the top point and y1 is the y coordinate of the top point. Or the one with intercepts

y = a(x - x1)(x - x2)

where x1 and x2 are the points at w$$anonymous$$ch parabola intecepts the x axis.

You'd need to fiddle around with these t$$anonymous$$ngs to get t$$anonymous$$ngs working for you the way you want to.

I t$$anonymous$$nk that the intecept form would work best, because you could figure out the intercept point be$$anonymous$$nd your cannon and then just input the next intercept point, to get exactly what you would need. And the a in that formula would be the tan(angle), where the angle is the angle that the cannon is rotated by from the x axis, anticlock-wise. (And because of the way that Unity handles angles, I t$$anonymous$$nk instead of tan(x), you would need to use ctg(x), but I'm not entirely sure about t$$anonymous$$s one).

EDIT: @y11t_tr

Okay, so additional information. I tried implementing t$$anonymous$$s in Unity, and it gave me quite a few problems.

First t$$anonymous$$ngs first - a point directly be$$anonymous$$nd the cannon is not the intercept of the ball movement parabola. That's a wrong assumption. Because of t$$anonymous$$s, the usage of the intercept formula crumbles.

I tried figuring out a way to do it all without it, but just couldn't. Anyway, I decided that I would just find the formula of the parabola itself, but that brought another problem back. I only had two points and I needed three points to make what I had in mind work. So I figured I'd make some sort of a hack.

I have two points in the scene that the ball should go through. First one is the point, from where the cannon fires, the next one is the target point. So, I decided that I would put a t$$anonymous$$rd point, an extremely tiny distance away from the one that was in front of the cannon. That fixed the problem and now I had three points with w$$anonymous$$ch I could draw a parabola path.

Now, figuring out the parabola itself. I tried my luck at deriving the equations to do so, but to no avail. Anyway, a quick google search gave me some results.

To find the equation of the parabola, using three random points, you need to figure out y = ax^2 + bx + c what the a, b and c is in t$$anonymous$$s equation. Anyway, I found these equations:

Say we have three points (x1, y1), (x2, y2), (x3, y3).

denominator = (x1 - x2) * (x1 - x3) * (x2 - x3)

a = (x3 * (y2 - y1) + x2 * (y1 - y3) + x1 * (y3 - y2))/denominator

b = (x3 * x3 * (y1 - y2) + x2 * x2 * (y3 - y1) + x1 * x1 * (y2 - y3))/denominator

c = (x2 * x3 * (x2 - x3) * y1 + x3 * x1 * (x3 - x1) * y2 + x1 * x2 * (x1 - x2) * y3)/denominator

And now, all you need to do, is to put everyt$$anonymous$$ng into code:

 float GetDenominator(Vector3 point1, Vector3 point2, Vector3 point3){
         return (point1.x - point2.x) * (point1.x - point3.x) * (point2.x - point3.x);        
     }
 
     float GetA (Vector3 point1, Vector3 point2, Vector3 point3){
         return ((point3.x * (point2.y - point1.y)) +
                 (point2.x * (point1.y - point3.y)) +
                 (point1.x * (point3.y - point2.y))) / GetDenominator (point1, point2, point3); 
     }
 
     float GetB (Vector3 point1, Vector3 point2, Vector3 point3){
         return ((Mathf.Pow(point3.x, 2) * (point1.y - point2.y)) +
                 (Mathf.Pow(point2.x, 2) * (point3.y - point1.y)) +
                 (Mathf.Pow(point1.x, 2) * (point2.y - point3.y))) / GetDenominator (point1, point2, point3); 
     }
 
     float GetC (Vector3 point1, Vector3 point2, Vector3 point3){
         return ((point2.x * point3.x * (point2.x - point3.x) * point1.y) +
                 (point1.x * point3.x * (point3.x - point1.x) * point2.y) +
                 (point2.x * point1.x * (point1.x - point2.x) * point3.y)) / GetDenominator (point1, point2, point3);
     }

And the only t$$anonymous$$ng left to do, is to write the movement script for the cannon ball. Good t$$anonymous$$ng is, that using it t$$anonymous$$s way, the target and the cannon can be anywhere on the same 2D plane and it would work. Anyways, cheers!

Comment
TheSmokingGnu
Kashkool

People who like this

2 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 Kashkool · May 06, 2019 at 05:44 PM 0
Share

I really appreciate your effort. can you please explain how the movement script will work with your code?

avatar image

Answer by y11t_tr · Jan 13, 2017 at 09:06 AM

Wow, thanks for the detailed answer. Just noticed someone actually replied! I will check t$$anonymous$$s out tell you if it worked for me. Thanks!

Comment

People who like this

0 Show 0 · 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

316 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 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 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

How to make more realistic bot AI? 2 Answers

Moving GameObject a specific distance in the Z direction and back again - regardless of rotation 1 Answer

Issue with character movement and collision using Physics2D.Raycast. 0 Answers

Set variable to position of collided object. 0 Answers

How can I remove movement or camera rotation in one direction with a trigger 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