Unity C#Raycast鼠标点击 [英] Unity C# Raycast Mouse Click
问题描述
我已经花了一整天的时间研究光从Unity C#Raycasting,我没有什么可以展示它。我已经学习了教程,在线资源,堆栈溢出问题,甚至有字复制脚本,希望Unity将最终认识到我实际使用Raycast的所有尝试。下面是一个使用Raycast的脚本示例,它对我来说不起作用:
I have literally spent all day researching the light out of Unity C# Raycasting and I have nothing to show for it. I have studied tutorials, online resources, stack overflow questions, and have even word for word copied script in hopes that Unity would finally recognize all my attempts to actually use a Raycast. Here is an example of a script using Raycast that simply won't work for me:
if (mouseDown) {
print ("mouse is down");
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)) {
print ("response???");
}
}
我觉得这应该可以工作...但它不。 mouseDown正在工作,因为它应该,但当我点击我的对象,它拒绝承认从我的鼠标位置到对象的rayhit。我还应该提到,该项目是在2D。任何建议?
I feel like this should work... but it's not. The mouseDown is working as it should but when I click on my object it refuses to acknowledge the rayhit from my mouse position to the object. I should also mention that the project is in 2D. Any suggestions?
推荐答案
1 。如果您要检测的对象是 Image
/ Canvas
,那么这不是怎么做的。要使用Image / Canvas检测触摸,您必须从 IPointerDownHandler
或 IPointerClickHandler
派生,然后从中实现函数。
1.If the Object you are trying to detect touch with is an Image
/Canvas
, then this is not how to do this. To detect touch with Image/Canvas, you use have to derive from IPointerDownHandler
or IPointerClickHandler
then implement the functions from them.
public class YourClass : MonoBehaviour,IPointerDownHandler,IPointerClickHandler
{
public void OnPointerClick(PointerEventData eventData)
{
Debug.Log("Clicked");
}
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("Down");
}
}
2 。现在如果GameObject你想要检测的触摸只是一个2D纹理或Sprite,然后使用下面的代码:
2.Now if the GameObject you want to detect the touch with is just a 2D Texture or Sprite then use the code below:
if (Input.GetMouseButtonDown(0))
{
Vector2 cubeRay = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D cubeHit = Physics2D.Raycast(cubeRay, Vector2.zero);
if (cubeHit)
{
Debug.Log("We hit " + cubeHit.collider.name);
}
}
$ c> Collider2D 添加到2D纹理或Sprite。确保碰撞器通过重新调整碰撞器的大小来覆盖2D纹理或Sprite。由于这是一个2D游戏,您使用的任何 collider
必须以 2D 结尾。例如,有一个 Box Collider
,并且有一个 Box Collider 2D
。您必须附加 Box Collider 2D
。到精灵/纹理。
For this to work, you must attach Collider2D
to the 2D Texture or Sprite. Make sure that the Collider is covering the 2D Texture or Sprite by re-sizing the collider. Since this is a 2D game, any collider
you are using must end with 2D.For example, there is a Box Collider
and there is a Box Collider 2D
. You must attach Box Collider 2D
. to the Sprite/Texture.
3 。如果#2 无法使用,那么您的项目已创建为 / strong>,而不是 2D 。删除项目,创建一个新项目,并确保您选择2D这一次。 #2 现在只要一个2D 对撞机
附在它上面,它就可以工作。
3.If #2 did not work, then your project was created as a 3D instead of 2D. Delete the project, create a new Project and make sure you choose 2D this time. #2 answer should now work as long as a 2D collider
is attached to it.
这篇关于Unity C#Raycast鼠标点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!