Unity 射线移动场景物体

 

 脚本 物体 画面

 

 

 

通过获取射线的坐标进行移动的

 

void Update()
{
AGF();


}

public void AGF()
{
if (Input.GetMouseButton(1))
{
//1获得鼠标点击屏幕的二维坐标
Vector3 mouse = Input.mousePosition;
//2创建一个射线 从相机位置到鼠标点击的位置的方向上
Ray ray = Camera.main.ScreenPointToRay(mouse);
//定义一个射线检测的保存对象
RaycastHit hit;//hit保存射线发射后碰撞的信息
//开始碰撞检测,射线检测返回一个bool类型,true:有碰撞,false:无碰撞
if (Physics.Raycast(ray, out hit,100f)) //碰撞检测(射线,碰撞结果给hit,射线的长度100米)
{
print(hit.point);
game.transform.position= new Vector3(hit.point.x, 2f, hit.point.z);
}
}
}
public GameObject game;
}

 

 

 

 

 就这样

posted @ 2020-10-21 15:52  手札记  阅读(483)  评论(0编辑  收藏  举报