Basic projectile trajectory with transform

I think I have my formulas right. I want to take a sphere g.o. use an initial x, y value and gravity to calculate launch angle and move the sphere in the typical parabolic arch across the screen. The launch angle calculates just fine, but the sphere does nothing, just sits there. Any ideas?

I press the code (101010) format button, but it only formats my first line of code?! Any ideas on that too?

``````var xInitial: float = 10;

``````

var yInitial: float = 10; var x: float; var y: float; var gravity: float = -9.8; var myRigidBody: Rigidbody; var theta: float;

theta = (Mathf.Atan(xInitial / yInitial)*180)/Mathf.PI;

function FixedUpdate () {

if ( y > 0) {

``````x = xInitial * Time.deltaTime;
y = yInitial + (.5 * gravity * (Time.deltaTime * Time.deltaTime));
transform.position = Vector3(x, y, 0);
``````

}
}

function OnGUI () { GUI.Label(Rect(3,20,100,100),"theta = " + theta.ToString()); }

I am not sure why you have to calculate the trajectory on your own. Is there something wrong with Unity's physics engine? I would try something like:

``````var force = 10;
var myRigidBody: Rigidbody;

var angle = 50;

function Start () {

myRigidBody = rigidbody;

}

function OnGUI () {

if(GUI.Button(Rect(10, 10, 150, 75), "Fire")) {
Fire ();
}

}

function Fire () {

transform.eulerAngles.x = -angle;
myRigidBody.velocity = transform.forward * force;

}

``````

and I played with your code some, and here is what I could come up with and keep your code in tact. Your problem seemed to be that you continued multiply your x and y by x/y initial which does not change, so your position would not change. Also, you might have been meaning to multiply by Time.time. Time.delta time is 1/FPS which is not the steady incrementing you want to get a consistent parabolic shape, while Time.time is a much more gradual increment.

One other thing I noticed is that there is no offset of the vertex based on position so the sphere will not travel the whole arch but you probably want to put the vertex forward of the ball's starting position, so that you can see the full parabola. I left that for you to decide how to do because there are multiple ways.

``````var xInitial: float = 10;
var yInitial: float = 10;
var x: float;
var y: float;
var gravity: float = -9.8;
var theta: float;

function Start () {

theta = (Mathf.Atan(xInitial / yInitial)*180)/Mathf.PI;

x = xInitial;
y = yInitial;
}

function FixedUpdate () {

if ( y > 0) {
x += Time.deltaTime; //goes up 1 every second.  Multiply to increase the speed.
y += (.5 * gravity * (Time.deltaTime * Time.deltaTime)); // You probably either want to use x * x or Time.time * Time.time.
transform.position = Vector3(x, y, 0);
}

}

function OnGUI () {
GUI.Label(Rect(3,20,100,100),"theta = " + theta.ToString());
}

``````