I have been following the same penguin for a hundred blocks and it has not changed directions once and the only times it changes in the y axis is when it goes up to get oxygen please fix this
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
You're absolutely right – with the current implementation, the penguin's swimming behavior still lacks the dynamic up/down movement and frequent direction changes we want. The issue lies in how the target positions are being chosen and how often the penguin re-evaluates its path.
Here's a revised version of the PenguinSwimGoal that addresses these issues by:
Increasing the frequency of direction changes
Biasing vertical movement to encourage more up/down swimming
Reducing the horizontal movement range for more frequent turns
Adding a minimum water depth check to prevent hugging the surface
java
package net.mattias.oceancritteria.entity.goals;
import net.mattias.oceancritteria.entity.custom.PenguinEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.entity.ai.goal.Goal;
import net.minecraft.world.phys.Vec3;
import java.util.EnumSet;
public class PenguinSwimGoal extends Goal {
private final PenguinEntity penguin;
private final double speedModifier;
private int timeToNextUpdate;
public PenguinSwimGoal(PenguinEntity penguin, double speedModifier) {
this.penguin = penguin;
this.speedModifier = speedModifier;
this.setFlags(EnumSet.of(Goal.Flag.MOVE));
}
@Override
public boolean canUse() {
return this.penguin.isInWater() && this.penguin.getRandom().nextInt(10) == 0; // More frequent activation
}
@Override
public boolean canContinueToUse() {
return this.penguin.isInWater();
}
@Override
public void start() {
this.timeToNextUpdate = this.adjustedTickDelay(10); // Change direction very frequently
this.setRandomSwimTarget();
}
@Override
public void tick() {
if (--this.timeToNextUpdate <= 0) {
this.setRandomSwimTarget();
this.timeToNextUpdate = this.adjustedTickDelay(10 + this.penguin.getRandom().nextInt(20));
}
}
private void setRandomSwimTarget() {
Vec3 currentPos = this.penguin.position();
int attempts = 0;
while (attempts < 20) {
attempts++;
// Smaller horizontal range for more frequent turns
double offsetX = (this.penguin.getRandom().nextDouble() * 2 - 1) * 4;
double offsetZ = (this.penguin.getRandom().nextDouble() * 2 - 1) * 4;
// Biased vertical movement: 60% chance to move up/down, 40% chance to stay at same level
double offsetY;
if (this.penguin.getRandom().nextDouble() < 0.6) {
offsetY = (this.penguin.getRandom().nextDouble() * 4) - 2; // ±2 blocks vertically
} else {
offsetY = 0;
}
Vec3 targetPos = currentPos.add(offsetX, offsetY, offsetZ);
BlockPos targetBlock = BlockPos.containing(targetPos);
// Check if target is in water and not too close to surface/seabed
if (this.isValidSwimTarget(targetBlock)) {
this.penguin.getNavigation().moveTo(targetPos.x, targetPos.y, targetPos.z, this.speedModifier);
return;
}
}
}
private boolean isValidSwimTarget(BlockPos pos) {
// Check if position is in water
if (!this.penguin.level().getFluidState(pos).is(FluidTags.WATER)) {
return false;
}
// Prevent swimming too close to the surface (minimum depth of 3 blocks)
if (pos.getY() >= this.penguin.level().getSeaLevel() - 3) {
return false;
}
// Prevent swimming into the void
if (pos.getY() < this.penguin.level().getMinBuildHeight() + 5) {
return false;
}
return true;
}
@Override
public void stop() {
this.penguin.getNavigation().stop();
}
}
Key Improvements:
Much More Frequent Direction Changes:
The penguin now changes direction every 0.5-1.5 seconds (10 + random(20) ticks)
This is achieved by setting a very short timer in start() and tick()
Biased Vertical Movement:
60% of the time, the penguin will move up or down by up to 2 blocks
This creates natural diving and surfacing behavior even when not needing air
Smaller Horizontal Movement:
The penguin now moves ±4 blocks horizontally instead of ±6
This forces more frequent turns and prevents long straight paths
Surface Avoidance:
The penguin won't swim within 3 blocks of the surface (adjustable in isValidSwimTarget())
This prevents constant surface hugging and encourages deeper swimming
Void Protection:
Added a check to prevent swimming too low in the ocean
Additional Tips for Testing:
Give the Penguin More Air Supply:
Temporarily increase the penguin's air supply duration so it doesn't need to surface as often:
java
// In PenguinEntity class
public int getMaxAirSupply() {
return 400; // 20 seconds instead of default 15
}
Observe in Creative Mode:
Use /gamemode creative and follow the penguin from a distance
Use F3 + B to show hitboxes and see the pathfinding
Debug Pathfinding:
Add temporary debug logs to see the chosen targets:
These changes should make the penguin's swimming much more dynamic with constant direction changes and natural up/down movement. The key was making the direction changes extremely frequent and adding a strong vertical movement bias.