Jump to content

Recommended Posts

Posted (edited)

I am implementing a custom projectile that simply goes in a straight line.

Nearly everything about my custom projectile works, except the rotation. I want the projectile to rotate in the direction it is going, like an arrow. This is my source code below. 

public class CustomProjectileEntity extends Projectile implements GeoAnimatable {

    private double shooterX;
    private double shooterY;
    private double shooterZ;
  
    // Constructor is placed here; shooterX, shooterY, and shooterZ are initialized
  
    public void shoot(LivingEntity target, float velocity) {
        double xDir = target.getX() - this.shooterX;
        double yDir = target.getY() - this.shooterY;
        double zDir = target.getZ() - this.shooterZ;
        this.shoot(xDir, yDir, zDir, velocity, 0.0f);
    }

    public void tick() {
        super.tick();
      
        // ...
      
        Vec3 vec3 = this.getDeltaMovement();
        double newX = this.getX() + vec3.x;
        double newY = this.getY() + vec3.y;
        double newZ = this.getZ() + vec3.z;
        this.setDeltaMovement(vec3);
        // this.updateRotation(); // no need because projectile rotation is already initialized in Projectile::shoot!
        this.setPos(newX, newY, newZ);
    }
  
  	// other methods ...

}

You can also see that I created this custom entity with GeckoLib. Other than the code above, is there anything I need to change in the Renderer class?

public class CustomProjectileRenderer extends GeoEntityRenderer<CustomProjectileEntity> {

    public CustomProjectileRenderer(EntityRendererProvider.Context renderManager) {
        super(renderManager, new CustomProjectileModel());
        this.shadowRadius = 0.0f;
    }

    public @NotNull ResourceLocation getTextureLocation(@NotNull CustomProjectileEntity instance) {
        return new ResourceLocation(ExampleMod.MODID, "textures/entity/custom_projectile_texture.png");
    }
  
    // SOLUTION: override GeoEntityRenderer::actuallyRender
      @Override
    public void actuallyRender(PoseStack poseStack, CustomProjectileEntity animatable, BakedGeoModel model, RenderType renderType, MultiBufferSource bufferSource, VertexConsumer buffer, boolean isReRender, float partialTick, int packedLight, int packedOverlay, float red, float green, float blue, float alpha) {
        poseStack.mulPose(Axis.YP.rotationDegrees(animatable.getYRot()));
        poseStack.mulPose(Axis.XP.rotationDegrees(-animatable.getXRot()));
        super.actuallyRender(poseStack, animatable, model, renderType, bufferSource, buffer, isReRender, partialTick, packedLight, packedOverlay, red, green, blue, alpha);
    }

}

 

Edited by LeeCrafts
Posted (edited)

Solved. I just had to override GeoEntityRenderer::actuallyRender and apply the rotations there. Super.actuallyRender has to be called afterwards (not before).

I think that the reason why I had to do this manually is because my CustomProjectileEntity is not a LivingEntity. I looked at the code in the GeoEntityRenderer class, and its rendering methods apply rotations to entity that are LivingEntities. Kinda explains why the custom mob I had also created with GeckoLib had no issue rotating.

And there's no need to call updateRotation() because the projectile's rotation is already initialized in Projectile::shoot.

Edited by LeeCrafts
  • LeeCrafts changed the title to [1.19.3, SOLVED] Custom projectile does not rotate

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.