ServerRpc Coroutine

Very simple: How do I call a IEnumerator function?
I would need it for my Players footstep generation. I would need to call a infinit loop which checks whether the player is running or not and if so he will spawn one and delay the next by .3 seconds. For that i need a “yield return new Waitforseconds()” and for that i need a coroutine. Now, as soon as i try to declair my Coroutine as a ServerRpc I get

Error: “(0,0): error - RPC method must return void!” as an error.
Code:

void Start(){
StartCoroutine( FootstepGenServerRpc());
}


[ServerRpc]
    private IEnumerator FootstepGenServerRpc(){
        begin:
        for( ;steping; ){
            GameObject step = Instantiate(Footsteps,new Vector3(transform.position.x, transform.position.y, 121f), transform.rotation);
            step.GetComponent<NetworkObject>().Spawn();
            yield return new WaitForSeconds(.3f);
            if(!steping) break;
            step = Instantiate(Footsteps2, new Vector3(transform.position.x, transform.position.y, 121f), transform.rotation);
            step.GetComponent<NetworkObject>().Spawn();
            yield return new WaitForSeconds(.3f);
        }
        if(!steping){
            yield return new WaitForSeconds(.3f);
            goto begin;
        }
    }

Call an RPC that then starts the coroutine.
However, this isn’t a good footstep implementation. You should check the distance moved and if it exceeds a certain step width, you play a sound. If you do it time based it will sound wrong, especially if the player move speed varies and isn’t an on/off movement with instant stop.

3 Likes