I am controlling a camera using the fps script, is there a way to measure the players speed that he is moving?
I somehow need to measure the change in the players x,y and z pos each frame and then divide this by time… any ideas?
I am controlling a camera using the fps script, is there a way to measure the players speed that he is moving?
I somehow need to measure the change in the players x,y and z pos each frame and then divide this by time… any ideas?
If you only need to measure speed between the previous frame and the current frame, then all you need to do is store the position information of the player object from the current frame into a Vector3, so that come next frame you can use it to calculate the difference.
Here’s a bit of code showing what I mean:
Vector3 oldPosition = new Vector3();
void Update()
{
//This is the global position of the game object
//that this script is attached to
currentPosition = gameObject.transform.position;
//Calculate the distance between the position vectors of the current and the last frame
float distance = Vector3.Distance(currentPosition, oldPosition);
float speed = distance / Time.deltaTime;
//We store the current position in another Vector
//so we can use it in calculations in the next frame
oldPosition = new Vector3(currentPosition.x, currentPosition.y, currentPosition.z);
}
I hope this clears a few things up.
-Yilmaz
If you’re using the FPSWalker.js script as it’s provided it requires you to use the CharacterController component as well. And if that’s the case then there’s no need to track anything as the CharacterController exposes a velocity property already. Get that vector and its magnitude and you’re all set.
Doesnot work for me!