Jittery MoveToward task

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.


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; } } }
 
below is the node I use for moveTowards in BD

using UnityEngine;
using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;
public class MoveTowards : Action
{
// The speed of the object
public float speed = 0;
// The transform that the object is moving towards
public SharedTransform target;
public override TaskStatus OnUpdate()
{
// Return a task status of success once we've reached the target
if (Vector3.SqrMagnitude(transform.position - target.Value.position) < 0.1f)
{
return TaskStatus.Success;
}
// We haven't reached the target yet so keep moving towards it
transform.position = Vector3.MoveTowards(transform.position, target.Value.position, speed * Time.deltaTime);
return TaskStatus.Running;
}
}
 
Top