- Home /
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()); }
Answer by Peter G · May 18, 2010 at 08:53 PM
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()); }
Hopefully something here will help you.
Thanks Peter G! I was using the Unity physics engine, but for the project I'm working on, I was asked to use transforms. It's a physics lesson for kids.(I probably shouldn't be the one writing it:) ) Thanks again, I'll be back in a few to add the check.
Thanks again, and what is "offset of the vertex"? You are right, it doesn't ever go back down it just goes up to y initial and slowly tracks down the x coordinate. the function for displacement in the y direction y += .5t^2, should take care of that, shouldn't it? Should I ask this as another question?
y does go down, I took time out of the equation and it dropped. Now I have to figure out how to get it to launch and follow the arch, ins$$anonymous$$d of going directly to it's highest point and then dropping. I should probably go back and take high school physics again too :)
Well, I'm not a physics expert, but your function as you describe sounds like a linear equation. What is your equation? And by offset the vertex, I meant push it forward, so you can see the rising and the falling of the arch.
Remember high school, y = scale(x - horizonatalShift)2 + verticalShift. You want to add a horizontal shift. Your current equation takes almost no rewriting. y = .5 gravity (Time - horizontal shift) ^ 2.
Thanks again for all your help. Here's what I ended up with and it appears to work based on the values I print to the console. Thanks again Peter G! x = xInitial Time.time; //yi+vi*t-1/2*g*t^2 y = (yInitial Time.time) + (.5 gravity) (Time.time * Time.time); //Print values to console for checking print ("x = " + x); print ("y = " + y);