Raycast Registering even when far from Mouse

Sorry for the million raycast posts, I’m trying to make a basic drag & drop minigame, but for some reason the raycast is registering on the object, even when you click far from the boxcollider. Here’s a video showing it,

As you can see, the boxcollider only covers the word, but for some reason, sometimes it just jumps to the mouse even when you click far from it. I’m just trying to make it drag & drop so you can drag them into the spots ,but this might be kinda frustrating if you don’t even click close to the words. This is my raycast script,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class WordDropGame : MonoBehaviour
{
    public Camera cam;
    public bool clicky, isDragging;
    private GameObject objectHit;
    public GameObject gM;
    private Vector3 mousePos;
    public float moveSpeed;
    private void Start()
    {
        cam = Camera.main;
    }
    private void Update()
    {
        if (Input.GetMouseButton(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit2D info = Physics2D.Raycast(cam.transform.position, ray.origin);
            if (info.collider)
            {
               
                var objectHit = info.transform.gameObject;
                if (objectHit.CompareTag("WordDrop") && !gM.GetComponent<WordDropGame>().isDragging && gM != null)
                {
                    gM.GetComponent<WordDropGame>().isDragging = true;
                    objectHit.GetComponent<WordDropGame>().clicky = true;
                    print(objectHit.name);
                  
                }
            }
        }
        if (Input.GetMouseButtonUp(0))
        {
            clicky = false;
            gM.GetComponent<WordDropGame>().isDragging = false;
        }
        if (clicky)
        {
            mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            this.transform.position = Vector2.Lerp(transform.position, mousePos, moveSpeed);
        }
    }
}

Is there any way I can get this to more accurately drag and drop? It drags and drops just fine, but it has just a little too much range? I just tested again and it was even worse than the video, a word that was halfway accross the screen got snapped to the mouse…

And, the “isDragging” bool is so you can’t drag more than one at once.

Thanks in advance.

Your code doesn’t make much sense. First of all the Ray struct represents a 3d ray in worldspace. ScreenPointToRay creates a ray that starts at the camera and goes into the 3d scene. A 2d Raycast has nothing to do with the Ray struct. Physics2D.Raycast takes a 2d position as start and a 2d direction vector. you pass the cameras position as the origin of the ray and then you use the 3d ray origin as direction which makes no sense at all.

In your case using a 2d Raycast doesn’t make any sense since you seem you just want to detect if the mouse is over a 2d element. For this you want to use Physics2D.OverlapPoint or a similar method.

A 2d raycast is a line in 2d space, it does not go into the screen but just lies in the screen. So a raycast in 2d is about hitting a 2d object on on of its sides.

2 Likes

Sorry for the late response, just for anyone that might see this, I fixed it by removing the raycast altogether, and using the built-in “OnMouseDown()” method. Thanks for the information, Bunny83.

For reference, here’s my script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class WheelSpin : MonoBehaviour
{
    public Camera cam;
    public bool clicky;
    public float slowForce, fullForce;
    private GameObject objectHit;

    private void Start()
    {
        cam = Camera.main;
    }
    private void OnMouseDown()
    {
                Spin(slowForce, objectHit);
    }
   
    private void OnMouseUp()
        {
            Spin(fullForce,objectHit);
        }

    public void Spin(float spin,GameObject spinny)
    {
         GetComponent<Rigidbody2D>().AddTorque(spin);
   
    }

}

Didn’t think it was so easy haha, it doesn’t follow the mouse, but it kinda FEELS like it follows the mouse(which is why I gave it “slowForce” in the OnMouseDown())