I’m not the best at math either… I’d probably use iTween: see here (accurate lob). iTween is free and you could buy the accurate lob example for a dollar, if it’s close to what you’re looking for.
What I would do if I were you is lerp the projectile’s x-z values to the desired position, and set the y position based on that. In my example below, I used the sine function to calculate y position(since it has the shape of a trajectory.
Completely untested, my math and code might be (probably is) off.
var speed : float = 1.0; //how fast the projectile travels
var target : Transform; //what the projectile tries to hit
var trajectoryHeight : float = 10.0; //the peak of the trajectory
private var startPos : Vector3; //the position we started at
private var lerpPos : float = 0.0; //the lerp position we're at
function Start(){
startPos = transform.position;
}
function Update(){
lerpPos = Mathf.Clamp(lerpPos + Time.deltaTime * speed, 0.0, 1.0); //lerp values need to be between 0 and 1
transform.position = Vector3.Lerp(startPos, target.position, lerpPos);
transform.position.y = trajectoryHeight *Mathf.Sin(lerpPos * Mathf.Pi); //multiplying by pi gives it a cycle of 2 - we'll only use half a cycle
}
As said, I haven’t tested that at all. It may not compile, and it may not work if it does compile. It should be enough to get you started though.
You’ll probably also want to add some damping to lerpPos so it’ll slow down as it goes upwards and speed up as it goes down.
Lerping causes the trajectory to be circular, where as real trajectory creates a parabola.
I found this. It has a whole bunch of ‘working’ code at the bottom, but I have no idea how to implement it. It doesn’t seem to explain itself much. Maybe I just lack the common knowledge.
I cheated a bit to make a lob effect, but it works… my set up:
my turret is set at a 45 degree angle always… but is free to rotate around…just elevation is frozen
I have a max power setting
Max range… at max power the round lands at max range
I detect targets within max range… get their distance from turret
Use the formula
Power = 1-(distance / maxDistance)*maxPower;
Since my elevation is always a 45… the. Projectile lobs… and the turret lowers the power put into each shot to hit pretty accuratly.
Kind of a hack, but works for what I wanted… and fairly simple…
If you need any code example let me know…can post later
Just looked on wikipedia - Projectile motion - Wikipedia
Under - Angle θ required to hit coordinate (x,y)
can’t you just use that? And use a sensible initial velocity value.
vel = up to you
range = up to you
alt = up to you
g = gravity = 9.81
vel2 = vel * vel
vel4 = vel * vel * vel * vel
dis2 = distance * distance
num = vel2 + sqrt(vel4 - g * (g * dis2 + (2 * alt * vel2)))
dom = g * distance
angle = atan(num / dom)
I think theres a few checks you have to do like it wont hit the point if the (vel4 - g * (g * dis2 + (2 * alt * vel2)))
is negative.
what’s wrong with the code I gave you above?
here is a webplayer example of it.
right click to rotate the turret, hold left click to launch with power.
why not use a parabolic equation to solve your problem that is what it was made for… Just google parabolic equation and start putting in variables to make it work…
It is not clear from your post what you are doing.
If you want the projectile to hit a sertain position, then you need to solve the equation for an initial velocity.
The initial velocity is a vector with direction and magnitude. Keep one fixed and solve for the other.