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 /
avatar image
0
Question by xylofiso · Feb 01, 2016 at 04:11 AM · 2d gameprojectilearcparabola

How to have player fire projectile in a parabola?

Hello, I am trying to figure out a way to have my player fire the projectile in an arc "parabola" like style. Upon hitting the "submit" button I have the player fire a projectile that goes across the screen using the following code.

 GetComponent<Rigidbody2D> ().velocity = new Vector2 (speed, GetComponent<Rigidbody2D>().velocity.y);
 
 Vector3 position = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width,Screen.height,0f));

Sorry not sure what happened with the spacing but anyways, I want the projectile to arc as if they player was throwing a grenade. This is in a 2D game and I have been trying to research parabolas but I am not sure how I would incorporate the formula with this object. I have a transform for the firePoint on the character the projectile instantiates from and I just want the bullet maybe to go about 4 units vertical of the player and 3 units in front of the player, depending on the location of the firePoint each time. Any help is much appreciated, but I am looking for code and an explanation, not just a link to some website explaining parabolas unless it is also a website that explains how to code this in C#.

Comment
Add comment · Show 8
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 Cherno · Feb 01, 2016 at 11:15 AM 0
Share

Remember your mathematics lessons. A parabolic line can be described with a simple function f(x) = ...

There are online function plotting services that let you experiment until you get the right function.

Of course, you could also just use Unit'y physics system and let the Rigidbody2D handle it.

avatar image xylofiso · Feb 04, 2016 at 03:33 AM 0
Share

What do you mean f(x) = ...?

A parabola is y = ax(squared) + bx + c if I understand. I want a to be greater than 0 so it is like a rainbow shape. How can I have a game object fire along like this, no physics or anything involved, I just want to push a button then have the object follow that path.

avatar image Cherno xylofiso · Feb 04, 2016 at 01:59 PM 0
Share

That was just an example, like one of the most common functions, which is f(x) = x².

avatar image xylofiso Cherno · Feb 05, 2016 at 03:15 AM 0
Share

How can I have an object follow that path though? Right now the object just shoots straight on the x axis from the firePoint game object until it goes out of the camera view then the object is destroyed. How do I code in a parabola arc so the game object follows that?

Show more comments
avatar image xylofiso · Feb 05, 2016 at 06:50 PM 0
Share

@Raresh thanks for the negativity, if no one is allowed to learn by asking questions how are they suppose to accelerate in the field? @Chemo I am still not following you, I don't want the position to be instantly transformed, I want it to gradually move to that position, so maybe something along the lines of a function that has a variable += variable.transform.position? I have created a program using a tutorial found on http://catlikecoding.com/unity/tutorials/graphs/ but all this does is create 10 points and lays them out in a parabola, I don't understand how I could apply this to actually get an object to move along that path.

avatar image Cherno xylofiso · Feb 06, 2016 at 12:13 AM 0
Share

I have to agree with Raresh in so far as a basic understanding of $$anonymous$$thematic functions is pretty much required for serious game program$$anonymous$$g.

Anyway, I feel like you need to do some more researching. What do you need to know? 1. How to move a GameObject from A to B smoothly, over time. For this, you can use Vector3.Lerp, for example. 2. How to make the object not go in a straight line, but rather in a curve. For this, you need the math know-how.

Here is a function I wrote long ago in UnityScript that could serve as inspiration. I quickly conveted it to C# without testing but the math is correct.

 public IEnumerator TravelInCurve(Vector3 targetPos, Transform tr) {
 float t = 0f;
 float distance = Vector3.Distance(tr.position, targetPos);
 float speed = 12f;
 while(t < 1) {
      tr.position = Vector3.Lerp(tr.position, GetQuadraticCoordinates(t, tr.position + Vector3.up * 1.4f, Vector3.Lerp(tr.position, targetPos, 0.5f) + Vector3.up * (Vector3.Distance(tr.position, targetPos) / 2.0f), targetPos), t);
      t += Time.deltaTime / distance * speed;
      yield return null;
 }
 }
     
     public Vector3  GetQuadraticCoordinates(float t, Vector3 p0, Vector3 c0, Vector3 p) : 
     {
         return $$anonymous$$athf.Pow(1 - t, 2) * p0 + 2 * t * (1 - t) * c0 + $$anonymous$$athf.Pow(t, 2) * p1;
     }
 

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Eno-Khaon · Feb 01, 2016 at 04:47 AM

Ahh, trajectory prediction. It's as fun as always.

Sorry to disappoint, but I'm simply going to provide you with "a link to some website" -- rather, a previous answer I gave on this particular topic.

http://answers.unity3d.com/questions/1087568/3d-trajectory-prediction.html#answer-1087707

All the calculations are based around a 3-Dimensional trajectory, but the process of calculating trajectory involves breaking it down into only a horizontal and vertical axis. If it all makes sense, then it should be trivial to simplify the calculations for a 2-axis system.

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

Answer by Bonfire-Boy · Feb 05, 2016 at 09:28 AM

If there's no drag and non-zero constant gravity, then ballistic objects follow parabolas naturally. I suspect that's why you want it to follow a parabola.

Unity contains a physics system just so that you can do this stuff, so you can focus more on creativity and not have to get bogged down in the Maths.

So, I think you should at least consider using the physics system, as Cherno suggested. And if you do so then the answer to the question "How can I make a player fire a projectile so that it follows a parabola?" is just this:

  1. Set drag to zero and gravity non-zero

  2. Apply a force to the object

You say that you don't need to know exactly where the object is going to land, in which case you could set the size of force applied by trial-and-error. If you did need to know exactly, then the stuff in Eno's answer looks like it'd do the job.

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

Answer by Arcade-Perfect · Feb 06, 2016 at 02:18 PM

You can find information about Bezier Curves, if you are not good at math, I suggest you to buy a plugin from the Asset Store like this one: Path Definition by Guille Bauza

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

Answer by TreyH · Feb 13, 2016 at 03:01 PM

Gravity will already give you this?

 // Defaults
 Vector2 direction = Vector2.one.normalized;
 float magnitude = 10f;
 
 // Add the stuff
 Rigidbody2D rb = GetComponent<Rigidbody2D> ();
 
 // Add force
 rb.AddRelativeForce(direction * magnitude, ForceMode2D.Impulse);

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

39 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

Related Questions

Creating a 2d arrow projectile in an arc that always hits its moving target 2 Answers

Arcing projectile using physics 0 Answers

How to shoot a bullet and animate at the same time??? Its a 2D shooter c# 3 Answers

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

Arrow arc in 2D 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