I have an Npc in my rpg that moves around based on a Random() variable. My main goal is to have him walk in a random direction a small bit every 2 seconds. I only want him to move about 32 pixels every time he walks.
public void npcLogic()
{
rand = new Random();
now = System.nanoTime();
if((now - lastMove) >= moveSpeed)
{
if(rand.nextFloat() >= .4)
{
Game.npcX += 32;
lastLook = 3;
}
else if(rand.nextFloat() >= .3)
{
Game.npcX -= 32;
lastLook = 2;
}
else if(rand.nextFloat() >= .2)
{
Game.npcY += 32;
lastLook = 1;
}
else if(rand.nextFloat() >= .1)
{
Game.npcY -= 32;
lastLook = 0;
}
lastMove = now;
}
}
The problem is he jumps to his destination rather than smoothly walking there. I have tried setting a timer to move him slightly for a specific amount of time like
Long smNow = 0L;
Long smLast = 0L;
Long smSpd = 5000000L;
public void npcLogic()
{
rand = new Random();
now = System.nanoTime();
if((now - lastMove) >= moveSpeed)
{
if(rand.nextFloat() >= .4)
{
smNow = System.nanoTime();
while((smNow - smLast) >= smSpd)
{
Game.npcX += Player.SPEED;
lastLook = 3;
}
smLast += smNow;
}
lastMove = now;
}
}
but this Stops movement of the player character and he still jumps, and now way too far. So does anyone have any suggestion to make the Npc move smoothly while the player character can still move around?