【发布时间】:2015-04-29 19:44:51
【问题描述】:
我尝试了一些处理对象点击的代码示例,但它们不起作用。
我在场景中有对象的网格:
在主摄像头上有一个带有代码的 C# 脚本组件:
using UnityEngine;
using System.Collections;
public class cameraAnim3 : MonoBehaviour
{
void Update() {
if (Input.GetMouseButtonDown (0)) { // if left button pressed...
print ("cli!!!");
// create a ray passing through the mouse pointer:
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) { // if something hit...
print ("clicked on object!!!");
// if you must do something with the previously
// selected item, do it here,
// then select the new one:
Transform selected = hit.transform;
selected.gameObject.SetActive (true);
print (selected.gameObject.name);
// do whatever you want with the newly selected
// object
}
}
}
}
当我在头部网格上单击左键时,在控制台消息“cli!!!”中显示,但没有消息“点击对象!!!”显示出来了。
如何捕捉点击这个网格?
【问题讨论】:
-
请不要使用
unity标签来回答与 Unity 游戏引擎相关的问题。在使用之前阅读标签描述通常是一个好主意。 -
你没有在对象上安装 colider
标签: unity3d unityscript