using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class door : MonoBehaviour
{
// private Transform door_transform;
public GameObject AnimationObj;
private Animation animation_A;
// Start is called before the first frame update
void Start()
{
animation_A = gameObject.GetComponent<Animation>();
}
void OnTriggerEnter(Collider coll)
{
if (coll.gameObject.name == "Student")
{
//AnimationObj.GetComponent<Animation>()["OpenDoorAni"].time = AnimationObj.GetComponent<Animation>()["OpenDoorAni"].clip.length;
//AnimationObj.GetComponent<Animation>()["OpenDoorAni"].speed = 1.0f;
//AnimationObj.GetComponent<Animation>().CrossFade("OpenDoorAni");
animation_A.Play("OpenDoorAni");
}
}
void OnTriggerExit(Collider coll)
{
// animation01["OpenDoorAni"].time = animation01["OpenDoorAni"].clip.length;
if (coll.gameObject.name == "Student")
{
AnimationObj.GetComponent<Animation>()["OpenDoorAni"].time = AnimationObj.GetComponent<Animation>()["OpenDoorAni"].clip.length;
AnimationObj.GetComponent<Animation>()["OpenDoorAni"].speed = -1.0f;
AnimationObj.GetComponent<Animation>().CrossFade("OpenDoorAni");
}
}
}
该代码段展示了一个Unity3D场景中,当学生角色触发门时,门自动开启和关闭的实现。使用了Collider组件检测碰撞,Animation组件播放预设的'OpenDoorAni'动画。在角色进入和离开触发区域时,分别控制动画的播放方向以实现门的开关效果。

2万+

被折叠的 条评论
为什么被折叠?



