- Home /
Raycast isn't working as expected
Hello! I am using a raycast to tell my console what the ray hit. I am using a sign prefab for this, and it seems to only work half the time. When I click towards the outer edges of the sign, it prints that I hit it in the console, but when I get inner more or around the middle, it doesn't print. Why don't you think this is working, and since I am new to raycasts, what exactly does the raycast look for when finding a collider? Please help, thanks.
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Cam : MonoBehaviour {
public float rayLength;
private Animation anim;
void Start () {
anim = GetComponent<Animation>();
}
void Update () {
if (Input.GetKeyDown(KeyCode.K)) {
anim.Play ();
}
if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject()) {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (ray, out hit, rayLength)) {
Debug.Log (hit.collider.name);
}
}
}
}
Answer by sparkzbarca · Jan 05, 2018 at 01:58 AM
As to what a raycast looks for, it looks for an intersection.
It does this basically using trig of some kind or another.
For example for the simplest type of collider a sphere, it's literally a distance check (this is why sphere are great if you need a million colliders in a scene because even a crap pc can do loads of distance checks in a moment)
a sphere with a radius of one has collided with another sphere with a radius of one when the distance between the centers is less than or equal to 1 for example. This is because at all points the edge of the collider of a 1m radius sphere is half that or .5 meters.
If the edge of one sphere is .5 meters and the other is the same, if those two objects ever get so close they are a meter away, they must by definition be touching.
For squares or triangles, you can do again a similar thing just involving trigonometry.
Basically your trying to determine if two lines intersect for most things. very very smart math people have figured out ways to do this fairly easily and that's mostly all that needs be said, if your interested you can google for the relevant math on determine intersect point of two lines.
In this case i would try to do a debug raycast so do this
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems;
public class Cam : MonoBehaviour {
public float rayLength;
private Animation anim;
void Start () {
anim = GetComponent<Animation>();
}
void Update () {
if (Input.GetKeyDown(KeyCode.K)) {
anim.Play ();
}
if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject()) {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction,Color.red, 10f);
if (Physics.Raycast (ray, out hit, rayLength)) {
Debug.Log (hit.collider.name);
}
}
}
}
this line i added at the bottom
Debug.DrawRay(ray.origin, ray.direction,Color.red, 10f);
it will draw a ray that lasts for ten seconds showing you where teh ray is actually being cast so you can see what it hits, if it does seem to hit where intended, then you have a collisision detection problem, if it's not then you have a raycast problem.
So after debugging I found out this, which doesn't help much: When I did the exact code you put above, all I saw was a red line (the ray) co$$anonymous$$g out of the camera in the direction it was ment to go, but it only went out like an inch then stopped. In the inspector, I have my raycast length set to 100, and even set it to 1,000 but it stayed the same length, so how can I make it longer to see where it is actually going? @sparkzbarca
Your answer
Follow this Question
Related Questions
My Raycast Won't Fire 1 Answer
Rect from two points issue 0 Answers
Raycasting ignoring small sized object 1 Answer
Destroying Game Object On MouseButtonDown + Distance Collision 0 Answers