I suspect this is a very simple resolution to this but I haven’t yet found it.
I’m using this very simple code piece…
_move.Speed = MoveSpeed * Time.deltaTime;
transform.position = Vector2.MoveTowards(playerPosition, movePosition, _move.Speed);
And it’s being called every Update (I’ve also tried FixedUpdate). However, when the FPS rate spikes or drops the movement happens at a much faster, noticeable rate. I’m just trying to keep a steady speed.
p1 = p0 + velocity * time
So, if your object is moving at speed MoveSpeed, multiply by Time.deltaTime, as you do. Then add that to the current position:
transform.position += _move.Speed;
I’m sorry, I do not understand. transform.position expects a Vector3/2 while _move.Speed is a float containing value 30 in my case.
Ah okay. So what direction does the GO travel in? If you know the direction it travels in, then multiple that direction by your speed.
1 Like
You have to define a direction for the Object to travel to, and multiply that direction with the Time.deltaTime variable, this makes it travel at the same speed, regardless of your framerate.
function Update() {
var direction : Vector2 = Vector2.right;
Transform.position += direction * Time.deltaTime;
}
void Update() {
Vector2 direction = Vector2.right;
Transform.position += direction * Time.deltaTime;
}
in your case you should do this
var step : float = MoveSpeed * Time.deltaTime;
var newPosition : Vector2 = Vector2.MoveTowards(currentPosition, targetPosition, step);
float step = MoveSpeed * Time.deltaTime;
Vector2 newPosition = Vector2.MoveTowards(currentPosition, targetPosition, step);
It seems that you’re already doing this correctly though…
What exactly are you experiencing?
By the way, make sure that this code is inside the update function!
1 Like
That is what I’m doing. It just seems that on occasion my object will move a bit faster on occasions. Others notice it as well. Maybe I’m dropping frames somewhere. The artifact doesn’t happen often so I’ll shrug it aside for now and see if it’s an issue at compile time.
The point of Time.deltaTime is that it uses the time since the last frame. So dropping frames doesn’t affect movement speed.
–Eric