[1.16.5] Change projectile position in first person

Recommended Posts

I'm writing a mod for weapons and I ran into the problem of projectile position when shooting. How can I spawn a projectile so that when viewed from the first person, it flies out of the weapon, and not from the center of the screen?

Share on other sites

The easiest solution is to change the position of the bullet before it is spawned. It will however slightly mess with your aim, so take that into consideration.

An example code written in 1.15.2 (in 1.16.5 it is propably something similar):

float rot = playerEntity.rotationYaw/180.f*3.14159f; //Converts the player rotation to radians
float z = MathHelper.sin(rot) * 0.3f;	//the multiplier (0.3f) defines how much is the bullet moved to the right
float x = MathHelper.cos(rot) * 0.3f;	//this and the former equation localizes the bullet to the rotation of the player
//the subtrahend 0.5f controlls the height of the bullet
//changing the minus sign before x and z to plus moves the bullet to the left
anEntity.setPosition(playerEntity.getPosX() - x, playerEntity.getPosYEye() - (double)0.5f, playerEntity.getPosZ() - z);
//add the bullet to the world after this

Share on other sites

This code is similar to the arrow. The problem with this code will be that when I turn 45-90 degrees, my bullet will take the position on the left side

Share on other sites

Upd, oh sorry, i replaced in my entity yaw rotation to player yaw and this solve my problem. Thx

Share on other sites

That shouldn't happen because of the sinus and cosinus, at least it didn't happen in my tests.

Share on other sites

Just now, Thorius said:

That shouldn't happen because of the sinus and cosinus, at least it didn't happen in my tests.

Your code works well, I should have used the yaw player instead of taking yaw from my projectile. Thank you again

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.

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

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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

• Posts

• I was trying to ask for help with ho to detect other installed mods and it seemed like it wasnt working, i said this and the admin immediately bombarded me with things sounding like he hated me. i told him to please back off and that im trying my best but he banned me instead. my discord is "daisydogs07" this took place about 5 minutes ago too
• https://mclo.gs/OB1h4m0
• https://gist.github.com/it-is-allie/b38b7f77998ba6e53e2fd3b4281c1be6 removed that this time. still same issue
• Can you Make a Lalaloopsy Mod (With Crossover Lalaloopsy Dolls & Custom Lalaloopsy Dolls, Canon Ships, Voicelines, Families and Babies,)
• Did you remove friendsandfoes?

• Who's Online (See full list)

×

• Activity

×
• Create New...