我如何在位于不同游戏对象的脚本中访问带有OnCollision的动画器?



当OnCollision在被触发的游戏对象上被调用时,它将与其他游戏对象上的动画师进行通信,以便它可以开始动画。

这里有这样的代码:

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

public class SmallDoor : MonoBehaviour
{
private Animator anim;

void Start()
{
anim = GetComponent<Animator>();
}

private void Update()
{
}

private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
anim.SetBool("DoorOpen", true);
}
}

您可以像这样从相同的脚本运行动画…

private Animator anim;

void Start()
{
anim = GetComponent<Animator>();
}

private void Update()
{
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
other.GetComponent<Animator>().SetBool("DoorOpen", true);
}

但是如果你想访问对象上的脚本,然后尝试这样做…

在另一个对象上确保它们有一个像这样的公共函数

public void Function()
{
//Insert code here
}

然后在主对象上继续

private Animator anim;

void Start()
{
anim = GetComponent<Animator>();
}

private void Update()
{
}
private void OnTriggerEnter(Collider other)
{
other.GetComponent<InsertOtherScriptName>.Function();
if (other.tag == "Player")
anim.SetBool("DoorOpen", true);
}

确保更改"insertotherscriptname"one_answers"功能()";无论你需要什么/拥有什么。

希望我能帮上忙

最新更新