- Home /
How to track an object per Frame?
Hey, i am trying to make a simple tracker for my object which will report the changes on that object,like rotation,scale,position etc.The update is pretty slow which i expected that it would be per frame but it is not.Could you point me out where am i wrong here?The update seems to be close to per second.
Thanks.
var time : float = 0;
function Update () {
if(time < 2) {
//cache the data from object at this time
time += 1;
}
if(time > 1) {
//cache the data from object at this time
time = 0;
}
//compare the caches
}
Hey, that is true.I am debugging the caches to the consoles and i think there is some delay on there because now it works perfectly fine as i wanted but the update in the editor which made me get confused.
Hi the code above works fine just curios about is there any special function that will do the job in a shorter way?
Answer by Huacanacha · Nov 04, 2013 at 07:42 PM
Just store the previous values at the end of each Update function. You don't need the 'time' counter. So the logic for Update() would be:
Compare current position, rotation etc to the previous values
Do whatever else you want to do in Update
Store the current values as 'previousPosition', 'previousRotation' etc
Hey thank you very much for taking time and replying. I am a little confused. Are you suggesting to create a new function? Excuse my understanding please
Just use some instance variables or Properties (if you can use C#) to store the previous values. Still use the Update() function using the steps I describe above. Here's a C# example (UnityScrips should be very similar) for position only:
private Vector3 previousPosition;
void Start() {
previousPosition = transform.position;
}
void Update() {
Vector3 deltaPosition = transform.position - previousPosition; // Change in position from previous frame
// Do whatever you like with the result here, like:
Debug.Log(" deltaPosition = " + deltaPosition);
previousPosition = transform.position; // Last line in Update() function
}
Hi thank you very much! I will try it and will mark your answer Thank you
Legend says he never marked it as an answer
Your answer
Follow this Question
Related Questions
How advance animation 1 frame at a time? 1 Answer
Performance of Line Renderer Functions 0 Answers
my object instantiates to much 1 Answer
BCE0023 Error 1 Answer
Breaking a loop, Javascript? 3 Answers