- Home /
Trouble with Basic Math Formula for Deriving Velocity
Hello everyone, I'm having trouble understanding how a value (in the script below) is being derived. In the game scene, I have this script attached to a column that moves up and down, and I can confirm that currentPosition and previousPosition are different values. However, the values of x, y , and zVel are always 0. I don't understand what is wrong with the formula where 2 different numbers can equal 0.
public Vector3 currentPosition;
public Vector3 previousPosition;
public float xVel;
public float yVel;
public float zVel;
void Update ()
{
currentPosition = transform.localPosition;
//Debug.Log ("currentPosition is " + currentPosition);
Debug.Log ("current Y position is " + currentPosition.y);
xVel = currentPosition.x - previousPosition.x;
yVel = currentPosition.y - previousPosition.y;
Debug.Log ("yVel is " + yVel);
zVel = currentPosition.z - previousPosition.z;
MovePosition ();
previousPosition = transform.localPosition;
//Debug.Log ("previousPosition is " + previousPosition);
Debug.Log ("previous Y position is " + previousPosition.y);
}
void MovePosition ()
{
if (nextPhase == false && inMotion == false)
{
transform.localPosition = Vector3.MoveTowards (transform.localPosition, endingPosition, movementSpeed * Time.deltaTime);
if (Vector3.Distance(endingPosition, transform.localPosition) < 0.01f)
{
inMotion = true;
if (inMotion == true) //&& Time.time > waitToRetract)
{
StartCoroutine (WaitForRetract(rectractTimer));
}
}
}
if (nextPhase == true && inMotion == false)
{
transform.localPosition = Vector3.MoveTowards (transform.localPosition, startingPosition, movementSpeed * Time.deltaTime);
if (Vector3.Distance(startingPosition, transform.localPosition) < 0.01f)
{
inMotion = true;
if (inMotion == true)
{
StartCoroutine (WaitForRestart(restartTimer));
}
}
}
}
Can we see the variable declarations? If you're using integers to store floats, the resulting values will be truncated.
Can you provide the code to your $$anonymous$$ovePosition() $$anonymous$$ethod as well please.
Just saying, in the code you've posted, inmotion will always be true on from that first time you set it to true.
Answer by RudyTheDev · Feb 21, 2014 at 12:57 AM
This is backwards:
MovePosition ();
previousPosition = transform.localPosition;
You can't move first, then remember the "previous" position. You have to remember where you were, then do movement. Otherwise you remember the new position, which will be the currentPosition
next frame (unless something extra happens after Update()
, but then you will only account for that extra change).
@$$anonymous$$TheDev: I think the problem is where I'm doing the float calculations. When I think about it I'm basically saying:
Awake()
{
store whereIAm
store whereIWillBe
//which will be the same, then:
}
Update ()
{
store whereIAm (say its 0)
Do calculations
$$anonymous$$ove ()
store whereIWillBe (say its 1)
}
The problem is, when Update is called again, whereIAm will equal 1, while whereIWillBe is also still 1 - which makes sense. What doesn't make sense (to me anyway), is how I'm getting different values for those 2 values since they should be the same.
$$anonymous$$oving where the calculation was done seems to have solved the problem, it doesn't fully explain what was going on, but I'll take what I can get.
You could be getting different values because some other movement happens in between Update()
calls, like some rigid body physics in FixedUpdate()
.