SuperNewbee
New member
I am trying to make a speedometer for my game object (a cube - no rigid body or collider)
I am using the 'MoveTowards' task from the movement pack addon to move my cube. (no other tasks in my simple tree) I attached the following script to calculate the speed. I noticed that the speed is always fluctuating.
SpeedTest.cs script seems to work when moving by using transform.position and rigidbody moving (without behavior designer)
What is going on? I am very confused.
I am using the 'MoveTowards' task from the movement pack addon to move my cube. (no other tasks in my simple tree) I attached the following script to calculate the speed. I noticed that the speed is always fluctuating.
SpeedTest.cs script seems to work when moving by using transform.position and rigidbody moving (without behavior designer)
What is going on? I am very confused.
using UnityEngine;
public class SpeedTest : MonoBehaviour
{
private Vector3 lastPosition;
public float speed;
void Start()
{
lastPosition = transform.position;
}
void Update()
{
if (lastPosition != transform.position)
{
Vector3 direction = transform.position - lastPosition;
speed = direction.magnitude / Time.deltaTime;
lastPosition = transform.position;
}
}
}