Do you mean that you want to give it a max speed, or have it move at a consistent speed?
If you want to give it a max speed, look at this example which is an extension of an earlier example. Note, I'm still using the code that makes it follow the mouse.
Code:
int maxSpeed = 8; //best keep this below 16
public override void AI()
{
Vector2 acceleration = Vector2.Normalize(Main.MouseWorld - projectile.Center); //convert the distance from this projectile to the mouse to a unit-vector
projectile.velocity += acceleration * 1.5f; //add one and a half of my acceleration to my velocity
projectile.velocity *= 0.95f; //slow down slightly, i.e. apply drag
if (projectile.velocity.Length() > maxSpeed)//if going too fast
{
projectile.velocity.Normalize();//normalise
projectile.velocity *= maxSpeed;//and multiply by my max speed
}
projectile.rotation = projectile.velocity.ToRotation(); //calculate the direction that this projectile is going and rotate to face that way.
}
If you simply want it to move at a fixed rate, the following will do, but the movement will be very abrupt.
Code:
int maxSpeed = 8; //best keep this below 16
public override void AI()
{
projectile.velocity = Vector2.Normalize(Main.MouseWorld - projectile.Center) * maxSpeed; //normalise the difference between my position and multiply by my desired speed.
projectile.rotation = projectile.velocity.ToRotation(); //calculate the direction that this projectile is going and rotate to face that way.
}
Also, I think my findNearest function could be more efficient. Replace the first three 'if' statements inside the 'for' loop with the following:
Code:
if (!Terraria.Main.npc[i].CanBeChasedBy(projectile))//Don't target anything it shouldn't
continue;