1.
(1)对某个模型改变其中心点,可以通过父子关系进行更改,创建一个空物体,将该物体放入空物体中,同时将中心点更改到边缘。
(2)确定中心位置
(3)使用键盘按键实现旋转: 将改脚本添加到被旋转物体上
private Transform m_Transform; //定义变量
// Start is called before the first frame update
void Start()
{
m_Transform = gameObject.GetComponent<Transform>(); //实例化
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Z)) //旋转按键,按下旋转90度
{
openDoor();
}
if (Input.GetKeyUp(KeyCode.Z)) //旋转按键,松开旋转90度
{
closeDoor();
}
}
public void openDoor() //定义函数,开门
{
m_Transform.Rotate(Vector3.up,90); //沿y轴方向,旋转90度
}
public void closeDoor() //定义函数,关门
{
m_Transform.Rotate(Vector3.up,-90); //沿y轴方向,向反方向旋转90度
}
2.触发器触发开关 通过GameObject.Find("door") 指定存在door脚本的物体,进行查找和关联
(1)对物体创建box collider,同时选中 “Is Trigger” 建立触发器
(2)调整触发器中的边距,检测是否触发
(3)将脚本添加到该触发器物体上
private Door m_door; //定义Door脚本的变量
// Start is called before the first frame update
void Start()
{
m_door = GameObject.Find("door").GetComponent<Door>(); //通过名称查找到door脚本存在的物体
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter(Collider other) //触发体,若接触到触发体,执行该函数
{
if (other.gameObject.name == "Student") //判断接触触发体物体的名称,若为student,执行door脚本中的openDoor()
{
m_door.openDoor();
}
}
void OnTriggerExit(Collider other) //触发体,若接触到触发体,执行该函数
{
if (other.gameObject.name == "Student") //判断接触触发体物体的名称,若为student,执行door脚本中的closeDoor()
{
Debug.Log("b");
m_door.closeDoor();
}
}