Object scale not syncronizing

Options

Hi, I'm trying to implement a damage zone in a game, similar to the ones in most BR games, but I'm having trouble reducing the scale of this zone object (restricting play area), the "DamageZone" object is spawned via Runner.Spawn(), it has a NetworkObject, and a NetworkTransform interpolating the child.

Prefab configuration

But ONLY the visuals (blue part) never sync between client and server, it just gets reduced really fast... it doesnt even sync between clients (it's slower on some clients)... the only place where it seems ok, is on the server, everything else is working fine, the collider scale seems to be getting reduced correctly and damage is getting applied when the player enters the zone...

I had to test it, moving my player on the client, but checking where the zone is, looking at the server...

To reduce the scale I'm doing something simple like this.,

Why could this be happening? sorry if it's maybe a noobish question. I haven't seen such scale changes in any sample, so I'm not sure how to validate if I'm missing some component or anything...

Thanks in advance.

Best Answer

  • Luke_Sta
    Luke_Sta admin
    Answer ✓
    Options
    // Using Networked property instead of transform ensures that clients stay in perfect sync.
    [Networked] Vector3 Scale { get; set; }
    
    
    public override void OnSpawn()
    {
        // get correct scale value on spawn from the transform.
        Scale = transform.localScale;
    }
    
    
    public override void FixedUpdateNetwork()
    {
        if Scale.x > 0 || Scale.z > 0)
        {
            // This will run on the server and predictively on each client.
            ShrinkZone();
        }
    
    
        // Ensures that the scale is kept up to date with the Networked state.
        transform.localScale = Scale;
    }
    
    
    public void ShrinkZone()
    {
        Scale -= new Vector3(decreaseVelocity * Runner.DeltaTime, 0f, decreaseVelocity * Runner.DeltaTime);
    }
    

    This should work.

Answers

  • Luke_Sta
    Options

    NetworkTransform only synchronizes position and rotation. If you want scale to be synchronized you have to put the scale value into a Networked property. Only modify the Networked property in FixedUpdateNetwork and at the very end of the FixedUpdateNetwork set the transform.local scale to the value of the property.

  • Hm, I don't quite follow this. We're changing the networked property in FixedUpdateNetwork and also setting the local scale after?

    public override void FixedUpdateNetwork()
    {
        base.FixedUpdateNetwork();
        Scale = transform.localScale;
        transform.localScale = Scale;
    }
    

    Doesn't seem right to me. How does this work?

    I'm also attempting to sync scale and I thought about going the networked property route with a callback to send the change to all proxies. I then would call a function to change the networked property's scale. But I'm more intrigued in your solution now.

  • Luke_Sta
    Luke_Sta admin
    Answer ✓
    Options
    // Using Networked property instead of transform ensures that clients stay in perfect sync.
    [Networked] Vector3 Scale { get; set; }
    
    
    public override void OnSpawn()
    {
        // get correct scale value on spawn from the transform.
        Scale = transform.localScale;
    }
    
    
    public override void FixedUpdateNetwork()
    {
        if Scale.x > 0 || Scale.z > 0)
        {
            // This will run on the server and predictively on each client.
            ShrinkZone();
        }
    
    
        // Ensures that the scale is kept up to date with the Networked state.
        transform.localScale = Scale;
    }
    
    
    public void ShrinkZone()
    {
        Scale -= new Vector3(decreaseVelocity * Runner.DeltaTime, 0f, decreaseVelocity * Runner.DeltaTime);
    }
    

    This should work.