TrailRender not showing in all clients using Mirror networking
Hi, i'm making an arcade racing multiplayer game where each player prefab has attached two child transforms with the TrailRender component in them.
My player prefab inherits from NetworkBhevaior, i have my ownership & authority logic in it's controller. I also have NetworkTransform for the player prefab and two NetworkTransformChild for each transform that has the TrailRender. Basically i can't make other clients to see the player TrailRender emmiting.
Is a basic trail that simulates a skid. So when the angularVelocity of the car is greater than it's linear velocity it's start drifting and the TrailRenderers are set to emitting = true. The thing is only the player client who's drifting can see the trails, others not.
I tried converting those child transform into prefabs and putting them as spawnable prefabs in the network manager but it didn't work. Also as i mentioned i have in my player prefab those NetworkTransformChild targeting it's child transform with the TrailRender and also doesn't work.
My player prefab hierarchy:
*Car is the parent game object and CHILD_TRAILRENDER 1 & 2 are child of Car with the TrailRender component, the whole thing is my player prefab.
-CAR -CHILD_TRAILRENDERER_1 -CHILD_TRAILRENDERER_2
I set my trail renderers in my player controller as "TrailRender[] skids";
And in FixedUpdate() when i detect that angularVelocity is greater than fordward velocity i have an "foreach TrailRender skid in skids" and set skid.emitting = true. That's all code i have for it. works but not visible for other clients.
Please if someone knows or have an idea of how can i make this trails appearing in all clients it would be good.
Thanks for your time.
Answer by GetLitGames · Sep 29, 2020 at 01:17 PM
Right, this will be a concept that will take some time to understand. The networking library is only transmitting data. What I mean is that you should learn exactly what data it is handling and when it sends and receives because that is the main focus of the networking library.
So, to get back to your problem - your script that turns on the renderer is only running on the local machine (or maybe you are allowing it to run on all machines and it simply isn't working as expected). So when it turns on the trailrenderer, there is no magic that happens to turn it on for all the other clients. The other clients may have that script on them, but that doesn't mean it is functioning. Most people who start out with networking think that the networking code will replicate everything happening on the objects, but that is not the case. They usually make it very easy to enable replicating the position and rotation of gameobjects, but almost anything else needs an understanding of how the networking library works and what options you have to send and receive things.
Besides manually calculating the velocity in the script, you can also send RPCs to other machines telling them to turn on /off the trailrender.
My first question would be what do you mean by velocity? If you are checking velocity from a rigidbody, only the local machine's rigidbody will have any velocity. Most likely the remote copies of your player's vehicle is controlled simply by replicating the transform's position. So you may have a rigibody locally and move the player's vehicle that way (by adding force, having collisions etc), but the only data transmitted to other clients is most likely the position and rotation of the object. In fact, you wouldn't want want the remote copies to be affected by gravity, you would want their rigidbody to be kinematic.
Also, if you are doing server-authoritative control, then only the server should have an active rigidbody allowing gravity and collisions. You generally wouldn't want the remote copies to have gravity and physics turned on because all that should be happening is that the remote copies follow whatever happens on the server. So only the server needs those things, the remotes are simply puppets. Even collisions - only the server collisions should matter anyways.
So if you are running the same code on every machine, one change that might work is to manually calculate velocity by storing the current position and then in the next frame calculate the direction and velocity of the transform with some math.