c# - Dynamically changing speed of object in Unity -
i need object move , down according dynamic timing. exact locations stored in list() called timings. contain sorted entries such 0.90 1.895 2.64 3.98... these timings relative playing of music, can compared themusic.time. (themusic audiosource).
right (see code below), it's moving statically , down using movespeed. how can make alternatively, top , bottom point reached @ predefined times? when reaches end of list of timings movement should stop.
public class patrol : monobehaviour { public transform[] patrolpoints; //contains top , bottom position public float movespeed; //needs changed dynamically private int currentpoint; // initialization void start () { transform.position = patrolpoints [0].position; currentpoint = 0; } // update called once per frame void update () { print (currentpoint); if (currentpoint >= patrolpoints.length) { currentpoint = 0; } if (transform.position == patrolpoints [currentpoint].position) { currentpoint++; } transform.position = vector3.movetowards (transform.position, patrolpoints[currentpoint].position, movespeed * time.deltatime); } }
it important there no moving away absolute time point. e.g. when timings reaches high time such 1009 there shouldn't drift.
also note other things (such changing color , checking user behaviour) need happen @ same time, see other question.
the path solution this answer shows function move gameobject on time.
with function in hand, can start coroutine, loop on list
contains beatstimer
. inside each loop should have variable determines if should go up or down after each loop. in example named variable updownmovedecider
. variable decide index of patrolpoints
array use.(i assume there 2 points index 0 , 1).
also, can call movetox
function inside for
loop each time. make sure yield
wait movetox
function finish or return before going next function.
below should like:
public transform[] patrolpoints; //contains top , bottom position list<float> beatstimer = new list<float>(); public transform objecttomove; void start() { //for testing purposes beatstimer.add(0.90f); beatstimer.add(1.895f); beatstimer.add(2.64f); beatstimer.add(3.98f); //start moveobject startcoroutine(begintomove()); } ienumerator begintomove() { // 0 = move up, 1 = move down int updownmovedecider = 0; //loop through timers (int = 0; < beatstimer.count; i++) { if (updownmovedecider == 0) { //move debug.log("moving time: " + beatstimer[i] + " in index: " + i); //start moving , wait here until move complete(movetox returns) yield return startcoroutine(movetox(objecttomove, patrolpoints[updownmovedecider].position, beatstimer[i])); //change direction 1 next move updownmovedecider = 1; } else { //move down debug.log("moving down time: " + beatstimer[i] + " in index: " + i); //start moving , wait here until move complete(movetox returns) yield return startcoroutine(movetox(objecttomove, patrolpoints[updownmovedecider].position, beatstimer[i])); //change direction 0 next move updownmovedecider = 0; } } } ienumerator movetox(transform fromposition, vector3 toposition, float duration) { float counter = 0; //get current position of object moved vector3 startpos = fromposition.position; while (counter < duration) { counter += time.deltatime; fromposition.position = vector3.lerp(startpos, toposition, counter / duration); yield return null; } }
the code inside for
loop long easier understand. here shorter version no if/else
statement.
//loop through timers (int = 0; < beatstimer.count; i++) { //move up/down depening on updownmovedecider variable string updown = (updownmovedecider == 0) ? "up" : "down"; debug.log("moving " + updown + " time: " + beatstimer[i] + " in index: " + i); //start moving , wait here until move complete(movetox returns) yield return startcoroutine(movetox(objecttomove, patrolpoints[updownmovedecider].position, beatstimer[i])); //change direction updownmovedecider = (updownmovedecider == 1) ? 0 : 1; }
Comments
Post a Comment