c# - How to Draw Projectile Trajectory with Unity3d's built-in Physics Engine? -
i working on game fling projectile reacts physics forces. using built in unity physics in game, drawing trajectory of projectile before launched. so, i'm using linerenderer , simple physics class rolled compute positions:
public class slingphysics { private vector3 halfgravity; private float forcemultiplier; public void init(vector3 gravity, float slingforce, float mass, float drag) { halfgravity = gravity / 2.0f; forcemultiplier = slingforce / mass / (1 + drag); } public vector3 getposition(vector3 start, vector3 direction, float timedelta) { vector3 pos = direction * (timedelta * forcemultiplier); pos.y += (halfgravity.y * timedelta * timedelta); return start + pos; } /// <summary> /// computes array of trajectory object positions time. /// </summary> /// <returns>number of positions filled buffer</returns> /// <param name="startpos">starting position</param> /// <param name="direction">direction (and magnitude) vector</param> /// <param name="timeincrement">time increment of samples</param> /// <param name="yfloor">minimum height, below clipped</param> /// <param name="positions">buffer fill positions</param> public int getpositions(vector3 startpos, vector3 direction, float timeincrement, float yfloor, vector3[] positions) { int maxitems = positions.length; int = 0; (; < maxitems; i++) { vector3 pos = getposition(startpos, direction, timeincrement * i); if (pos.y < yfloor) break; positions[i] = pos; } return i; } }
this works great , (somewhat surprisingly) matches actual trajectory projectile ends traveling when launched , governed unity physics engine.
but now, decided introduce drag on rigidbody , trajectory no longer perfect. continue try change physics model match unity doing, seems losing battle.
is there way precompute , draw trajectory using unity physics engine?
if not, how drag , angular drag implemented mathematically in unity physics?
is there documentation source unity's (5.3.4) physics implementation?
i continue try change physics model match unity doing
no, don't it. whenever unity updated, physics stuff break. example, unity 4 , 5 physics not the-same. had heavily modify unity 4 physics code make work properly.
the easiest solution problem not calculate @ all. way create two projectiles. projectile1 main projectile can seen , shot. projectile2 hidden projectile can't seen player.
on drag mode, shoot projectile2(hidden) store path or position(vector3) has traveled through in list
. use stored path draw projectile trajectory
.
you need shoot projectile2(hidden) once , store path. if during drag mode, finger, mouse or drag moves position, have clear list
, re-shoot projectile2(hidden) once again, store position list
, update projectile trajectory
again.
when finger released, can shot projectile1(main projectile) visible player.
if using linerenderer
, number of setvertexcount
the-same number of list.count
. drawing lines can done looping on positions in list
.
the result the-same no matter how many times unity updated.