जब भी क्लाइंट या होस्ट किसी निश्चित ऑब्जेक्ट पर कुंजी दबाता है तो गेम ऑब्जेक्ट को कैसे दिखाया जाए?


मेरे पास यहां यह कोड है जो एक गेम ऑब्जेक्ट प्रदर्शित करता है जब मैं कीबोर्ड पर ई दबाता हूं तो एकमात्र मुद्दा यह है कि यह होस्ट के साथ काम करता है और जब ग्राहक ई दबाता है तो उसके साथ इंटरैक्ट करता है तो कुछ नहीं होता है। समाधान क्या है या मैं क्या गलत कर रहा हूं और समय लेने के लिए धन्यवाद।

मैंने क्या प्रयास किया है:

using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.InputSystem;

public class InteractiveComputer : NetworkBehaviour
{
    public GameObject computerScreen;
    public float interactionRange = 3f; // Adjust the interaction range as needed
    private float displayDuration = 5f; // Duration to display the computer screen

    private bool isComputerScreenVisible = false;
    private bool isInteracting = false; // Flag to prevent multiple interactions

    private void Start()
    {
        if (IsServer)
        {
            isComputerScreenVisible = false;
            computerScreen.SetActive(false);
        }
    }

    bool IsPlayerInRange()
    {
        foreach (var player in NetworkManager.Singleton.ConnectedClientsList)
        {
            if (player != null && Vector3.Distance(transform.position, player.PlayerObject.transform.position) <= interactionRange)
            {
                return true;
            }
        }
        return false;
    }

    void Update()
    {
        if (!IsServer)
            return;

        // Check if the player is nearby
        if (IsPlayerInRange())
        {
            // Check if the 'e' key is pressed
            if (Input.GetKeyDown(KeyCode.E) && !isInteracting)
            {
                InteractWithComputerServerRpc();
            }

            // Check if the 'A' button on the gamepad is pressed
            if (Gamepad.current != null && Gamepad.current.buttonSouth.isPressed && !isInteracting)
            {
                InteractWithComputerServerRpc();
            }
        }
    }

    [ServerRpc]
    private void InteractWithComputerServerRpc()
    {
        isComputerScreenVisible = true;
        RpcUpdateComputerScreenVisibilityClientRpc(isComputerScreenVisible);

        // Set a timer to hide the computer screen after the specified duration
        Invoke(nameof(HideComputerScreenClientRpc), displayDuration);

        // Set interacting flag to prevent multiple interactions
        isInteracting = true;
    }

    [ClientRpc]
    private void RpcUpdateComputerScreenVisibilityClientRpc(bool isVisible)
    {
        isComputerScreenVisible = isVisible;
        computerScreen.SetActive(isVisible);

        // If the computer screen is shown on the client, set the interacting flag to true
        if (isVisible)
        {
            isInteracting = true;
        }
    }
[ClientRpc]
    private void HideComputerScreenClientRpc()
    {
        isComputerScreenVisible = false;
        computerScreen.SetActive(false);

        // Reset interacting flag after hiding the computer screen
        isInteracting = false;
    }
}

//works well but for the server can only interact but client no

コメント

タイトルとURLをコピーしました