#Melee Attacking

11 messages · Page 1 of 1 (latest)

knotty moss
#

Hi there! I am new to networking, but I researched a bit and I know some concepts. Nevertheless its hard for me to actually apply those concepts.

The Game I want to make is a 2D Platformer where People battle each other.
For now I just want to implement simple Controls and health system as a starting point.
The movement was easy, since I just had to use the NetworkTrasform.
I also have a rough implementation for the attacking but there is a lot improvements to make. For example as it is right now, it is client authorative, since the server doesn't validate anything.
So I hoped you can have a look at my code and tell me what you think. what is bad practise and what can maybe stay this way. How would you solve this. I want do do it right. I am also thankful for any links to good resources, especially examples.

I am on FishNet 4.6.0R Pro

Code in next Message..

#
public class AttackControllerNetworked: NetworkBehaviour
{
    [SerializeField] private Rect attackRect;
    private readonly SyncVar<float> attackPower = new SyncVar<float>();

    public override void OnStartServer() {
        attackPower.Value = 30f;
    }


    private void Update() {
        if (!IsOwner) return;
        if (Input.GetKeyDown(KeyCode.E) || Input.GetKeyDown(KeyCode.Keypad0)) {
            AttackOwner();
        }
    }
    [Client]
    private void AttackOwner() {

        AnimateAttack();

        Vector2 boxPos = transform.TransformPoint(attackRect.position);
        Vector2 boxSize = transform.TransformVector(attackRect.size);

        var hits = Physics2D.OverlapBoxAll(boxPos, boxSize, 0);

        List<PlayerHealthNetworked> playersHits = new();
        foreach (var hit in hits) {
            if (hit.gameObject == gameObject) continue;
            var playerHealth = hit.GetComponent<PlayerHealthNetworked>();
            if(playerHealth != null) playersHits.Add(playerHealth);
        }

        AttackServer(new AttackArgs(attackPower.Value, playersHits.ToArray()));
    }

    [ServerRpc]
    private void AttackServer(AttackArgs args) {
        AttackClient();
        foreach(PlayerHealthNetworked h in args.playersHit) {
            h.ApplyDamage(attackPower.Value);
        }
    }

    [ObserversRpc(ExcludeOwner = true)]
    private void AttackClient() {
        AnimateAttack();
    }

    private void AnimateAttack() {
        //Todo Animate
    }
}```
#
public class PlayerHealthNetworked : NetworkBehaviour
{
    [SerializeField] private Image healthBarFill;

    private readonly SyncVar<float> health =  new SyncVar<float>();
    private readonly SyncVar<float> maxHealth = new SyncVar<float>();

    public override void OnStartServer() {
        maxHealth.Value = 100;
        health.Value = maxHealth.Value;
    }

    private void OnEnable() {
        health.OnChange += OnHealthChange;
    }

    private void OnHealthChange(float prev, float next, bool asServer) {

        float fillAmound = next / maxHealth.Value;
        healthBarFill.DOKill();
        healthBarFill.DOFillAmount(fillAmound, 0.1f);
    }

    [Server] public void ApplyDamage(float damage) {
        health.Value = Mathf.Clamp(health.Value - damage, 0, maxHealth.Value);
    }
}
knotty moss
#

I would really appreciate some help. Hope someone reads this.

dry jackal
#

Hi there!
The code looks good for a client auth attacking system.

#

For a server system you would send just the input of attacking and then do the hit checks entirely on the server side

#

As a small side tip you can place post code like this in Discord to utilize its code blocks:
```cs
// csharp code here
```

knotty moss
# dry jackal For a server system you would send just the input of attacking and then do the h...

But how do I check if it was a hit on the attackers screen like its described here: https://www.gabrielgambetta.com/lag-compensation.html
"Since the server gets all the input with timestamps, it can authoritatively reconstruct the world at any instant in the past. In particular, it can reconstruct the world exactly as it looked like to any client at any point in time."
How would I set up something like this? and for the hit check, would that even work with the OverlapBox method that I used. In this case I would have to move the players back to a previous ticks position to and then do the OverlapBox since it uses Unity physics.
So many questions in my head.

dry jackal
#

The demo should be in the demos folder in FishNet itself

knotty moss
#

thank you @dry jackal I will have a look