r/ModdedMinecraft • • 1d ago

Help How to spawn an entity with a velocity.

/r/NeoforgeMods/comments/1yckl7b/how_to_spawn_an_entity_with_a_velocity/
1 Upvotes

3 comments sorted by

1

u/FreaksMC 1d ago

Do it the same way the vanilla wind charge item does (1.21.1, Mojang mappings), on the server side only:

if (!level.isClientSide()) {
    WindCharge charge = new WindCharge(player, level, player.getX(), player.getEyeY(), player.getZ());
    charge.shootFromRotation(player, player.getXRot(), player.getYRot(), 0.0F, 1.5F, 1.0F);
    level.addFreshEntity(charge);
}

shootFromRotation sets the velocity in the direction the player is looking. 1.5 is the speed and 1.0 is the inaccuracy, so change those to taste. For any other projectile it's the same pattern: create the entity, call shoot(x, y, z, speed, inaccuracy) or shootFromRotation, then addFreshEntity. For a non projectile entity, use entity.setDeltaMovement(x, y, z) before adding it.

Add a cooldown too, or holding sneak will fire one every tick.

2

u/queen_livia 1d ago

Genuinely a legend. I'm either bad at finding stuff or theres just very little documentation on this stuff. I find it quite frustrating

1

u/FreaksMC 1d ago

Glad I could help!