using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorTrigger : MonoBehaviour {
private Door m_Door;
public Transform door;
// Use this for initialization
void Start () {
//通过GameObject.Find查找DoorParentParent这个物体中的Door ,m_door的实例化
m_Door = GameObject.Find("DoorParent").GetComponent<Door>();
}
// Update is called once per frame
void Update () {
}
private void OnTriggerEnter(Collider coll) //当触发的名字是“Student”的时候,实现关门操作
{
if (coll.transform.name == "Student")
{
print("Open");
door.transform.rotation = Quaternion.Euler(new Vector3(0, 90, 0));
}
}
private void OnTriggerExit(Collider other)
{
if (other.transform.name == "Student")
{
print("Open");
door.transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Door : MonoBehaviour {
private Transform m_Transform; //定义一个Transform中的变量
void Start () {
m_Transform = gameObject.GetComponent<Transform>(); //实例化Transform中的对象
}
public void OpenDoor()
{
m_Transform.Rotate(Vector3.up, 90); //通过对象.Rotate实现开门操作
}
public void CloseDoor()
{
m_Transform.Rotate(Vector3.up, -90); //通过对象.Rotate实现关门操作
}
private void OnCollisionEnter(Collision collision)
{
if (collision.transform.name== "Student")
{
print("Open");
transform.rotation=Quaternion.Euler(new Vector3(0, 90, 0));
}
}
private void OnCollisionExit(Collision collision)
{
}
}
网友评论