功能实现
1.一开始隐藏物体1
2.物体2到达物体1的指定范围后激活物体1
3.物体2离开物体1后隐藏物体1
实现步骤
- 创建物体1,物体2,空物体
- 将空物体的trigger事件√起,并collider的size
- 将物体1作为空物体的子物体
- 将移动的代码赋予物体2
- 将隐藏和显示的代码赋予空物体
代码1 移动
private Transform m_Transform;
void Start () {
m_Transform = gameObject.GetComponent<Transform>();
}
void Update () {
MoveControl();
}
void MoveControl()
{
if (Input.GetKey(KeyCode.W))
{
m_Transform.Translate(Vector3.forward * 0.1f, Space.Self);
}
if (Input.GetKey(KeyCode.S))
{
m_Transform.Translate(Vector3.back * 0.1f, Space.Self);
}
if (Input.GetKey(KeyCode.A))
{
m_Transform.Translate(Vector3.left * 0.1f, Space.Self);
}
if (Input.GetKey(KeyCode.D))
{
m_Transform.Translate(Vector3.right * 0.1f, Space.Self);
}
if (Input.GetKey(KeyCode.Q))
{
m_Transform.Rotate(Vector3.up, -1.0f);
}
if (Input.GetKey(KeyCode.E))
{
m_Transform.Rotate(Vector3.up, 1.0f);
}
m_Transform.Rotate(Vector3.up, Input.GetAxis("Mouse X"));
m_Transform.Rotate(Vector3.left, Input.GetAxis("Mouse Y"));
}
代码2隐藏与显示
public GameObject hideshow1;
void Start()
{
hideshow1.SetActive(false); //一开始隐藏物体1
}
private void OnTriggerEnter(Collider other)// trigger 事件
{
if (hideshow1 == null)
return;
hideshow1.SetActive(true);
}
private void OnTriggerExit(Collider other)
{
if (hideshow1 == null)
return;
hideshow1.SetActive(false);
}
// 借鉴原文链接,侵删:https://blog.csdn.net/qq_42007287/java/article/details/104954206
转载:https://blog.csdn.net/weixin_43935977/article/details/106529404
查看评论