Search In
• More options...
Find results that contain...
Find results in...

# [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.

×   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

• Doing these things moves the camera. Your code subtracts the camera's position value from the MatrixStack.
• That is just the modpack, no logs.
• I created a modpack for me and my friends but we constantly crash after ~5 minutes of playing , Can some one help me please ? Here are the logs and the modpack : https://www.mediafire.com/file/kqnui7c0wzu8t7k/VladPack4-1.16.5.zip/file
• I wrote a class that stores the BlockPos and BlockState of a specific block that needs to be changed into another block after a certain amount of time. So far I just coded stone bricks being changed into cracked stone bricks. In every worldtickevent I call a method called onTick in which there is a slight chance (1 in 1000) that this change will occur. The class that does this is called DecayHandler.java   DecayHandler.Java :   public class DecayHandler { public BlockState blockState; public BlockPos blockPos; public World world; private Random random; public int decayChance = 1000; private boolean decayed = false; public DecayHandler(BlockState blockState, BlockPos blockPos, World world){ this.blockState = blockState; this.blockPos = blockPos; this.world = world; random = new Random(); } public DecayHandler OnTick(){ //TODO: Refactor in other methods and implement BlockSwapper if(random.nextInt(decayChance) == 0 && !decayed){ System.out.println(this.blockPos + " Decayed"); if(world.setBlockAndUpdate(blockPos, Blocks.CRACKED_STONE_BRICKS.getBlock().defaultBlockState())){ this.blockState = Blocks.CRACKED_STONE_BRICKS.getBlock().defaultBlockState(); decayed = true; return this; } } //WorldDecayData.get(); return null; } }   This code works fine when I load in to a world and place stone bricks down. The stone bricks stay there for a couple of seconds and then they change into cracked stone bricks. The problem occurs when I save and quit to title and rejoin the same world when I placed stone brick that hadn't decayed yet. The game seems to remember what DecayHandlers were running which I guess is logical because I never closed it. So when the world is reloaded again my code stops without error and no longer functions. I can see in the terminal that the last message that was displayed was that a block decayed so I put a breakpoint on it to see what went wrong. By the way, the game keeps running just fine but my code just stops. The part where it seems to go wrong is in this line:  if(world.setBlockAndUpdate(blockPos, Blocks.CRACKED_STONE_BRICKS.getBlock().defaultBlockState())) When I try to step in to every single detail the callstack becomes insanely large and I'm unable to understand what's going on. The only thing I know is that right at the end they put my thread in to parking or something? I really don't understand what was going on. Can someone explain why this is happening? I don't have a clue of what's going on.

• 1
• 1
• 0
• 4
• 4
• ### Who's Online (See full list)

×

• #### Activity

×
• Create New...