- Home /
Touch drag objects, raycast cant detect object?
Hi i've been working on a little game, which features a deck of cards and I want to be able to move the top card when I raycast from touch to it. I have tried implementing quite a few different things and have been searching online for hours trying to find out why it doesnt work. Currently I am using the following script for the raycast
Vector3 v3;
if(Input.GetMouseButtonDown(0))
{
RaycastHit hit;
//Debug.DrawRay(new Vector3(Input.mousePosition.x,Input.mousePosition.y,Input.mousePosition.z),Vector3.forward);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.Log("got past first if");
if(Physics.Raycast(ray,out hit,1000))
{
Debug.Log("past second if");
if(hit.collider.tag == "Ace")
{
toDrag = hit.transform;
dist = hit.transform.position.z - Camera.main.transform.position.z;
//v3 = Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
v3.x = Input.mousePosition.x;
v3.y = Input.mousePosition.y;
v3.z = dist;
v3 = Camera.main.ScreenToWorldPoint(v3);
offset = toDrag.position - v3;
dragging = true;
Debug.Log("sth is hit");
}
}
}
if (Input.GetMouseButton(0))
{
if (dragging)
{
v3.x = Input.mousePosition.x;
v3.y = Input.mousePosition.y;
v3.z = dist;
v3 = Camera.main.ScreenToWorldPoint(v3);
toDrag.position = v3 + offset;
}
}
if (Input.GetMouseButtonUp(0))
{
dragging = false;
}
The issue is that I dont even get past the second if, let alone the third. This implies the raycast is not even being cast, what could be the reason? there are no objects in front of the one i am raycasting to.
Answer by zach-r-d · Jun 10, 2015 at 07:30 PM
If Physics.Raycast is called, then the raycast is definitely happening; if it returns false (causing the second if not to be entered) that means it didn't hit anything. Here are a few things to try, let us know how it goes:
Remove the distance limit for the raycast
Make sure there is a collider on the card deck
Make sure the card deck, and none of its parents, have their layer set to Ignore Raycast
I reckon it's because there is no collider on the card deck, I will check to see if that works and let you know, thank you.
Your answer
Follow this Question
Related Questions
Help With Touch to Drag Script 1 Answer
Move an object to Input.Touch location 0 Answers
Unity UI: neglect transparent area of button and trigger user input underneath it. 2 Answers
Detect if dragged over object 1 Answer
Getting raycast to fire at touch position but only when second finger is on screen 0 Answers