- Home /
Question by
ethanloewen · Aug 23, 2018 at 01:04 AM ·
rigidbodyvelocitycountertransform.translate
How do I make a velocity counter without rigidbody.velocity.magnitude?
Hey, I am fairly new to Unity and c# in general so apologies in advance. I want to make a Velocity counter that just records how far the player rigidbody is moving. My movement system uses transform.Translate so velocity.magnitude will not work. I attempted to make a system that uses the location of the previous frame and the current to determine how fast the player is going but this does not work as the "velocity" displayed is often 0, even when the player is moving quickly. Any ideas where I went wrong? my fixed update code is bellow.Thanks for any and all help.
public Rigidbody rb;
public float moveSpeed = 0.1f;
public float moveSpeedMax;
public Vector3 deltaPos;
private Vector3 lastPos;
public float currentPos;
public float velocity;
void FixedUpdate()
{
deltaPos = transform.position - lastPos;
Debug.Log(" deltaPosition = " + deltaPos);
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 inputMove = new Vector3(horizontal * moveSpeed * Time.deltaTime, 0, vertical * moveSpeed * Time.deltaTime);
inputMove = Vector3.ClampMagnitude(inputMove, 1);
rb.transform.Translate(inputMove);
lastPos = transform.position;
}
Comment