ios 如何检测游戏对象上的触摸
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21409573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect touch on a game object
提问by Ashish Beuwria
I'm developing a game on Unity for iOS devices. I've implemented the following code for touch:
我正在 Unity 上为 iOS 设备开发游戏。我已经为触摸实现了以下代码:
void Update () {
ApplyForce ();
}
void ApplyForce() {
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) {
Debug.Log("Touch Occured");
}
}
I've dragged this script onto my game object which is a sphere. But the log message appears no matter where I touch. I want to detect touch only when the users touches the object.
我已将此脚本拖到我的游戏对象上,该对象是一个球体。但是无论我触摸哪里,都会出现日志消息。我只想在用户触摸对象时检测触摸。
回答by Steven Mills
stick the code bellow in your camera, or something else that will persist in the level you're designing. It can be anything, even an empty gameobject. The only important thing is that it only exists ONCE in your level, otherwise you'll have multiple touch checks running at the same time, which will cause some seriously heavy load on the system.
将下面的代码粘贴在您的相机中,或其他会保留在您设计的关卡中的东西。它可以是任何东西,甚至是一个空的游戏对象。唯一重要的是它在你的关卡中只存在一次,否则你将同时运行多个触摸检查,这会给系统带来一些严重的负载。
void Update () {
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay( Input.GetTouch(0).position );
RaycastHit hit;
if ( Physics.Raycast(ray, out hit) && hit.transform.gameObject.Name == "myGameObjectName")
{
hit.GetComponent<TouchObjectScript>().ApplyForce();
}
}
}
In the above script, hit.transform.gameObject.Name == "myGameObjectName"
can be replaced by any other method you want to check that the object hit by the raycast is the object you want to apply a force to. One easy method is by Tags for example.
在上面的脚本中,hit.transform.gameObject.Name == "myGameObjectName"
可以替换为任何其他方法,您要检查光线投射击中的对象是否是您要对其施加力的对象。例如,一种简单的方法是通过标签。
Another thing to keep in mind is that raycast will originate at your camera (at the position relative to your finger's touch) and hit the first object with a collider. So if your object is hidden behind something else, you won't be able to touch it.
要记住的另一件事是,光线投射将源自您的相机(在相对于您手指触摸的位置)并用碰撞器撞击第一个物体。因此,如果您的对象隐藏在其他东西后面,您将无法触摸它。
Also put the script bellow in your touch object (in this example, called TouchObjectScript.cs)
还将下面的脚本放在您的触摸对象中(在本例中,称为 TouchObjectScript.cs)
void Update () {
}
public void ApplyForce() {
Debug.Log("Touch Occured");
}
If something wasn't clear, or you need any further help, leave a comment and I'll get back to you.
如果有什么不清楚,或者您需要任何进一步的帮助,请发表评论,我会尽快回复您。
回答by Wiebren de Haan
The best way to do this, is just adding:
最好的方法是添加:
void OnMouseDown () {}
to the gameobject you want to be clicked. The problem with this is that you cannot press 2 things at one time.
到你想要点击的游戏对象。这样做的问题是您不能一次按 2 个东西。