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