Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
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 launching a cannon ball. It needs to shoot towards players facing point and slowly lose height and hit the ground eventually. Like this: alt text

How do I set up the movement of cannonball object like this? 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 hitting ground.

 void Update () {

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

 }




cannon.png (15.3 kB)
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
2

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

I would think 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.

This way you would need to figure out a way to know when the cannon hit the ground so the ball would stop when it hits 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 which parabola intecepts the x axis.

You'd need to fiddle around with these things to get things working for you the way you want to.

I think that the intecept form would work best, because you could figure out the intercept point behind 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 think instead of tan(x), you would need to use ctg(x), but I'm not entirely sure about this one).

EDIT: @y11t_tr

Okay, so additional information. I tried implementing this in Unity, and it gave me quite a few problems.

First things first - a point directly behind the cannon is not the intercept of the ball movement parabola. That's a wrong assumption. Because of this, 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 third 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 which 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 this 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 everything 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 thing left to do, is to write the movement script for the cannon ball. Good thing is, that using it this way, the target and the cannon can be anywhere on the same 2D plane and it would work. Anyways, cheers!

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

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 this out tell you if it worked for me. Thanks!

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.

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

Need some help programming basic AI movement 0 Answers

Grabbing the Relative eulerAngles.y of a Rotation 1 Answer

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

Set variable to position of collided object. 0 Answers

collision is making its own vector 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