I made a simple combat system, however, I can’t get the knockback right.
All I want is both the character delivering the “punch” and target that is being “hit”, to be moved by the knockback simultaneously.
I am using Bodyvelocity. It does work, but the two Humanoids getting moved by knockback are not being moved at the same time. I see a slight delay in one of the Humanoid being knock backed.
On top of that, I don’t believe using “Bodyvelocity” is the best way.
Physics are calculated by the network owner of a part. Since all players have network ownership over their own character, you always have to wait for the network owner to respond. Hence why it seems delayed.
The solution i’ve found is to give network ownership to the attacker for the time period of the knockback. You can do so by using the :SetNetworkOwner() method on the enemy’s HumanoidRootPart before you apply the knockback.
I applied what you said - and it worked, at least for the attacker’s perspective.
I don’t know if I did something wrong, but for the enemies’ point of view, it has this “glitchy / laggy” effect. I believe its because it keeps switching with the network ownership.
I gave network ownership to the attacker right before the knockback. Then I used :SetNetworkOwnershipAuto() when the enemies ‘stun’ is removed.
I received a notification that you linked to my reply in some topic. Thank you but I’d like to point out that creating ragdoll for every client in this game isn’t a good idea. Yes, you’ll solve the lag issue, but you’ll have to constantly sync the ragdoll and replicate all character visual changes to it for realism.
As I said earlier, there is no perfect solution, but in this case I would advise continuing to use the server ragdoll, despite minor lags and freezes.
Thank you both for your inputs. The ragdoll-client system does seem interesting - however, I don’t believe the ragdoll is causing the problem from this topic.
I will take your advice and continue with the server ragdoll - but I may attempt this other method another time, many thanks😁.
Ragdoll is not the main problem, I am more inclined to believe that each player has their own ping, and it is because of this that when using any BodyMovers, Mover constraints or Mechanical constraints on the player’s character, a certain delay will occur.
Even if you change the network owner from the player being knockbacked to yourself, it won’t completely solve the problem, as others will still see the lag. Just accept it as is; that’s how the physics engine works, and it can’t be changed…
Instead of BodyVelocity, I would recommend ApplyImpulse - instant knockback that just feels right.
Velocity is too linear but the impulse feels like the force you would receive from a hit. Combining the force with a ragdoll makes the player get pushed back and fall to the ground
ApplyImpulse can be used in the same RunContext that has the BasePart network ownership. In our case, the entire assembly is owned by the player. Running ApplyImpulse from the client for this purpose isn’t a good idea, as it’s vulnerable to exploits. (of course, you can forcefully change the network ownership to the server, but I personally don’t recommend it, as character control will be glitchy and laggy)
I’d recommend replacing this with changing AssemblyLinearVelocity directly. It basically does the same thing…
This is how the engine works, accept it, I repeat. This is okay.
If you want to make it smoother, then enable Massless on the root part of the character (or on all parts of which it is a parent) that you are going to knockack for about 0.5 seconds, then disable it. I talked about this in the answer that @nvidiagraphicsr4 suggested. Unfortunately, this is also not ideal, because at this moment the knockbacked character will “freeze” for a split second, but I think it will look better than what you have now.
If you want everything to be smooth without any visual lag, then you will have to replicate the knockback system on each of the clients (which I think will be worse). That’s all I can advise.