c#unity-穆特派仪与对象相互作用


  using UnityEngine;
using UnityEngine.Networking;
public class Interactable : NetworkBehaviour {
    public float radius = 2f;
    bool isFocus = false;
    Transform player;
    bool hasInteracted = false;
    public Transform interactionPoint;

    public virtual void Interact()
    {
        // This method is meant to be overwritten
        Debug.Log("Interacting with " + transform.name);
    }
    private void Update()
    {
        if (isFocus && !hasInteracted)
        {
            float distance = Vector3.Distance(player.position, interactionPoint.position);
             if(distance <= radius)
            {
                Interact();
                hasInteracted = true;
            }

        }

    }
    public void onFocused(Transform Player)
    {
        isFocus = true;
        player = Player;
    }
    public void onDeFocused()
    {
        isFocus = false;
        player = null;
        hasInteracted = false;

    }
    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.yellow;
        Gizmos.DrawWireSphere(transform.position, radius);
    }
}

这是用于覆盖的互动。

 using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Networking;

public class SphereBehaviour : Interactable {
    public float countdown = 1;
    public float timeLeft = 5f;

    public override void Interact()
    {
        Debug.Log("Interacting with the Sphere object");
        StartCoroutine(StartCountdown());

    }
    public IEnumerator StartCountdown() // Countdown from 5
    {
        countdown = timeLeft;
        while (countdown > 0)
        {
            yield return new WaitForSeconds(1.0f);
            countdown--;
        }
    }

问题是,这不是网络,我想不出要做的逻辑。接口无法正常工作。是否有可能不需要更改所有内容的解决方案?

我只是想将YouTube教程的布拉克基集转为多人游戏。

我假设您正在为某些UI元素使用公共"倒数"变量。

  1. 如果您希望每个玩家在触摸这个领域时看到自己的倒数,这应该很好。

  2. 如果有人触摸球体的人应触发每个人的倒计时,那么必须以不同的方式完成。您将希望主机/服务器跟踪玩家和球之间的互动,然后在客户端触发Countdown Coroutine。

我个人在任何情况下都不建议#1,因为应该完成碰撞/交互。

最新更新