- Home /

# 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#.

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.

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.

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

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?

@$$anonymous$$aresh 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.

I have to agree with Raresh in so far as a basic understanding of Mthematic functions is pretty much required for serious game programming.

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 Mathf.Pow(1 - t, 2) * p0 + 2 * t * (1 - t) * c0 + Mathf.Pow(t, 2) * p1;
}
```

**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.

**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:

Set drag to zero and gravity non-zero

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.

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

**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);
```

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

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

How do I stop a 2D projectile when it reaches a point 3 Answers

how to draw a aiming line 1 Answer

How to make a projectile in the finger/mouse position in a 2D endless runner? 0 Answers

How do I make a projectile arc and always hit a moving target? 3 Answers