Physics2D.Raycast返回null [英] Physics2D.Raycast returning null

查看:466
本文介绍了Physics2D.Raycast返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个让我超级困惑的问题.我要从键码"转到鼠标输入-然后又变成触摸输入,但是首先我想弄清楚为什么我不能只用鼠标来做到这一点.

I have a bit of a issue that has me super stumped here. I am going from Key codes , to mouse input - then it later become touch input, but first I want to figure out why i cant do this with just a mouse.

我有一个raycast2d设置-我想让raycast读取与我在屏幕上的对象的碰撞.他们只会在其物体标记为猫"的情况下做出反应-本质上,一旦发生这种情况,猫就会滑出并尝试攻击.但是,它告诉我tagg本身是实例化的引用.但是对象本身默认存在,所以我不确定在这里做什么.这是我的整个剧本.

I have a raycast2d setup - and I want the raycast to read collision with my objects on the screen. They are to only react if its an object tagged as "Cat" - essentially once that happens, the cat will swipe out and try to attack. However, it tells me the tagg itself is a reference that instantiated. But the object itself exists by default so im not sure what to do here. Here is my whole script.

 void Update() {
    //if ((Input.GetKeyDown(KeyCode.O) && !attacking && attackTimer <= 0)) {
    if (Input.GetMouseButtonDown(0) && !attacking && attackTimer <= 0) {  //every frame check to see if the mouse has been clicked.
                                                                          //Get the mouse position on the screen and send a raycast into the game world from that position.
        Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);//Vector 2 means only 2 points of axis are read. worldpoint means check the point of the world. Camera is used to determien what we are looking at, and we fill it with our mouse location.
        RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero);
        if (hit.collider == null) {//THIS IS THE LINE that says it is having issues. If i REMOVE this line, ANYWHERE i click on the screen activates the cat and thats not what I want to happen.

            Debug.Log("No Object Touched");

            if (hit.collider.CompareTag("Cat")) {
                GameManager.Instance.AudioSource.PlayOneShot(SoundManager.Instance.Swipe);

                attacking = true;
                attackTimer = attackCd;

                attackTrigger.enabled = true;
            }

更新的代码以匹配所需的更改: 我现在得到的错误是NullreferenceException-对于:

UPDATED CODE TO MATCH REQUESTED CHANGES: The error im now getting is the NullreferenceException - for:

 if (hit.collider.CompareTag("Cat")) {

使用程序员建议测试的方法重新测试后,这与我之前遇到的错误相同.

This was the same error i was getting before, after retesting with the methods programmer recommended to test.

控制台将向我显示我还没有单击任何对象,然后向我显示null.因此,我想它试图告诉我它没有找到场景中存在的任何标记为猫的东西?即使Cat是添加的自定义标签,它也已通过Box对撞机应用于猫的游戏对象.是否需要任何材料或任何材料来阅读它的存在?单击其特定位置后,还有其他方法可以调用该对象吗?

The console WILL show me, that I have not clicked a object, then show me the null. So I guess its trying to tell me that it doesnt find anythign tagged as cat that exist in the scene? Even though Cat is a custom tag added, and it was applied to the game object that is the cat - with a Box collider. Does it need a material or anything to read it exists? Is there another way I can call this object on click of its specific location?

更新:

       Debug.Log("No Object Touched");
                if (hit.collider) {
                    if (hit.collider.tag == "cat1") { - 

这摆脱了空引用,但它根本不读猫.如果我点击那只猫,什么也不会发生.是的,现在在编辑器中将其正确标记为"cat1". Meanign标签-新的自定义标签,创建了cat1标签.转到游戏对象,将标签更改为cat1.还确保了大肠菌已被触发.

this got rid of the null reference, BUT It doesnt read the cat at all. if i click the cat nothing happens. and yes it is now tagged as "cat1" properly in editor. meanign tags - new custom tag, created cat1 tag. went to game object, changed tag to cat1. Also ensured a colider is on, with is trigger.

推荐答案

经过数小时的搜索,我终于弄清楚了获取null的原因.

After hours of searching, I finally figured out the reason for getting null.

您需要将新组件">物理">盒对撞机"附加到您的Sprite游戏对象上.

You need to attach 'New Component' > 'Physics' > 'Box Collider' to your sprite game object.

现在,您可以在添加到游戏对象的脚本中使用以下代码片段来了解是否单击了该对象:

Now you can use the below code snippet in the script added to your game-object to know if the object is clicked:

private Ray ray;
private RaycastHit hit;

private void Update()
{
    if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) 
    {
        ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);

        if (Physics.Raycast(ray, out hit, Mathf.Infinity))
        {
            if (hit.transform.gameObject.name == "YourGameObject")
            {
                // Do your stuff on click
            }
        }
    }
}

这就是3d对象可以轻松检测到触摸的原因,因为在创建3d对象时已经附加了碰撞器.

This is the reason why 3d objects can easily detect touches, because they already have colliders attached to them when you create them.

希望这个答案对某人有帮助.

Hope this answer helps someone.

这篇关于Physics2D.Raycast返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆