- Home /
Is there a Magic Code for getting delta values?
Hey, i am wondering that if there is any magic code or function that gets the delta of any floating value between last and current frame? Like a Mathf but doing a compare job between frames.What would be the ways of doing it?Any magic formula for this?I know that Time.deltaTime is irrelevant depending on the fps and gets always the same value.So could we do a math over it? If we multiply a floating value with Time.deltaTime would it give a kind of delta for us?
Answer by KellyThomas · Dec 28, 2013 at 11:15 AM
No magic formula, just manual tracking:
float lastFrameValue;
void Update() {
float deltaValue = currentFrameValue - lastFrameValue;
//other stuff
lastFrameValue = currentFrameValue;
}
Wash, rinse, repeat.
You could build a system that took an ID and current value pair, then returned the delta but it's not part of the unity API and it would be slower to execute than using the pattern above.
I am using the code above as well but i am not satisfied with it is there any alternative to that please?
here is also the code;
#pragma strict
var target : Transform;
var oldAngle : float;
var newAngle : float;
var angle : float;
oldAngle = $$anonymous$$athf.Atan2((target.position - transform.position).x, (target.position - transform.position).z) * $$anonymous$$athf.Rad2Deg;
function Update ()
{
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
{
target.RotateAround(Vector3.zero, Vector3.up, 5);
}
else
{
target.RotateAround(Vector3.zero, Vector3.up, -5);
}
newAngle = $$anonymous$$athf.Atan2((target.position - transform.position).x, (target.position - transform.position).z) * $$anonymous$$athf.Rad2Deg;
angle = newAngle - oldAngle;
oldAngle = newAngle;
Debug.Log(angle);
}
As well as angle won't increase correctly, it does increase to 270 then all in sudden change to -90, i don't know why.
Ok then, the problem isn't tracking deltas between floats. The problem is dealing with a "wrapped" number space i.e. 360 = 0, 359 = -1.
In your code above, please try replacing Debug.Log(angle);
with:
Debug.Log("old: " + oldAngle + ", new: " + newAngle + ",delta: " + angle);
You will see why the numbers behave like they do.
One useful trick is to normalize them to the range -180 to 180.
This ensures that we have the smallest angle between the other two angles.
Your answer
Follow this Question
Related Questions
mathf.sin((Mathf.PI / 180) * 180) get 0.8966.. 2 Answers
Distance in a Trajectory of a projectile 0 Answers
Wispy magic effect 0 Answers
LineRenderer: 2D Parabola in 3D Space 1 Answer
Mathf iphone input 0 Answers