unity3d - Projectile trajectory / flight path -
i'm trying make projectile has movement behaviour shown red in diagram. know , have 2 vectors; start , end. end goal have randomness of arc @ iteration , projectile velocity change in lerp-fashion. i've done linear movement generations before, nothing this.
if question feels asking work me (my usual fear of asking questions novice coder) have tips , hints on methods/commands should into? language c# , unity version 5.6
edit # 1
after getting head-direction achieve closer end-goal function of this.
blue linear line representation of distance , angle between a(initiation point) , b(target). red arc trajectory i'm willing make projectile move as.
fortunately, figured wanted path guide projectile follow cubic bezier , got result in editor shown in diagram above a, b, moda, , modb. there few more things need working on mounting projectile follow path , control velocity , etc. following more questions couldn't through today.
first, general condition is fixed , b not. in order maintain desired flight path, figured need virtual line lineb(from moda modb) sync linea's angle , distance when b(the target) moving around in directions arc not extremely skewed, in attempts today either got wrong angle linea or moda orbited around , numbers weird angle changing in straight line movement of b a.
second have random-but-similar variety of red arc after first projectile fires , next. i'm guessing easier when past first 1 since it's matter of controlling lineb.
edit # 2
all functions asked above resolved: path generated b arc made moda , modb randomness of moda , modb @ each iteration moda , modb adjusting according b's position in real time.
now that's left make projectile follow path , control velocity till reaching b. below code generating arc-path. how should approach this?
public transform[] controlpoints = new transform[4]; public linerenderer linerenderer; private int curvecount = 0; private int segment_count = 50; private void drawcurve() { (int j = 0; j < curvecount; j++) { (int = 1; <= segment_count; i++) { float t = / (float)segment_count; int nodeindex = j * 3; vector3 pixel = calculatecubicbezierpoint( t, controlpoints[nodeindex].position, controlpoints[nodeindex + 1].position, controlpoints[nodeindex + 2].position, controlpoints[nodeindex + 3].position); linerenderer.positioncount = (((j * segment_count) + i)); linerenderer.setposition((j * segment_count) + (i - 1), pixel); } } } private vector3 calculatecubicbezierpoint(float t, vector3 start, vector3 moda, vector3 modb, vector3 end) { float u = 1 - t; float t2 = mathf.pow(t, 2); float u2 = mathf.pow(u, 2); float t3 = mathf.pow(t, 3); float u3 = mathf.pow(u, 3); vector3 p = u3 * start; p += 3 * u2 * t * moda; p += 3 * u * t2 * modb; p += t3 * end; return p; }
you should use animationcurve.
you can edit "graphic curve" in inspector (public variable animationcurve) use srcipt move object along path.
using unityengine; using system.collections; public class animationpath : monobehaviour { public animationcurve xcurve; public float totaltraveltime = 5.0f; public float travelspeed = 50.0f; public float xrange = 10.0f; // use initialization void start () { startcoroutine("travel"); } ienumerator travel() { float elapsedtime = 0.0f; while(elapsedtime < totaltraveltime) { float xpos = xcurve.evaluate(elapsedtime/totaltraveltime) * xrange; transform.position = new vector3(xpos, transform.position.y, transform.position.z + travelspeed * -time.deltatime); yield return null; elapsedtime += time.deltatime; } } }
i hope can you.
Comments
Post a Comment