how can make my player to jump like parabola
my character has to travel like this.whenever it lands on ground.
Answer by ZefanS · Dec 17, 2015 at 05:57 AM
Given that you are using a Rigidbody component on your player, thus utilizing the Unity physics engine, making an object move parabolically is fairly straight forward. Under only the influence of gravity, any projectile will travel in a parabolic arc. (See here for more info.) What you'll need to do is give the impulse when the player hits the ground in order to keep them traveling in this way.
In Unity, this can be achieved using the Rigidbody.AddForce() function. This function requires a vector pointing in the direction we wish the force to act in. That can be calculated according to the following diagram:
This is done in 2D for the sake of simplicity. The player's current position is given by (x,y). We want to impart a force at some angle θ to the ground. To do so, we need to calculate the vector leaving the player's position at that angle. Mathematically, this is the vector result of (x+a, y+b) - (x, y). We can solve for a and b using θ (assuming the hypotenuse of the triangle is of length 1), as such: a = cos(θ) and b = sin(θ).
Here is a simple implementation of this in Unityscript:
#pragma strict
public var theta : int;
public var power : float;
private var rb : Rigidbody;
private var thetaRad : float;
private var a : float;
private var b : float;
private var aim : Vector3;
private var dir : Vector3;
function Start()
{
rb = GetComponent.<Rigidbody>();
}
function OnCollisionEnter(collision : Collision)
{
if (collision.transform.tag == "Ground")
{
thetaRad = theta / 180.0f * Mathf.PI;
a = Mathf.Cos(thetaRad);
b = Mathf.Sin(thetaRad);
aim = Vector3(transform.position.x + a, transform.position.y + b, transform.position.z);
dir = aim - transform.position;
rb.AddForce(dir * power);
}
}
This script should be attached to a GameObject with a Collider and Rigidbody. Note that the surface that the object will bounce on must be tagged "Ground" and have a Collider attached for this to work. Because the object retains its momentum through each bounce, the arc of the parabola will tend to widen the more the objects bounces. This can be counteracted by setting the Drag of the Rigidbody to some value above zero (for instance 0.1). By changing the values of theta and power you can adjust the shape of the parabola.
I thought of another way of doing this without using the physics engine but instead calculating the coordinates directly. If we want to calculate how the object will jump in a parabolic shape from one point to another, (provided that the points are at the same height) we can think of them as the roots of the parabola. Thus, given an initial (a,b) position and a distance we wish to jump, we know the other root will be (a',b), where a' = a + the jump distance. Thus we can figure out the equation that will give us all the coordinates in between.
That equation is : y = -x^2 + (a+a')x - aa' + b where b is the initial y value.
Then all we need to do is feed in the x-values and get the y-values, and we can move the object through the parabolic arc over a number of frames. The following script implements this method:
#pragma strict
public var range : float;
public var numberOfFrames : int;
private var x : float;
private var xx : float;
private var y : float;
private var a : float;
private var b : float;
private var c : float;
private var currentX : float;
private var currentY : float;
private var xDivision : float;
function Start()
{
Parabola();
}
function Parabola()
{
x = transform.position.x;
xx = transform.position.x + range;
y = transform.position.y;
a = -1.0f;
b = x + xx;
c = -x * xx;
xDivision = range / numberOfFrames;
for (var i = 0; i < numberOfFrames; i++)
{
currentX = x + i * xDivision;
currentY = a * Mathf.Pow(currentX,2) + b * currentX + c + y;
transform.position.x = currentX;
transform.position.y = currentY;
yield;
}
}
There are two public parameters: range and numberOfFrames. range determines how far the object will travel. numberOfFrames determines how many frames the object will take to move through the parabola. It can be thought as the inverse of the speed that the object will travel.
This method currently only works in 2D, but could be generalized to 3D by calculating the 2D trajectory coordinates and transforming them based on the direction the object should travel relative to the global coordinate axes.
Also note that this does not control for the maximum height of the object, and thus it will change according to the range.
Hope this helps.
the script is not working. i mean according to my requirement. .... i have fixed length of jumping...... and the player is traveling only left to right.
Okay, well maybe if you modified it to meet your needs it would work. Also, note that you didn't state those requirements in the question.