- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
请看下面的图片。
在第一张图片中,您可以看到有一个盒子碰撞器。第二张图是我在安卓设备上运行代码的时候
这是附加到 Play Game 的代码(它是一个 3D 文本)
using UnityEngine;
using System.Collections;
public class PlayButton : MonoBehaviour {
public string levelToLoad;
public AudioClip soundhover ;
public AudioClip beep;
public bool QuitButton;
public Transform mButton;
BoxCollider boxCollider;
void Start () {
boxCollider = mButton.collider as BoxCollider;
}
void Update () {
foreach (Touch touch in Input.touches) {
if (touch.phase == TouchPhase.Began) {
if (boxCollider.bounds.Contains (touch.position)) {
Application.LoadLevel (levelToLoad);
}
}
}
}
}
我想看看触摸点是否在对撞机内部。我想这样做是因为现在如果我点击场景中的任何地方 Application.LoadLevel(levelToLoad);叫做。
我希望仅在单击 PLAY GAME 文本时调用它。任何人都可以帮助我处理这段代码或者可以为我的问题提供另一种解决方案吗??
遵循 Heisenbug 逻辑的最新代码
void Update () {
foreach( Touch touch in Input.touches ) {
if( touch.phase == TouchPhase.Began ) {
Ray ray = camera.ScreenPointToRay(new Vector3(touch.position.x, touch.position.y, 0));
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, 10)) {
Application.LoadLevel(levelToLoad);
}
}
}
}
最佳答案
触摸的位置在屏幕空间坐标系(Vector2
)中表示。在尝试将其与场景中对象的其他 3D 位置进行比较之前,您需要在世界空间坐标系中转换该位置。
Unity3D
提供了执行此操作的工具。由于您在文本周围使用 BoundingBox
,因此您可以执行以下操作:
Ray
,原点在触摸点位置,方向平行于相机前轴 (Camera.ScreenPointToRay)。GameObject
(Physic.RayCast) 的BoundingBox
相交。代码可能看起来像这样:
Ray ray = camera.ScreenPointToRay(new Vector3(touch.position.x, touch.position.y, 0));
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerOfYourGameObject))
{
//enter here if the object has been hit. The first hit object belongin to the layer "layerOfYourGameObject" is returned.
}
将特定层添加到您的“玩游戏”GameObject
很方便,以使光线仅与其发生碰撞。
编辑
上面的代码和解释很好。如果你没有得到正确的碰撞,可能你没有使用正确的层。我目前没有触摸设备。以下代码适用于鼠标(不使用图层)。
using UnityEngine;
using System.Collections;
public class TestRay : MonoBehaviour {
void Update () {
if (Input.GetMouseButton(0))
{
Vector3 pos = Input.mousePosition;
Debug.Log("Mouse pressed " + pos);
Ray ray = Camera.mainCamera.ScreenPointToRay(pos);
if(Physics.Raycast(ray))
{
Debug.Log("Something hit");
}
}
}
}
这只是一个让您朝着正确方向前进的例子。尝试找出您的情况出了什么问题或发布 SSCCE .
关于android - 检查触摸点是否在 Unity 中的盒子碰撞器内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16442728/
这个问题在这里已经有了答案: CSS3 Flexbox: display: box vs. flexbox vs. flex (3 个答案) 关闭 1 年前。 今天我们中的许多人都知道 displa
$('.finalLink').html(" Click to Follow"); 我正在尝试创建一个以 super 框模式打开的链接。尝试了灯箱,结果相同。这些链接有效,但只是转到该网站。如果我静态
我使用了 RayHandler.useDiffuseLight(true) ,现在无论我设置什么环境光值,屏幕上未照亮的部分都是黑色的。这是正常的吗?我见过其他用 libgdx 制作的应用程序,它同时
我需要帮助解决一个简单的问题。我的任务即将结束,我们要制作不同的艺术人物。我制作了一个“正方形内的正方形”盒子,需要生成该盒子的 4 行和 4 列。 我认为最好的解决方案是多一些 for 循环,但不能
我怎样才能实现this effect使用 CSS 和/或 jQuery? 我会发布到目前为止我编写的代码,但它没有用...... 最佳答案 使用jQuery Masonry或Isotope为了达成这个
我写了下面的代码: for row in range(len(listOfLists)): print('+' + '-+'*len(listOfLists)) print('|',
我想知道如何在 CSS3 中制作成 Angular 盒子。喜欢这个网站: http://themeluxe.com/themes/glissando/ (白人) 我怎样才能使边框看起来更好、更平滑。
我正在尝试实现具有三个垂直部分的简单布局: 页面顶部的小标题。 主要内容,必要时展开,以便页脚保留在页面底部。 页面底部的页脚。 主要内容部分应进一步分为两部分:一个带有输入框和按钮的非常小的表单,以
我正在尝试构建如下图所示的网格 IMG 链接:http://postimg.org/image/qo3b4nof1/ 但我得到的 DIV E 几乎在 D-DIV 旁边 这是我的代码
如何修改fancybox盒子的定位?我希望它被定位,以便它漂浮在我的容器 div 中... 感谢您的宝贵时间和帮助! 最佳答案 jquery.fancybox-1.2.6 的第 255 行有这段代码
这个问题在这里已经有了答案: Inset border-radius with CSS3 (8 个答案) 关闭 9 年前。 border-radius 属性可以使盒子的 Angular 变圆。但是如
我创建了一个包装器元素,其中包含 2 个彼此相邻的行内 block 框(图片中为浅绿色和黄色)。然后我创建了另外两个 div,它们都嵌套在其中一个内联 block 中以使内容居中。它可以工作,但由于某
我在使用 WordPress 时遇到问题,在盒子中嵌套盒子。在 WordPress 之外,这工作正常。我确定只有一两个参数有误。 外框为dp23,内框为dp22由于某种原因,内框 (dp22) 之间存
我目前尝试在容器内排列盒子的结果是这样的输出。 但我想要实现的是, 用最大数量的框填充每一行,但每个框都有固定的填充和边距。我相信我已经做到了。 我需要每行中的单个框来覆盖剩余空间,间隙仅为 1 像素
我在下面有多个 flexbox 的代码。当页面展开时,我如何将它们垂直居中放置在页面中?我尝试在我的 flex-container 中使用 justify-content: center; 但当我使用
我有这样的布局: 我
我是编程新手,我注意到有人提到了 flex box。在阅读了很多相关内容之后,它似乎对我有用。唯一的问题是我在安装它时遇到了很多麻烦,即使在搜索了大约一个小时的帮助之后也是如此。我从 github 下
Vagrantfile有问题: # -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure("2") do |config| config
我正在尝试下载一个 Vagrant 的盒子,但他们的服务器此时非常缓慢。他们有公共(public)镜子吗?我想下载precision64.box 文件。 谢谢 最佳答案 尝试更可靠的: Vagrant
在一个父容器内,三个 div 彼此重叠。顶部 div 是固定高度。底部 div 的内容占用了未知数量的垂直空间,但需要显示其中的所有内容。顶部 div 应填充剩余的垂直空间。每一个 // 100%
我是一名优秀的程序员,十分优秀!