- Home /
Drawing projectile trajectory
I have been trying to draw the trajectory path of a launched object using a line render but the line never seems to match the actually path the object takes.
I've already spent hours watching physics tutorials and scouring the web for an answer, but nothing seems to work.
This is a 2D game, so all objects are 2D(if that matters). Also I use seconds for the time but instead fixedTimeStep.
Here is pic of the situation.
Here is code
var launchPower : float = 20;
function Update() {
if(Input.GetKeyDown(KeyCode.Space)){
var projectile : GameObject = Instantiate(prefab1,transform.position,transform.rotation);
projectile.rigidbody2D.velocity.x = launchPower;
}
DrawTraject(launchPower);
}
function DrawTraject(var velocity : float;){
var line = this.gameObject.GetComponent(LineRenderer);
line.SetVertexCount(20);
for(var i = 0; i < 20; i++){
//I multiply by Physics.fixedDelta time so it can units/FixedtimeStep
line.SetPosition(i, Vector3(transform.position.x + (launchPower * Physics.fixedDeltaTime) * i, transform.position.y - (Physics.gravity.y * Physics.fixedDeltaTime) * (i*i) /2, 0);
}
}
you also might want to vote for a physics simulation/trajectory prediction feature request here: http://feedback.unity3d.com/suggestions/function-for-manually-simulating
Answer by HappyMoo · Jan 01, 2014 at 02:59 AM
Ok... let's recreate the physics formula, however, if it turns out to be wrong off too much, it may be a better idea to move the projectile yourself.
Also, I assume for now that you have drag=0 - if not, we have to experiment a bit as drag can be inserted at different stages in the process...
I usually write C#, so excuse me if I produce syntax error on the way - I won't test the code:
DrawTraject(projectile.transform.position, projectile.rigidbody2D.velocity);
function DrawTraject(var startPos : Vector2, var startVelocity : Vector2;){
var verts = 20;
var line = this.gameObject.GetComponent(LineRenderer);
line.SetVertexCount(verts);
var pos:Vector2 = startPos;
var vel:Vector2 = startVelocity;
var grav:Vector2 = Vector2(Physics.gravity.x, Physics.gravity.y);
for(var i = 0; i < verts; i++)
{
line.SetPosition(i, Vector3(pos.x, pos.y, 0));
vel = vel + grav * Physics.fixedDeltaTime;
pos = pos + vel * Physics.fixedDeltaTime;
}
}
This looks like it will work. I just tested the following code which is very similar, and it tracked the projectile accurately:
pos = transform.position;
for (var i = 0; i < count; i++) {
lr.SetPosition(i, pos);
velocity += Physics2D.gravity * Time.fixedDeltaTime;
pos += velocity * Time.fixedDeltaTime;
}
'velocity' is the initial velocity of the projectile. This calculation assumes no drag.
I think I found the problem. I replaced the projectiles Rigidbody2D with a regular Rigidbody and it worked PERFECTLY!
Which code does? You mean the physics is different for 3D and 2D?
Note I tested the code I posted (which is the same code that Happy$$anonymous$$oo posted). It worked perfectly in 2D. Take a look at any settings (especially the drag setting) you had in your Rigidbody2D.
Answer by idunlop_oefun · Jan 01, 2014 at 02:10 AM
It's difficult to tell from your example code but have you tried turning on the following property:
line.useWorldSpace = true;
Without that property enabled the line is rendered relative to the game objects position.
Answer by Spinnernicholas · Jan 01, 2014 at 02:33 AM
You have to square time as well as i and it's easier to use vectors for the math.
float t = Time.fixedDeltaTime;
Vector3 Pi = transform.position + new Vector3(launchPower,0,0)*t*i + new Vector3(0,-Physics.gravity,0)*.5*t*t*i*i;
Pi.z = desiredZ;
line.SetPosition(i, Pi);
You could also define the velocity and acceleration vectors beforehand to make it really easy to read:
float t = Time.fixedDeltaTime;
V = new Vector3(launchPower,0,0);
A = new Vector3(0,-Physics.gravity,0);
Vector3 Pi = transform.position + V*t*i + .5*A*t*t*i*i;
Pi.z = desiredZ;
line.SetPosition(i, Pi);
Your solution got me close but it is still off. I;m trying to figure out what it is but I'm no physiyst Here is a pic
No, wait.. that was too fast...We're using Physics.fixedDeltaTime, so we are doing everything that FixedUpdate would do... doesn't matter where the code runs.
I assume Physics.fixedDeltaTime == Time.fixedDeltaTime - I'm new to unity...
It might be because there is drag. Check the physics settings to see if there could be any other forces.
Answer by Ankit Priyarup · Jan 10, 2014 at 10:53 AM
http://youtu.be/ZCDiTxBT1nE This is the script created by me i hope this will be helpfull
Answer by thaitd1985 · Mar 06, 2016 at 08:05 PM
Hi @ninjaboynaru, Do you resolve this issue. I got same issue like you. I replaced with rigidbody2d by rigidbody and it worked perfectly. I don't know what is wrong with rigidbody2d