Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

I'm adding a new enemy to the game. But when I spawn it, it doesn't attack the player, even though I added the corresponding target selector. I borrowoed most of it from the ZombieEntity class.

Here is the class:

public class AngrySubEntity extends MonsterEntity {

    public AngrySubEntity(EntityType<? extends AngrySubEntity> type, World worldIn)
    {
        super(type, worldIn);

        this.goalSelector.addGoal(8, new LookAtGoal(this, PlayerEntity.class, 8.0F));
        this.goalSelector.addGoal(8, new LookRandomlyGoal(this));

        this.goalSelector.addGoal(7, new WaterAvoidingRandomWalkingGoal(this, 1.0D));
        this.targetSelector.addGoal(2, new NearestAttackableTargetGoal<>(this, PlayerEntity.class, true));
    }

    public static AttributeModifierMap.MutableAttribute createAttributes()
    {
        return MonsterEntity.func_234295_eP_()
                .createMutableAttribute(Attributes.FOLLOW_RANGE, 35.0D)
                .createMutableAttribute(Attributes.MOVEMENT_SPEED, 0.23F)
                .createMutableAttribute(Attributes.ATTACK_DAMAGE, 3.0D)
                .createMutableAttribute(Attributes.ARMOR, 2.0D);
    }

    protected int getExperiencePoints(PlayerEntity player)
    {
        return 0;
    }

    public boolean attackEntityFrom(DamageSource source, float amount)
    {
        if (!super.attackEntityFrom(source, amount))
        {
            return false;
        }
        else return this.world instanceof ServerWorld;
    }

    protected SoundEvent getHurtSound(DamageSource damageSourceIn)
    {
        return SoundEvents.ENTITY_ZOMBIE_HURT;
    }

    protected SoundEvent getStepSound()
    {
        return SoundEvents.ENTITY_ZOMBIE_STEP;
    }

    protected void playStepSound(BlockPos pos, BlockState blockIn)
    {
        this.playSound(this.getStepSound(), 0.15F, 1.0F);
    }
}

 

You should register your goals in #registerGoals instead of your constructor. I'm not sure, but i think you should have an attack goal too (In zombie it is ZombieAttackGoal). If it still doesn't work, please post more code (or a github link). Hope i can help

  • Author
45 minutes ago, Silverminer said:

You should register your goals in #registerGoals instead of your constructor. I'm not sure, but i think you should have an attack goal too (In zombie it is ZombieAttackGoal). If it still doesn't work, please post more code (or a github link). Hope i can help

Thank you, now with those changes added, my entity now follows the player, like it tries to attack, but doesn't actually attack.

@Override
protected void registerGoals() {
  super.registerGoals();

  this.goalSelector.addGoal(8, new LookAtGoal(this, PlayerEntity.class, 8.0F));
  this.goalSelector.addGoal(8, new LookRandomlyGoal(this));

  this.goalSelector.addGoal(1, new MeleeAttackGoal(this, 1.0D, true));
  this.goalSelector.addGoal(7, new WaterAvoidingRandomWalkingGoal(this, 1.0D));
  this.targetSelector.addGoal(2, new NearestAttackableTargetGoal<>(this, PlayerEntity.class, true));
}

 

16 hours ago, Kiryu144 said:

Thank you, now with those changes added, my entity now follows the player, like it tries to attack, but doesn't actually attack.

24 minutes ago, Silverminer said:

There are also a lot of vanilla examples that you could look at, e.g. ZombieEntity

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.