Playback speed position vs translate

I think my problem is in my understanding of transform.translate. My script launches an object but doesn’t use the physics engine. I have two sliders with an initial velocity and launch angle. I take those two values and calculate the x and y vector and then I use:

transform.position = Vector3(x, y, 0);

It worked but playback was really slow. I want it to look similar to real life. Someone suggested using transform translate instead, so I replaced the above with:

transform.Translate(Time.deltaTime * x, y, 0);

Translate definitely moves the object faster and it looks like a ball being launched, but weird things are happening.

The same calculation for y displacement yields different results for position and translate:

y = yStart + (yInitial * (Time.time - newTime)) + (.5 * gravity) * ((Time.time - newTime) * (Time.time - newTime));  // yi+vi*t-1/2*g*t^2

When used in transform.position, the y values grow and shrink and the object goes up and then down. When I use translate, y starts at 10 and shrinks but the ball still goes up and then down. How would using transform.translate change the value in another line of code?

transform.translate acts the same regardless of what my input velocity and angle are. Straight up and straight down even though the y values start at 10 and go straight down.

Here’s the whole file:

var xInitial: float; 				// Initial x Velocity
var yInitial: float; 				// Initial y Velocity
var x: float; 	 		 
var y: float;
var yStart: float; 					// Initial y position
var gravity: float = -9.8; 
var theta: float;					// Launch angle
var r: float;						// Magnitude vector
var velMagnitude: int = 0;			// Launch Vector
var newTheta: int = 0;				// Slider launch angle
var newTime: float = 0;				// Adjustment to bring time to zero once button is pressed
var isPressed: boolean = false;
var timeReset: boolean = false;
var myRigidbody : Rigidbody;

function FixedUpdate () {
	// Check if adjustment needs to be made for Time.time the game clock which is always running
	if (timeReset == true) {
		// Reset time to 0
		newTime = Time.time; 			//used for the time value in the displacement formulas
		timeReset = false;
		yStart = transform.position.y;  //Get initial y position of object for displacement calculations
		}
		
	if (isPressed == true) {
						
			// 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);
			transform.Translate(Time.deltaTime * x, y, 0);
		} else { 
		isPressed = false;
		}
}   


function OnGUI () { 
	//-----------------SLIDERS--------------------------------//
	// Initial Velocity
	GUI.Label(Rect(3,300,200,50), "Initial Velocity");
	velMagnitude = GUI.HorizontalSlider(Rect(3,320,200,20),velMagnitude,0,100);
	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,90);
	GUI.Label(Rect(220,380,50,20), newTheta.ToString() + "°");
 	   	
	//-----------------BUTTONS--------------------------------//
	if (GUI.Button (Rect (3,430,105,50), "Launch!")) {
		isPressed = true;
		timeReset = true;
	}

		// Reset Location
	if (GUI.Button (Rect (108,430,105,50), "Reset")) {
		transform.position = Vector3(0, 10, 0);	
		isPressed = false;
		x = 0;
		y = 10;
		z = 0;
		transform.rotation = Quaternion.identity;
		}
		
	//------------------DEBUGGING------------------------------//
	GUI.Label(Rect(3,20,200,100),"isPressed = " + isPressed.ToString());
	GUI.Label(Rect(3,30,200,100),"velMagnitude = " + velMagnitude.ToString());
	GUI.Label(Rect(3,40,200,100),"theta = " + newTheta.ToString());
	GUI.Label(Rect(3,50,200,100),"xInitial = " + xInitial.ToString());	
	GUI.Label(Rect(3,60,200,100),"yInitial = " + yInitial.ToString());	
	GUI.Label(Rect(3,70,200,100),"x = " + x.ToString());
	GUI.Label(Rect(3,80,200,100),"y = " + y.ToString());
	GUI.Label(Rect(3,90,200,100),"time = " + (Time.time - newTime).ToString());
	GUI.Label(Rect(3,100,200,100),"yStart = " + yStart.ToString());
	//GUI.Label(Rect(3,110,200,100),"ySpeed = " + rigidbody.GetPointVelocity(myRigidbody).ToString());
	}

I would like to just use the physics engine, but I’ve been asked to move it point by point. I’m mainly after it moving faster, whether it’s with position or translate.

Thank you for reading this and any help you may have,
Robert

In this line:-

transform.Translate(Time.deltaTime * x, y, 0);

…you don’t need to multiply the X value by Time.deltaTime because it is already based on a time offset. The result of using it would be that you get very small motion in the X direction compared to Y.

Thanks again Andeeee, I really appreciate all the help you have given me over the past few months.

In reference to the:

transform.Translate(Time.deltaTime * x, y, 0);

Translate is already has based on a time offset, are the values placed in there velocity?

Thank you again,
Robert[/code]

My explanation was woefully inadequate. Allow me to elaborate…

Speed is calculated as distance / time, so multiplying speed by time will give you distance. In the Update function, you need to know how much to move the object on a given frame, so you need to know the speed and the time since the last movement. The time is given by Time.deltaTime, so you often see things like:-

transform.Translate(Vector3.forward * speed * Time.deltaTime);

…which moves the object forward with a given speed.

If the trajectory of the object can be calculated at any point using a function, generally you don’t use these position deltas on each frame. Rather, you set the position of the object exactly using the value obtained from the function. The input to the function is usually the elapsed time since the start of the motion. This is calculated by taking the current time each frame and subtracting the start time. Some functions require the “normalised” time, which is the elapsed time divided by the total time the motion is supposed to take (the result being a fraction between 0 and 1). Since you are setting the position directly, you set the transform.position value instead of using transform.Translate:-

transform.position = TrajectoryFunction(elapsedTime);

In your current code, there seems to be some confusion as to which approach you are using. The start time is recorded in newTime and then each frame, the elapsed time is calculated with Time.time - newTime. However, you then call transform.Translate with a value multiplied by Time.deltaTime. It is more likely that you want the trajectory-based approach for what you are doing. If that is the case, you will need to set the position directly.