1.创建一个地面。一个两个立方体,一个胶囊
2.给胶囊加一个刚体组件
3.给胶囊加 一个角色控制的脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NEW : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
// 水平轴
float horizontal = Input.GetAxis("Horizontal");
// 垂直轴
float vertical = Input.GetAxis("Vertical");
// 向量
Vector3 dir = new Vector3(horizontal, 0, vertical);
// 朝向范方向移动
transform.Translate(dir * 2 * Time.deltaTime);
}
}
4.把触发区立方体设置成是触发器
5.编写触发脚本并挂在到触发区juxing上
using System.Xml.Linq;
using UnityEngine;
public class PZ : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
}
// 进入触发
private void OnTriggerEnter(Collider other)
{
// 获取墙的物体
GameObject qiang = GameObject.Find("qang");
if (qiang !=null) {
qiang.SetActive(false);
}
}
// 触发中
private void OnTriggerStay(Collider other)
{
}
// 触发结束
private void OnTriggerExit(Collider other)
{
}
运行结果
当用键盘的wasd键控制胶囊到juxing触发区时,qiang消失
untiy 触发碰撞