This site uses strictly necessary cookies. More Information

X- Home /

**closed**Jul 07, 2020 at 10:54 PM by JMEJAY for the following reason:

Problem is not reproducible or outdated

# [Maths] How to project a curve in two axis? (Grav y + RelativeVelocity x)

Hello,

I am trying to make an object follow a curved line, and I am using the equation

*y = ax^2 + bx + c*

but I am also trying to affect this by a relative velocity value to give it extra curve in the x.

Currently I am inputting these values:

= Gravity (9.81)*a*= time*x*= power (5)*b*(start pos of object.y - 0)*c*relativeVelocity*d*`void Update () { //relativeVelocity is found by mouseDelta.x DrawMyLine(); } void DrawMyLine() { // y = ax^2 + bx + c + d // a = grav x = time b = power c = start pos d = relativeVelocity ballLine.SetVertexCount (rpg_exp); Vector3[] posArray = new Vector3[rpg_exp]; float a; float b; float c; float d; float t = 0.0f; float dt = 0.2f; float gravity = Physics.gravity.y; float lineRelVel = relativeVelocity.x; for (int i = 0; i < rpg_exp; i++) { a = (gravity/2) * (t*t); b = velocity.y * t; c = ballStart.y; posArray[i].y = a + b + c; b = velocity.x * t; c = ballStart.x; d = lineRelVel * t; // I added this component, but it is wrong. posArray[i].x = b + c + d; lineRelVel += lineRelVel; // What am I doing... b = velocity.z * t; c = ballStart.z; posArray[i].z = b + c; ballLine.SetPosition(i,posArray[i]); t += dt; } }`

Thanks for your time.

**Answer** by Orann
·
Feb 05, 2015 at 07:21 AM

It looks like you might want to be using Vector3's instead of flat floats for your equation there, like this. A Vector3 represents a position or velocity - in this case the vector for gravity is down so it looks like this: (0, -9.8, 0). It's good practice to use the built in references to things like gravity and time, so that your code is consistent with Unity's internal physics.

```
Vector3 a = Physics.gravity;
Vector3 b = new Vector3(10f, 60f, 0f);
Vector3 c = new Vector3(0f, 0f, 0f) //start pos
float x = Time.deltaTime; //This is how you generally get time in Unity - this will give you the time since the last frame
Vector3 CurrentPos = a * (x * x) + b * x + c;
```

**Answer** by fafase
·
Feb 05, 2015 at 05:02 AM

Considering you have gravity in your equation, I would think you are after projectile motion:

http://en.wikipedia.org/wiki/Projectile_motion

In this case, you use:

```
y = originalForce * t * sin(angle) - 0.5 * gravity * (t * t);
```

the other way is to use AddForce with gravity enable, and let the engine do this computation.

**Answer** by emc233
·
Feb 05, 2015 at 05:10 AM

Translate does not do what you think. Search for transform.translate in this doc: http://docs.unity3d.com/ScriptReference/index.html

this code is in csharp:

```
using UnityEngine;
using System.Collections;
public class phy : MonoBehaviour {
private Vector3 initial_postition;
private float start_time;
float a;
float b;
float c;
// Use this for initialization
void Start () {
start_time = Time.time;
initial_postition = this.transform.position;
a = 9.81f;
b = 5;
c = 0;
}
// Update is called once per frame
void Update () {
float x = Time.time - start_time;
float y = a * x * x + b * x + c;
this.transform.position = new Vector3(x, y, 0);
}
}
```

**Answer** by moonstruck
·
Feb 05, 2015 at 09:18 AM

Hello! The equation you use is more of an accelerated straight movement where a is acceleration, b is initial velocity and c is starting position. If you want to move your object through parabolic trajectory then you can use the formula differently. First of all lets parametrize it using `x = t`

. Thus we have `y = a*t^2 + b*t + c`

. `z`

should remain constant if you want the trajectory to be flat. Beware, `a`

, `b`

and `c`

don't mean the same if you use the formula to define a trajectory, they define different forms of parabolas. The code can be rewritten:

```
private float t = 0;
void Update() {
Vector3 newPos = new Vector3( t, a*t*t + b*t + c, 0 );
transform.position = newPos;
t = t + Time.deltaTime;
}
```

You can experiment with different values of these parameters: `a`

, `b`

, `c`

and initial `t`

too, they can be negative like `t = -1`

.

Thanks for this. I wonder if you can help me once more, in that my parabola needs to take into account the objects relativeVelocity on the x axis. I can't quite figure it out. I imagine it is just another component to the equation.

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

move object with parabola 0 Answers

Parabolic movement doing strange things with certain angles 1 Answer

How to calculate xSpeed and ySpeed according to float value of rotation? 1 Answer

Firing A Projectile Along A Parabola 1 Answer

How to simplify the equation of a moving vertex if its object is squeezed? 0 Answers