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.