- Home /
simple trajectory for rigidbody
Hello. I am trying to implement a sort of simple projectile trajectory system for my AI. Currently, it's using a quick n dirty Transform.LookAt for the targets position. I would like the upwards angle ( the x axis, it's a 2D game) to increase by X amount based on the distance from the target. I want to create a type of arch looking movement. Also, should I use addforce if I want it to be accurate? or something more like a Lerp?
@Fattie, simply because Unity will handle physics for you, doesn't mean that you don't need to know how hard to throw it.
Answer by Peter G · Jan 21, 2012 at 01:25 PM
There are a number of formulas you can use for simple projectile motion. The trick is you have to figure out what variables you want to vary. If you strictly want to vary the angle then you can use one formula:
Given:
Distance from target = R
angle Theta;
Velocity.magnitude = v
gravity = 9.8f
v^2 * sin(2Theta) / 9.8 = R
Solve for Theta:
Theta = asin(dg/v^2)/2
That's about as simple as you can make it. In code terms, it looks more like this:
var velocity : float;
function Launch () {
var theta : float = 0;
var distance = (target.position - lauchPosition).x;
theta = .5 * Mathf.Asin(distance * 9.8 / velocity / velocity);
rigidbody.AddForce( velocity * new Vector3(Mathf.Cos(theta), Mathf.Sin(theta), 0) , ForceMode.VelocityChange);
}
Thank you very much! I will experiment with these today! and I'm not the best at $$anonymous$$ath so this helps :p
After some further testing, the code works, sort of.... The current code has 2 problems. One of them is relatively easy to fix, the other is harder.
Asin always returns a value from -pi/2 to pi/2. Now, you'll notice that's always on the right side of the unit circle, meaning, it can't shoot anything to the left. There's an easy fix for this. Subtract theta from pi based upon the sign of distance (notice this is a "g" sign as opposed to sin).
The second problem is a little harder. There is a $$anonymous$$imum velocity required to reach the target. Now if you have a fixed target, that's easy to solve. But, if you target moves as I assume it does, then it requires another variable to vary which makes the math a little more difficult.
I'll see what I can come up with for the second problem. You should be able to fix the first on your own though.
1 solution to the second problem is to set the velocity ins$$anonymous$$d of the angle. Choose the launch angle then you can figure out the velocity that will always give you that angle.
Answer by ziC · Jan 21, 2012 at 04:35 PM
// Animates the position in an arc between sunrise and sunset.
var sunrise : Transform;
var sunset : Transform;
function Update () {
// The center of the arc
var center = (sunrise.position + sunset.position) * 0.5;
// move the center a bit downwards to make the arc vertical
center -= Vector3(0,1,0);
// Interpolate over the arc relative to center
var riseRelCenter = sunrise.position - center;
var setRelCenter = sunset.position - center;
transform.position = Vector3.Slerp(riseRelCenter, setRelCenter, Time.time);
transform.position += center;
}
http://unity3d.com/support/documentation/ScriptReference/Vector3.Slerp.html
this is not really related to what he is asking about.
Your answer
Follow this Question
Related Questions
2D projectile trajectory prediction 2 Answers
Unity Messing up Basic Math? Projectile Trajectory Algorythm 1 Answer
projectile affected by parent object 1 Answer
Projectile trajectory 1 Answer