Topic category: Help with Minecraft modding (Java Edition)
I am creating a tank in MCreator with some custom code of mine.
The player can ride this tank.
While the player is riding the tank, this code should make the tank shoot an ArrowCustom at the players raytraced x y and z coordinates.
This is called from onEntitiyUpdate()
The only problem is that the arrows/bullets do not shoot at the raytraced position but instead go beneath the tank.
When I get off the tank however the bullet does shoot at the correct position.
Here is the code:
public void onEntityUpdate() {
super.onEntityUpdate();
int x = (int) this.posX;
int y = (int) this.posY;
int z = (int) this.posZ;
Entity entity = this;
{
java.util.HashMap<String, Object> $_dependencies = new java.util.HashMap<>();
$_dependencies.put("entity", entity);
MCreatorTankOnEntityTickUpdate.executeProcedure($_dependencies);
}
Entity entity2 = this.getPassengers().isEmpty() ? null : (Entity) this.getPassengers().get(0);
if (this.isBeingRidden()) {
//Raytracing the x y and z look coordinates of the player.
double eTargetX = (double) (entity2.world.rayTraceBlocks(entity2.getPositionEyes(1f),
entity2.getPositionEyes(1f).addVector(entity2.getLook(1f).x * 10, entity2.getLook(1f).y * 10, entity2.getLook(1f).z * 10), false, false,
true).getBlockPos().getX());
double eTargetY = (double) (entity2.world.rayTraceBlocks(entity2.getPositionEyes(1f),
entity2.getPositionEyes(1f).addVector(entity2.getLook(1f).x * 10, entity2.getLook(1f).y * 10, entity2.getLook(1f).z * 10), false, false,
true).getBlockPos().getY());
double eTargetZ = (double) (entity2.world.rayTraceBlocks(entity2.getPositionEyes(1f),
entity2.getPositionEyes(1f).addVector(entity2.getLook(1f).x * 10, entity2.getLook(1f).y * 10, entity2.getLook(1f).z * 10), false, false,
true).getBlockPos().getZ());
double eposX = eTargetX - this.posX;
double eposY = (eTargetY - 0.1);
double eposZ = eTargetZ - this.posZ;
EntityArrowCustom entityarrow2 = new EntityArrowCustom(this.world, this);
entityarrow2.shoot(eposX, eposY - entityarrow2.posY + (double) MathHelper.sqrt(eposX * eposX + eposZ * eposZ) * 0.2F, eposZ, 1.6F, 12.0F);
//(eposX, eposY, eposZ, entity.rotationPitch, entity.rotationYaw);
this.world.spawnEntity(entityarrow2);
}