鼠标拾取
function Update ()
{
if (Input.GetMouseButton (0)) //鼠标左键按下
{ //选择第一个maincamera然后调用它的“从屏幕某点发送射线”函数
//参数为鼠标位置
var ray = Camera.main.ScreenPointToRay (Input.mousePosition); //返回值为射线
var hit : RaycastHit; //光线投射碰撞结果结构体
if (Physics.Raycast (ray, hit)) //投射,结果保存在hit中
{
Debug.DrawLine (ray.origin, hit.point); //可省略,画出这条线
print(hit.collider.gameObject.name); //碰撞器的gameobject的名字
}
}
}
Unity中的输入控制
第一种
直接从input得到按键的状态。
}
第二种
设置好axis,选择菜单中的edit-project settings-input在inspector中设置好参数
然后在需要获得输入的地方直接调用
// A very simplistic car driving on the x-z plane.
// 一个十分简单的在x-z平面的驾车例子
// Get the horizontal and vertical axis.
//获取横向和纵向坐标轴
// By default they are mapped to the arrow keys.
//默认情况下他们关联到方向键上
// The value is in the range -1 to 1
//值的范围是在-1到1之间
// Make it move 10 meters per second instead of 10 meters per frame...
// 使它每帧移动10米变为每秒移动10米...
// Move translation along the object's z-axis
//沿着z轴平移对象
// Rotate around our y-axis
//以我们的y轴为中心旋转
}
使用控制器和键盘输入时返回值范围在-1到1之间。如果坐标轴设置为鼠标运动增量,鼠标增量乘以坐标轴灵敏度的范围将不是-1到1 。