Update methode not really needed?

I think that checking every frame if the respawningCoin has moved is not the best way. This doesn’t seem to be optimised.
Can’t we store instead a vector2 property in the RespawningCoin Class with getter and setter. or is there something i’m missing ?
When the value is Set, then we’ll do “Show(true)”.

What do you think ?

Edit : I tried to do it that way, but that doesn’t seems to work because the code is triggered on the server side, I understand why you did it that way.
Still looking for another option… :stuck_out_tongue:

well, it is not possible to inherit from the Transform class in Unity and directly modify the value of the position property. The Transform class is a sealed class in Unity, which means it cannot be inherited. so I don’t know if this is possible to do it another way.

My conclusion is that we might use a boolean and synch it accross the clients… But it comes with a traffic cost (more data to be send). Your way makes more calls on the clients but the network is less disturbed.

Please let me know what you think about my conclusion and why you choose this implementation. :slight_smile:

Well I think I find a better way.
It’s better to get rid of the networktransform component and use a networkVariable for the position. We don’t change the position often. We can check if the value has changed and execute the code on all client + server.

here’s part of my code :

public class RespawningCoin : Coin
{
public NetworkVariable pos = new NetworkVariable();

public event Action<RespawningCoin> OnCollected;

public override void OnNetworkSpawn()
{
    pos.OnValueChanged += OnPosValueChanged;
}

public override void OnNetworkDespawn()
{
    pos.OnValueChanged -= OnPosValueChanged;
}

private void OnPosValueChanged(Vector2 oldValue, Vector2 newValue)
{
    if (oldValue == newValue) return;
    
    transform.position = new Vector3(pos.Value.x, pos.Value.y, 0);
    Show(true);
}

// …
}

So in the CoinSpawner, we don’t change the transform anymore, juste the variable :

private void HandleCoinCollected(RespawningCoin coin)
{
    Vector2 spawnPos = GetSpawnPoint();
    //coin.transform.position = spawnPos;
    coin.pos.Value = spawnPos;
    coin.Reset();
}

I tested, everything works fine :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms