#Slow Sync On Transform Objects

12 messages · Page 1 of 1 (latest)

astral hare
#

So essentially im prototyping a combat game. With which hits will launch the players, and when they collide with objects tagged as "Terrain" will use a vector3.reflect function to find what the angle would be. This is all done with a character controller move function that essentially moves the character based on these parameters + gravity. However, the synchronization when this happens is horrendous, it struggles to keep up and often times ends up at the wrong place where in it slowly slides the player through the air towards where they actually are on their screen. Any idea what might be causing this, its is my optimization, is there a setting I might be missing, or is the player just moving too fast to sync reliably. All the other movement calls like physically moving, or moving with set distances and shapes through attacks work perfectly.

worldly heart
#

Would need to see code and components

astral hare
#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FishNet.Object;

public class MoveForcePlayer : NetworkBehaviour
{
    public Vector3 directionOfForce;
    public float powerOfForce;
    public float forceTime;
    private float startLerpForce;
    private float timeForceApplied;
    [SerializeField]
    private int framesofImpact;
    private bool waitingToRebound;
    [SerializeField]
    public bool startForce;
    [SerializeField]
    private bool tween;
    private float timer;
    public bool lookingForGround;

    [SerializeField]
    private LayerMask terrain;

    [SerializeField]
    private float forceDiminishSpeed;
    private CharacterController player;
    private PlayerControl playerControl;

    // Start is called before the first frame update
    void Start()
    {
        player = GetComponent<CharacterController>();
        playerControl = GetComponent<PlayerControl>();
    }

    // Update is called once per frame
    void Update()
    {
        if (startForce)
        {
            MovePlayer();
        }
    }

    [ServerRpc(RequireOwnership = false)]
    public void RequestAddForce(Vector3 forceDir, float forceSpeed, float forceSetTime)
    {
        RequestAddForceRPC(forceDir, forceSpeed, forceSetTime);
    }

    [ObserversRpc]
    public void RequestAddForceRPC(Vector3 forceDir, float forceSpeed, float forceSetTime)
    {
        directionOfForce = forceDir;
        powerOfForce = forceSpeed;
        startLerpForce = powerOfForce;
        forceTime = forceSetTime;
        startForce = true;
        tween = true;
        playerControl.isGrounded = false;
    }

    [ServerRpc(RequireOwnership = false)]
    private void MovePlayer()
    {
        MovePlayerRPC();
    }

    [ObserversRpc]
    private void MovePlayerRPC()
    {
        if (waitingToRebound)
        {
            playerControl.fallSpeed = 0f;
            framesofImpact++;
            
            if (framesofImpact >= powerOfForce / 1.5f)
            {
                waitingToRebound = false;
                framesofImpact = 0;
            }
            return;
        }

        if (tween)
        {
            float t = timeForceApplied / forceTime;
            t = Mathf.Sin(t * Mathf.PI * 0.5f);
            powerOfForce = Mathf.Lerp(startLerpForce, 0f, t);
        }

        if (directionOfForce != new Vector3(0, 0, 0))
        {
            player.Move(directionOfForce * powerOfForce * Time.deltaTime);
            timeForceApplied += Time.deltaTime;

            if (timeForceApplied >= forceTime)
            {
                powerOfForce = 0f;
                directionOfForce = new Vector3(0, 0, 0);
                timeForceApplied = 0f;
                timer = 0;
                startForce = false;
            }
        }
    }

    public void BreakMovement()
    {
        powerOfForce = 0f;
        directionOfForce = new Vector3(0, 0, 0);
        timeForceApplied = 0f;
        timer = 0;
        startForce = false;
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.layer == 8)
        {
            startLerpForce = powerOfForce;
            startLerpForce *= 0.8f;
            waitingToRebound = true;
            PlayerRebound(collision);
        }
    }

    private void PlayerRebound(Collision col)
    {
        if (powerOfForce < 15f)
        {
            BreakMovement();
            return;
        }
        else
        {
            Vector3 dir = Vector3.Reflect(directionOfForce, col.contacts[0].normal);
            directionOfForce = dir.normalized;
        }
    }



    // MAKE IT LERP AS LONG AS YOU WANT THE CC TO LAST
    // ON GOD
}
#

If there is anything else I can provide please let me know

worldly heart
#

This should probably be a TargetRpc sent to owner.

    [ObserversRpc]
    public void RequestAddForceRPC(Vector3 forceDir, float forceSpeed, float forceSetTime)
    {```
#

You are telling all players to apply the force which is fighting the NT results.

#

By sending to only owner, the owner applies forces, updates servers with values via NT and server updates clients.

astral hare
toxic violetBOT
#

FirstGearGames received thanks.

worldly heart
astral hare
worldly heart
#

Lol the last time I used chatgpt to get some code it gave me a block of code that was allocating every frame.