- Home /
This question was
closed Feb 22, 2015 at 10:28 PM by
tanoshimi for the following reason:
The question is answered, right answer was accepted
Question by
SputnlK · Feb 22, 2015 at 04:18 PM ·
transformvelocityspeedfixedupdateacceleration
Acceleration of Transform in FixedUpdate (SOLVED)
im trying to get the acceleration of the transform of a gameobject in FixedUpdate. i need it for a raycast wheel i am trying to create. i already get the velocity of it through a code i found in the web. ive been searching all day now and i still havent figured out how to calculate the acceleration of a transform.
here is what i used to calculate the transform's velocity:
IEnumerator CalcVelocity()
{
while (Application.isPlaying)
{
// Position at frame start
prevPos = transform.position;
// Wait till it the end of the frame
yield return new WaitForSeconds(Time.fixedDeltaTime);
// Calculate velocity: Velocity = DeltaPosition / DeltaTime
currVel = (prevPos - transform.position) / Time.fixedDeltaTime;
localVel = transform.InverseTransformDirection(currVel);
}
}
(sorry, can't figure out how to make a code sample)
thanks if you take time to help me
EDIT: this is how i solved it:
IEnumerator CalcVelocity()
{
while (Application.isPlaying)
{
Vector3 pos1 = transform.position;
yield return new WaitForFixedUpdate();
Vector3 pos2 = transform.position;
Vector3 vel1 = (pos1 - pos2) / Time.fixedDeltaTime;
deltaPos = pos1 - pos2;
currVel = vel1;
localVel = transform.InverseTransformDirection(currVel);
yield return new WaitForFixedUpdate();
Vector3 pos3 = transform.position;
Vector3 vel2 = (pos2 - pos3) / Time.fixedDeltaTime;
Vector3 acc = (vel1 - vel2) / Time.fixedDeltaTime;
currAcc = acc;
localAcc = transform.InverseTransformDirection(currAcc);
}
}
Comment