Input 类
GetKeyUp
//按键按下弹起的时候返回true
void Update () {
if (Input.GetKeyUp(KeyCode.A))
{
Debug.Log("A键已弹起");
}
}
GetKeyDown
//按下的时候返回true
void Update () {
if (Input.GetKeyDown(KeyCode.A))
{
Debug.Log("A键已弹起");
}
}
GetKey
//按键 一直按着的时候返回true
void Update () {
if (Input.GetKey(KeyCode.A))
{
Debug.Log("A键已按");
}
}
GetMouseButtonDown
//判断鼠标按键按下
//0是鼠标左键,1是鼠标右键,2是鼠标中间
if (Input.GetMouseButtonDown(0))
{
Debug.Log("按下了鼠标左键");
}
虚拟按键
在unity 引擎中不光提供了鼠标键盘的输入,也提供了 手柄 遥感的支持
点击unity界面中的Edit - project Settings - input
会发现上面9个与下面9个名字一样,这个是unity内置的,上面9个对应的是键盘的,下面9个对应的是手柄遥感的,使用键盘默认会使用上面这9个,使用手柄摇杆会使用下面的
GetAxis 获取轴
Horizontal
//获取轴
//可以 不用GetKey 让物体移动,是用GetAxis 使用虚拟按键 Horizontal 让物体平移
void Update () {
float hor = Input.GetAxis("Horizontal");
//水平方向需要*right 这样使用a d 或者← → 就可以让物体平移
transform.Translate(Vector3.right * hor * Time.deltaTime * 5);
}
同样方法用虚拟按键也可以让物体按AD键左右旋转(像魔兽世界那样)
Mouse X
float f = Input.GetAxis("Mouse X");
Debug.Log(f);
GetButten
//GetButten用来判断是否按下虚拟按键
if (Input.GetButton("Jump"))
{
Debug.Log("按下空格");
}
mousePosition
//获取鼠标的位置
debug.log(Input.mousePosition);
练习让物体跟随你的鼠标移动
Vector3 ver;
private void Start()
{
//使用相机吧世界左边转换成屏幕坐标
ver = Camera.main.WorldToScreenPoint(transform.position);
}
void Update () {
//将屏幕坐标转换成世界坐标 鼠标位置x轴 鼠标位置的y轴 相机与物体的z轴
Vector3 v= Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, ver.z));
//物体的位置跟随鼠标走,但z轴 还是 摄像机与 物体的z轴
transform.position=v;
}
collider 碰撞器
unity 中的碰撞器
OnMouseEnter
//当鼠标进入碰撞器 一瞬间
//下面的例子是当鼠标碰到物体后 物体会变成红色
//图片第一次没有变红色是因为 unity不是在最上层的时候不会运行game窗口
Material met;
private void Start()
{
met = GetComponent<MeshRenderer>().material;
}
private void OnMouseEnter()
{
met.color = Color.red;
}
OnMouseExit
//当鼠标离开物体一瞬间
Material met;
private void Start()
{
met = GetComponent<MeshRenderer>().material;
}
private void OnMouseEnter()
{
met.color = Color.red;
}
private void OnMouseExit()
{
met.color = Color.cyan;
}
OnMouseUpAsButton
//鼠标松开的一瞬间
Material met;
private void Start()
{
met = GetComponent<MeshRenderer>().material;
}
private void OnMouseEnter()
{
met.color = Color.red;
}
private void OnMouseExit()
{
met.color = Color.cyan;
}
private void OnMouseUpAsButton()
{
met.color = Color.yellow;
}
Rigidbody 刚体
velocity
//设置刚体速度
Rigidbody rig;
void Start () {
rig = GetComponent<Rigidbody>();
rig.velocity = Vector3.right * 5;
}
angularVelocity
//设置刚体角速度
Rigidbody rig;
void Start () {
rig = GetComponent<Rigidbody>();
rig.velocity = Vector3.right * 5;
rig.angularVelocity = Vector3.up * 5;
}
drag
//设置刚体阻力
Rigidbody rig;
void Start () {
rig = GetComponent<Rigidbody>();
rig.velocity = Vector3.right * 5;
rig.angularVelocity = Vector3.up * 5;
rig.drag = 5;
}
angularDrag
//设置刚体角阻力
Rigidbody rig;
void Start () {
rig = GetComponent<Rigidbody>();
rig.velocity = Vector3.right * 5;
rig.angularVelocity = Vector3.up * 5;
rig.drag = 5;
rig.angularDrag = 8;
}
useGravity
//设置刚体是否受重力影响
Rigidbody rig;
void Start () {
rig = GetComponent<Rigidbody>();
rig.velocity = Vector3.right * 5;
rig.angularVelocity = Vector3.up * 5;
rig.drag = 5;
rig.angularDrag = 8;
rig.useGravity = false;
}
collisionDetectionMode
//设置刚体碰撞检测模式
//他是枚举
rig.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
position
//刚体的位置
//注意 鱼和熊掌不可兼得(transform)
Rigidbody rig;
void Start () {
rig = GetComponent<Rigidbody>();
rig.position = Vector3.one*2;
}
AddForce
//给刚体添加一个世界坐标的力
Rigidbody rig;
void Start () {
rig = GetComponent<Rigidbody>();
rig.AddForce(Vector3.right * 500);
}
AddRelativeForce
//给刚体添加一个本地坐标力(相对力)
Rigidbody rig;
void Start () {
rig = GetComponent<Rigidbody>();
rig.AddRelativeForce(Vector3.right * 500);
}
AddTorque
//给刚体添加一个本地坐标的力矩
Rigidbody rig;
void Start () {
rig = GetComponent<Rigidbody>();
rig.AddTorque(Vector3.right * 1);
}
AddExplosionForce
//模拟一个爆炸效果
Rigidbody rig;
void Start () {
rig = GetComponent<Rigidbody>();
//得到指定球形半径以内所有物体身上的碰撞体组件
Collider[] cs = Physics.OverlapSphere(transform.position, 10);
for (int i = 0; i < cs.Length; i++)
{
cs[i].GetComponent<Rigidbody>().AddExplosionForce(200, transform.position, 10, 3);
}
}
OnCollisionEnter
//当碰撞器碰撞一瞬间
private void OnCollisionEnter(Collision collision)
{
collision.collider.GetComponent<MeshRenderer>().material.color = Color.cyan;
}
OnCollisionExit
//当碰撞器离开一瞬间
private void OnCollisionExit(Collision collision)
{
collision.collider.GetComponent<MeshRenderer>().material.color = Color.cyan;
}