Client transform clobbers bullet that is created on server.

Hi,
This is a bit of a hard problem to explain, but I figured out what is wrong with my game. So I have a networked game using UNET. The game is a flying spaceship shooter game. When the players shoots, a glowing particle blob gets fired from the front of the vehicle and if it hits an enemy craft will impose damage to the enemy. The particle gets created in front of the vehicle and has a speed that travels faster than the players craft, so the player that shot the energy ball won’t hit it’s own bullet.

The players craft has local authority - every player will send their transform via using network transform component to the server and the server will accept the client position of the player and update the servers copy of the transform. The energy ball that gets fired is owned by the server because i want the server to do all collision checks and damage checks. The client that shot the bullet doesn’t have the right to control the x,y,z location of the energy ball - the server will do this and run physics and send the coordinates back to the clients. the clients do however run client side prediction, but the bullet will get overridden to the servers copy as updates come in.

There is a problem with this design. When the client fires, he will call a command to the server which signals to start firing (When the craft is firing, the craft every half a second will keep firing a new energy ball). When the player wants to stop firing, there is a command to the server to stop this. So the client can’t hack sending thousands of bullets every second - They can only signal the server when they want to start firing and the server will work out the time between the bullets when to fire telling all clients about the new object and updating the clients with the x,y,z coordinates of the bullet.

The bullet has a character controller on it and a script which propels the bullet forward using the controllers move function. Here’s the problem, when the server creates the bullet, it looks at the current craft x,y,z position (Which remember is delayed because the client may have not sent the newer coordinates yet) and it creates the bullet in front of the vehicle. What happens next is the client then sends a transform update which will “Clobber” over the bullet (Since when the bullet was created on the server, at that time on the client the craft was slightly a little forward more) . Because there is a collision from this, the bullet changes direction to suit the collision and the bullet flies in a different direction that is not where the craft is looking at.

So, some solutions to this may be to see if it is possible to make the character controller not “Have” collisions with certain objects in the scene like the craft it is being fired from… Is this possible? If I put a trigger on the bullet and make the bullet move by updating the transform position, the trigger doesn’t notify when it hits a collider because triggers only notify when colliders move into it.

See the great thing about a trigger is that it notifys a collision but doesn’t block a collision, BUT it only works if things crash into it, not the other way around.

The other way is to use maybe a Physics.Raycast before every move and not have a character controller attached and update it’s x,y,z transform coordinate instead of using character controllers move method.

On the Server: I would have the bullet remember the Player that generated it. I would change the bullet to a Trigger instead of Collision. Then OnTriggerEnter check what you triggered with. If it’s a wall of part of the environment then destroy the bullet (or explode or bounce or whatever). If it’s a player then check to see what player you hit. If it’s the owner of the bullet then ignore the trigger. If it’s an enemy player then cause damage & destroy the bullet.

On the Client: Do nothing. Disable/destroy collider permanently. When the bullet hits something that destroys it on the server then the destroy message will be sent to the client. It’s possible due to latency a bullet could penetrate a little into a wall in which case you could leave the collider enabled and do a local destroy on the client on a wall (or any stationary object) as long as it never falsely destroys the object on the client when it never gets destroyed on the server.

However, if your bullets will bounce or do stuff that requires a physics collision (not trigger) then what you can do is go to Edit-ProjectSettings-Physics and create a layer for each player. Lets say you have 4 players
LayerPlayer1: Only hits P2, P3, P4
LayerPlayer2: Only hits P1, P3, P4
LayerPlayer3: Only hits P1, P2, P4
LayerPlayer4: Only hits P1, P2, P3
If you have lots of players this method will not work because you are limited in the number of layers you can have in which case you MUST use triggers (and the program your own physics bounce or whatever)