我有一个小城市网格和一个公寓的三维模型。我将网格添加到我的场景中,并将三维模型放置在网格上。
我尝试处理鼠标只点击3d模型。我使用下面的代码。但是我点击屏幕上的任何地方,它的设置为声音= true
我添加了一个网格对撞机到三维模型游戏对象。
if (Input.GetMouseButtonDown (0)) {
Plane p = new Plane (Camera.main.transform.forward , transform.position);
Ray r = Camera.main.ScreenPointToRay (Input.mousePosition);
float d;
if(p.Raycast (r, out d)) {
sound = true;
}
我该怎么解决呢?
发布于 2016-03-25 09:46:11
您可以检查游戏对象的标签/名称。你需要这样使用:
void Update()
{
if (Input.GetMouseButtonDown (0)) {
Plane p = new Plane (Camera.main.transform.forward , transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
//Choose one of them below!!!
/// Name Comparison
if(hit.collider.gameObject.name.equals("NameOfTheObject")){
///Do Logic
}
//Tag Comparison
if(hit.collider.gameObject.CompareTag("TagOfTheObject")){
///Do Logic
}
}
}
}
https://stackoverflow.com/questions/36217174
复制相似问题