Jump to content

Recommended Posts

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);
    }
}

 

Posted

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

Posted
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));
}

 

Posted
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...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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