Drawn trajectory different than real trajectory

Hi all,

I’m trying to draw the trajectory of a projectile (aka Angry bird). I managed to do it, however once I launch the projectile it doesn’t exactly follow the trajectory, and I don’t manage to find from where comes the difference…

Here is the code I’m using:

void OnDrawGizmos(){
		GameObject player = GameObject.FindWithTag("Player");
		
		float h = 0.0f;
		
		while(player.transform.TransformPoint(PlotTrajectoryAtTime(h)).y >= 0){
		Gizmos.DrawSphere(player.transform.TransformPoint(PlotTrajectoryAtTime(h)), 0.2f);
		h = h + 0.1f;
		}
			
	}


public Vector3 PlotTrajectoryAtTime (float t) {
    	return  startVelocity*t + Physics.gravity*t*t*0.5f + startPoint;
}


public void Update(){
if (Input.GetKeyUp(KeyCode.L)){
		GameObject player = GameObject.FindWithTag("Player");
		GameObject.Find ("Rock").transform.position = player.transform.TransformPoint(0,startPoint.y,startPoint.z); //the object to launch
		
		Vector3 force = player.transform.TransformDirection(startVelocity); // to launch the rock in the good direction
			
		GameObject.Find("Rock").transform.rigidbody.velocity = force;
	}
}

However the trajectory drawn in the Editor is “higher” than the real trajectory followed by the rock… amy idea?

Cheers,

Nicolas

Game physics and real world physics are different. Game physics just estimates curved flight using straight line movements over 50 frames/sec. Each line will be a tiny bit over or under the actual curved line, but very close. Unity apparently applies gravity first.

You could move the thrown rock using the real physics formula, then “release” it as a rigidbody when a spherecast or something detected an imminent crash. Or, I think you could tweak the real calculation for your curve to match Unity’s. Maybe subtracting 1/2 a frame’s worth of gravity from the starting amount might give consistent results over various arcs.

I’m having the same problem with the calculated trajectory “higher” than actual trajectory. The difference is consistently about 7% of the “jump” height. I checked the formulas many times and tried various implementations always with the same result. It’s not the scale and there is no air drag. Artificially changing the initial velocity and/or direction even by a tiny bit only makes things worse.

If I cannot find a fix I will have to implement my own physics replacement as my game relies on EXACT trajectory, as the trajectory is calculated by the game and cannot be influenced by the player in my case.