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.
To address the issue of your penguin swimming in straight lines and rarely changing direction or depth, here's a revised version of your PenguinSwimGoal that ensures frequent direction changes, dynamic vertical movement, and natural-looking swimming behavior.
✅ Key Fixes Implemented:
Shorter target distances: Smaller horizontal ranges (±3 blocks) to create more turns.
Faster direction updates: Reduced cooldown to 1–2 seconds (20–40 ticks).
Vertical bias toward surface: Encourages more natural up/down movement even when not surfacing.
More attempts per tick: Increased from 15 to 30 attempts to find a valid underwater target.
Prioritization of reachable targets: Ensures navigation is updated only when a valid point is found.
✅ Updated PenguinSwimGoal.java
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() {
// Only activate if in water and not already doing something else
return penguin.isInWater();
}
@Override
public boolean canContinueToUse() {
return penguin.isInWater() && !penguin.getNavigation().isDone();
}
@Override
public void start() {
this.timeToNextUpdate = 0;
this.setRandomLocation();
}
@Override
public void tick() {
this.timeToNextUpdate--;
// Update direction more frequently
if (this.timeToNextUpdate <= 0 || penguin.getRandom().nextInt(80) == 0) {
this.setRandomLocation();
}
}
private void setRandomLocation() {
// Reset the timer to 1–2 seconds
this.timeToNextUpdate = 20 + penguin.getRandom().nextInt(20);
Vec3 currentPos = penguin.position();
// Try multiple times to find a valid target
for (int attempts = 0; attempts < 30; attempts++) {
double offsetX = (penguin.getRandom().nextDouble() * 2.0D - 1.0D) * 3.0D; // ±3 blocks
double offsetZ = (penguin.getRandom().nextDouble() * 2.0D - 1.0D) * 3.0D; // ±3 blocks
// Add a surface bias to encourage vertical movement
double offsetY;
if (penguin.getRandom().nextBoolean()) {
// Occasionally swim upward to simulate curiosity or light-seeking
offsetY = penguin.getRandom().nextDouble() * 3.0D;
} else {
// Sometimes dive slightly deeper
offsetY = -(penguin.getRandom().nextDouble() * 2.0D);
}
Vec3 targetPos = currentPos.add(offsetX, offsetY, offsetZ);
BlockPos targetBlockPos = BlockPos.containing(targetPos);
// Ensure the target is in water and not too close to the world bounds
if (penguin.level().getFluidState(targetBlockPos).is(FluidTags.WATER) &&
targetBlockPos.getY() > penguin.level().getMinBuildHeight() + 5 &&
targetBlockPos.getY() < penguin.level().getMaxBuildHeight() - 5) {
// Move to the new position
penguin.getNavigation().moveTo(targetPos.x, targetPos.y, targetPos.z, speedModifier);
return;
}
}
}
@Override
public void stop() {
penguin.getNavigation().stop();
}
}
🧪 Why This Works Better:
Smaller movement ranges ensure the penguin makes more turns and doesn't glide for long distances.
Frequent updates (every 1–2 seconds) force it to reevaluate its direction regularly.
Bias toward the surface encourages natural vertical movement even when not in oxygen danger.
More attempts per tick increases the chance of finding a valid path, especially in tight spaces.
✅ Optional: Use Vanilla RandomSwimmingGoal (Simpler Alternative)
If you're not adding custom behavior, replace your custom goal with this in your PenguinEntity:
java
import net.minecraft.world.entity.ai.goal.RandomSwimmingGoal;
// Inside registerGoals()
this.goalSelector.addGoal(4, new RandomSwimmingGoal(this, 1.0D, 20));
This vanilla goal:
Automatically recalculates path every 20 ticks (~1 second).
Finds reachable water positions with built-in bias toward surface.
Prevents long, linear movement.
🧩 Pro Tip: Combine with PenguinGoToSurfaceGoal
If you already have a surface goal for oxygen, make sure it's prioritized higher than the swim goal. Example:
java
this.goalSelector.addGoal(3, new PenguinGoToSurfaceGoal(this, 1.0D));
this.goalSelector.addGoal(4, new PenguinSwimGoal(this, 1.0D));
This ensures the penguin surfaces when needed, then resumes natural swimming afterward.
Let me know if you'd like to add more behaviors like shoaling, predator evasion, or interaction with blocks like coral or kelp!