我在游戏中有一个关卡,当您进入盒对撞机时,会打开一个门户,而当您离开它时,它会关闭。当我进入对撞机1时,两个门户都需要打开,而当我离开它时,两个门户都需要关闭。当我输入第二个门户的盒子对撞机2时,也需要发生这种情况。我有一个用于对撞机1的脚本,并将其应用于对撞机2。它检查播放器是否在对撞机中。我有一个动画器布尔,它直接从盒子对撞机脚本中获取变量以签入范围。我用那个布尔动画。但是,该动画器bool不适用于Box Collider2。Box Collider的变量有效,但animator bool无效。有没有办法连接第二个,或者我需要为那个盒子对撞机制作一个新的脚本?
Box Collider代码:
public bool inRange;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
inRange = true;
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
inRange = false;
}
}
}
门户网站脚本代码:
public class Portal : MonoBehaviour {
private Animator anim;
private bool inPortalRange;
public GameObject portalBorder;
void Start ()
{
anim = GetComponent<Animator>();
}
void Update ()
{
OpenPortal();
UpdateAnimation();
}
private void UpdateAnimation()
{
anim.SetBool("inPortalRange", inPortalRange);
}
private void OpenPortal()
{
PortalBorder poborder = portalBorder.GetComponent<PortalBorder>();
inPortalRange = poborder.inRange;
}
}
情况图片:
答案 0 :(得分:1)
首先,您不应该在每一帧使用ggplot() +
geom_sf(data = graph %>% activate(edges) %>% as_tibble() %>% st_as_sf(), col = 'darkgrey') +
geom_sf(data = graph %>% activate(nodes) %>% as_tibble() %>% st_as_sf(), col = 'darkgrey', size = 0.5) +
geom_sf(data = path_graph %>% activate(edges) %>% as_tibble() %>% st_as_sf(), lwd = 1, col = 'firebrick') +
geom_sf(data = path_graph %>% activate(nodes) %>% filter(id %in% c(from_node, to_node)) %>% as_tibble() %>% st_as_sf(), size = 2)
。像GetComponent
一样,您应该立即存储它。或者,您也可以简单地将anim
类型设置为portalBorder
,然后在通过检查器进行引用时自动设置相应的引用。
然后是的,当前您仅更新其中一个动画师。为了控制它们,您必须以某种方式连接它们。
我会做这样的事情
PortalBorder
但是,我实际上不是使用public class Portal : MonoBehaviour
{
private Animator anim;
private bool inPortalRange;
// Public read-only access
public bool InPortalRange => inPortalRange;
// Reference each other via the Inspector in both portals
public Portal OtherPortal;
// Give this directly the according type so you don't need GetComponent at all
public PortalBorder portalBorder;
// I would recommend to do things always as early as possible
// Awake is executed before Start
private void Awake()
{
anim = GetComponent<Animator>();
}
private void Update ()
{
OpenPortal();
UpdateAnimation();
}
private void UpdateAnimation()
{
// Here now use the range of either this or the other portal
anim.SetBool("inPortalRange", InPortalRange || OtherPortal.inPortalRange );
}
private void OpenPortal()
{
inPortalRange = portalBorder.inRange;
}
}
中的轮询呼叫,而是使用事件驱动的方法:
Update
现在您的脚本必须UnityEvent
(就像按钮的public class PortalBorder : MonoBehaviour
{
public UnityEvent OnEnteredPortalRange;
public UnityEvent OnLeftPortalRange;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
OnEnteredPortalRange.Invoke();
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
OnEnteredPortalRange.Invoke();
}
}
}
一样),您可以在其中通过检查器或使用代码添加回调
onClick