Button launched projectile usaing transforms

I apologize for asking yet another projectile motion question. I have been asked by my boss to use transforms instead of rigidBodies for a basic physics example using initial velocity and angle on an object. I think I have the math down… when I put the code into function FixedUpdate() the sphere gameobject launches in an arch just as expected and the coordinates echoed to the console checked out.

Now I have added a slider for velocity/angle and a button to apply the slider values to the sphere game object. To adjust for Time.time having a head start before the button is pressed, I set a newTime = Time.time var after the button is pressed, then use (Time.time - newTime)everywhere I would use Time.time.

Unfortunately, the sphere just jumps to a y of 9.98 and stays there. I put the transform in if ( y > 0 ) hoping it would perform the displacements calculations until the sphere reached the ground plane again.

Thank you for any help.

// Initial Velocity
var xInitial: float;
var yInitial: float;
// Initial Positions
var x: float = 0; 
var y: float = 10; 
var yStart: float; 
//Gravity 
var gravity: float = -9.8; 
var theta: float;
var r: float;
var velMagnitude: float = 0;
var newTheta: float = 0;
var newTime: float = 0;


function Start () {
		var yStart = y;
		}      
         
function FixedUpdate () {

         }         

function OnGUI () { 
	//-----------------SLIDERS--------------------------------//
	// Initial Velocity
	GUI.Label(Rect(3,300,200,50), "Initial Velocity");
	velMagnitude = GUI.HorizontalSlider(Rect(3,320,200,20),velMagnitude,0.0,500.0);
	GUI.Label(Rect(220,320,50,20), velMagnitude.ToString());

	// New theta slider
	GUI.Label(Rect(3,360,200,50), "Theta");
	newTheta = GUI.HorizontalSlider(Rect(3,380,200,20),newTheta,0.0,90.0);
	GUI.Label(Rect(220,380,50,20), newTheta.ToString());
 	   	
	//-----------------BUTTONS--------------------------------//
	
	if (GUI.Button (Rect (3,430,105,50), "Launch")) {
		
		// Create time offset to start at a time of zero
		newTime = Time.time;
		
		// Move object while y is above ground plane
		if ( y > 0 ) {				
		
			// Convert theta and r vector to x y vector 
			xInitial = velMagnitude * Mathf.Cos(newTheta * Mathf.PI/180);
			yInitial = velMagnitude * Mathf.Sin(newTheta * Mathf.PI/180);

			// Displacement x,y
			x = xInitial * (Time.time - newTime);
			y = yStart + (yInitial * (Time.time - newTime)) + (.5 * gravity) * ((Time.time - newTime) * (Time.time - newTime));  // yi+vi*t-1/2*g*t^2
		
			// Move object
        		transform.position = Vector3(x, y, 0); 
		}
	}
}

	// Reset Location
	if (GUI.Button (Rect (108,430,105,50), "Reset")) {
		transform.position = Vector3(0, 10, 0);	
		}
		


}

Firstly, OnGUI is not ideal for frame updates - you should use FixedUpdate for physics changes and Update for everything else (you should probably use Update here, since you’re not using the physics engine).

Also, you are updating the position of the object inside the “if (GUI.Button(…)” statement, so it will only happen on frames where the button was clicked. Generally, it’s a good idea to use OnGUI to set variables’ values and then make use of those values separately in Update/FixedUpdate.

Ok. Thanks. So I guess I need to set a button pressed bool so fixedUpdate doesn’t just run when I start the program.

Thanks Andeeee.