Touching a 2D gameObject does not work!

Hello again!

My script for touching a 2D gameObject does not work! here it is

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class VLCMODE : MonoBehaviour
{
   
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit2D hit;
            if (Physics2D.Raycast2D(ray, out hit))
            {
                //Select Stage
                if (hit.transform.name == "vlc")
                {
                    SceneManager.LoadScene("VLCMODE");
                }
            }
        }
    }
}

Please tell me what’s wrong with it! Thanks!

Is there a 2D collider attached to the object?

Yes. The error says :

You need to do Physics2D.Raycast, not Raycast2D. :slight_smile:

Ooooh ok thank you! But there is 2 more errors…

2D raycasts work a bit different from 3D ones. Try this:

var cam = Camera.main;

RaycastHit2D hit = Physics2D.Raycast(new Vector2(cam.ScreenToWorldPoint(Input.mousePosition).x, cam.ScreenToWorldPoint(Input.mousePosition).y), Vector2.Zero, 0f);

if (hit.collider.name == "NAME") {

}

ok but where do i put it in the script?

Replace your old code inside of

if (Input.GetMouseButtonDown(0))
{
     // put that code here
}

And don’t forget to replace “NAME” with what you need.

ooooooh ok i’ll try it now

The errors are gone but still another error…6157358--673028--upload_2020-8-1_10-55-46.png

In 2D you don’t use a raycast (especially one of zero length with a zero degenerate direction) to detect if a point overlaps a collider, you use Physics2D.OverlapPoint.

When you have errors on something then please take a look at it in the Scripting Reference. Note that everything is case-sensitive.

For instance, Vector2.zero.

2 Likes

@MelvMay Oh… I’ll try

Oh, my bad, I don’t work with 2D that much. :smile:

My script looks like this for the moment

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class VLCMODE : MonoBehaviour
{
   
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {   
            var cam = Camera.main;
            RaycastHit2D hit = Physics2D.OverlapPoint(new Vector2(cam.ScreenToWorldPoint(Input.mousePosition).x, cam.ScreenToWorldPoint(Input.mousePosition).y), Vector2.zero, 0f);
            if (hit.collider.name == "vlc")
            {
            SceneManager.LoadScene("VLCMODE");
            }
        }
    }
}

But there is still an error…


Yes i know… It’s an errorception…

Like Melv said, check the scripting reference.

if (Input.GetMouseButtonDown(0))
{
   Vector2 mPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
   Collider2D hit = Physics2D.OverlapPoint(mPos);

   if (hit != null && hit.transform.name == "vlc")
   {
      SceneManager.LoadScene("VLCMODE");
   }
}
2 Likes

YES! IT WORKS! THANK YOU GUYS!

1 Like