- Home /
vector velocity with framerate
Hello!!. I need help with something. I have the vector forward and I want to calculate it´s velocity. To do that I take the actual forward with its Time.time and the forward vector from last frame(with its Time.time as well). Then I do this:
var vel:Vector3=(VectorFCamera-transform.forward)/(LastTd-Time.time);
Where VectorFCamera is the forward vector from last frame and LastTd the Time.time. I set a constant velocity to see what happens:
transform.Rotate(Blur*Vector3.up, Space.World);
Ok. So what I expect is a constant vector velocity, but what I found is a non uniform vector. The direction is correct but the lenght is not. I think that it depends on the framerate but thats not possible because am considering with (LastTd-Time.time).
Maybe I am doing something wrong. Anyone knows how to solve this?
ok. I tried use fixedupdate, but I have another problem. $$anonymous$$y code is this:
private var VectorFCamera:Vector3=Vector3(0,0,0);
private var Speed:float=0.0f;
public var Blur : float = 3.0f;
function Update() {
transform.Rotate(Blur*Vector3.up*Time.deltaTime, Space.World);
Debug.Log (Speed);
}
function FixedUpdate() {
Speed = (transform.forward - VectorFCamera).magnitude / Time.deltaTime;
VectorFCamera=transform.forward;
}
The problem is that Debug.Log (Speed); always gives 0. What is happenig now?
Answer by Bunny83 · May 04, 2014 at 08:45 AM
Uhm, you rotate the object around it's pivot, so there is no translation of the pivot. Also you don't use the physics system to move the object, so calculating Speed inside FixedUpdate makes not much sense and could even lead to other problems.
But I am not checking the translation of the pivot. I am trying to obtain the vector variation of forward vector. I tried in update function: var vel:Vector3=(VectorFCamera-transform.forward)/(LastTd-Time.time);
But as I said it gives me aleatory length, as it depends on framerate.