- Home /
Plotting particle movement Time/keyframe issue
I have data with format as below:
x y z time T
-71.7087326 40.2462502 5.02939129 2.31992912
-67.7142334 40.249218 5.09217882 5.15146351
-61.1967964 40.2587852 5.23078823 9.7579298
I want to move a particle so that at time T, it will be at position x, y, z accordingly.
Currently what I do is to calculate average speed between 2 points (distance/time) and move the particle accordingly. Below is my code:
class Data
{
Vector3 pos;
float exactTime;
}
class CurrPoint
{
int id;
float currTime;
Vector3 avgSpeed;
}
CurrPoint p;
Data data;
void Update()
{
if(p.currTime == 0)
{
point.position = data[p.currID].pos;
}
p.currTime += Time.deltaTime;
if (p.currTime < data[p.id].exactTime)
{
point.position += Time.deltaTime * p.avgSpeed[p.id];
}
else
{
//exceeding value
//not the last node
if (p.id < data.Count - 1)
{
//calculate time needed to complete to next pos
float deltaT1 = data[p.id].exactTime - (p.currTime - Time.deltaTime);
float deltaT2 = Time.deltaTime - deltaT1;
//move to next id, calculate distance
p.id++;
points [i].position = data[p.id].pos;
//move using new speed as much as deltaT2
points [i].position += deltaT2 * p.avgSpeed [p.id];
}
else
{
//last node
//go back to beginning again (Reset)
p.id = 0;
p.currTime = 0;
}
}
}
And here is what it is like when the game run: [http://youtu.be/1llmgJHNtPw]
As you can see, there are times when the particle will be 'pushed back' before moving again. I think it is because the time/framerate that is not right as I am using Time.deltaTime. The particle has travelled too far before processing data at next line.
How to ensure that this won't happen? Or perhaps if there is other suggestion, you are welcomed. Thanks a lot!
Your answer
Follow this Question
Related Questions
Move from one position to another in x seconds 2 Answers
Object movement - undesired effects 1 Answer
Movement through an array over time 2 Answers
Move a keyframe in time only 1 Answer
Unexpected behavior on Vector3.moveTowards for Player 0 Answers