naverz / zepeto-script-sample

38 stars 7 forks source link

오브젝트 충돌 시 서버간 통신 및 클라이언트 별 UI출력 #60

Closed huri92 closed 2 years ago

huri92 commented 2 years ago

안녕하세요, 현재 특정 오브젝트와 충돌 시 다중 클라이언트에게 같은 메시지가 가는 현상이 발생 하였는데 해결 방법을 모르겠습니다.

아래는 클라이언트 코드 예시 입니다.

 OnTriggerEnter(other: Collider) {

        if(this.m_IsTrigger == false)
        {
                const data = new RoomData();
                data.Add("typeofgames", this.m_strObjType);
                this.m_room.Send("ActiveColliderEnter", data.GetObject());
                this.m_UI_Event.ShowUI();
            }
            else
            {
                this.m_room.Send("ActiveColliderEnter","Death");
            }
            this.m_IsTrigger = true;
        }
    }

아래는 서버 코드 예시입니다.

onCreate(options: SandboxOptions) {
            this.onMessage("ActiveColliderEnter", (client, message)=>{
            const player = this.state.players.get(client.sessionId);

            console.log(message.typeofgames);
            player.typesOfgames = message.typeofgames;
        });
}

아래는 스키마 코드 예시입니다.

    interface State extends Schema {
        players: MapSchema<Player>;
    }
    class Player extends Schema {
        sessionId: string;
        zepetoHash: string;
        zepetoUserId: string;
        transform: Transform;
        state: number;
        typesOfgames: string;
    }
    class Transform extends Schema {
        position: Vector3;
        rotation: Vector3;
    }
    class Vector3 extends Schema {
        x: number;
        y: number;
        z: number;
    }

유저가 오브젝트와 충돌 하였을때 OnTriggerEnter를 호출 하고 서버로 메시지를 보냅니다. 서버는 닿은 유저를 찾아내고 스키마 데이터를 수정합니다. 수정한 데이터를 받은 클라이언트에서 ShowUI메소드를 호출하여 UI를 출력하는데 클라이언트에서 모든 클라이언트에게 충돌 되었다는 메시지와 UI가 동시 출력 되는 현상입니다.

유니티 하이어라키에는 출력될 Canvas UI와 충돌을 감지하는 Collider가 배치 되어 있는 상황입니다.

여기서 어떻게 충돌 된 유저만 서버에 있는 스키마 데이터를 수정하고 해당 유저에게만 UI를 표시 할 수 있는지 답변 주시면 감사하겠습니다.

***추가적으로 Teleport 메소드 사용시 유저간 동기화가 안되는 현상이 있는데, 의도 된것인지 의도 된것이라면 유저를 어떻게 동기 상태로 Teleport시킬 수 있는지 궁금합니다.

const myPlayer = ZepetoPlayers.instance.GetPlayer(this.m_room.SessionId);
        myPlayer.character.Teleport(this.m_Dest.localPosition, this.m_Dest.rotation);

감사합니다.

huri92 commented 2 years ago

자문 자답입니다. 이는 클라이언트 각자 충돌 처리 될 로직이 밖으로 나와있어 발생했던 상황입니다. 단순히 플레이어 또는 플레이어가 독자적으로 가질만한 오브젝트에 충돌로직을 설정 해주어야 합니다.