- Home /
raycast doesn't collide on anything? (C#)
I've been reading around to try solving this problem, basicly what I'm trying to do is to raycast from maincamera to an object with a certain tag, and return an info (for now I just use print for debugging), but it doesn't get back anything, the script compile fine without errors of any sort, so I don't know where to look. Here the code:
using UnityEngine;
public class raycast_item : MonoBehaviour {
public float RayLength = 1.0f;
void Update() {
RaycastHit hit;
if(Physics.Raycast(transform.position, Vector3.forward, out hit, RayLength))
{
if (hit.collider.gameObject.tag == "Item")
{
print ("Item");
}
else
{
print ("Nothing");
}
}
}
}
The raylength seems pretty short, you may want to try $$anonymous$$ath.Infinity and see if that helps. Also, did you want gameObject.Transform.forward ins$$anonymous$$d of Vector3.forward? So that you're looking down the forward of the game object, ins$$anonymous$$d of Vector3( 0.0f, 0.0f, 1.0f ) down world space?
I'm an idiot, yeah I meant to use transform ins$$anonymous$$d of Vector3, but still making the ray length longer it doesn't work always, is kind of glitchy, but it works now so it only need a refinement. Thanks guys.
Answer by torrente · May 24, 2012 at 06:52 PM
fromCamToTarget = targetTransform.transform.position - camTransform.transform.position;
if (Input.GetButton("Fire1"))
{
if (Physics.Raycast(camTransform.transform.position,
fromCamToTarget,
out bulletHit,bulletDistance))
{
if (bulletHit.collider.gameObject.tag == "target")
{
Debug.DrawRay(camTransform.transform.position,
(targetTransform.transform.position - camTransform.transform.position),
Color.green);
Debug.Log(bulletHit.collider.gameObject.tag);
}
else
{
Debug.Log(bulletHit.collider.gameObject);
Debug.DrawRay(camTransform.transform.position,
fromCamToTarget,
Color.red);
}
}
}
If you want to raycast from one point to another, then it's better/easier to use Linecast. (And you can debug that with DrawLine rather than DrawRay.) Raycast is for when you have a point and want to see what's in a particular direction from there. Which I think is what the question is asking, but I'm not 100% sure.
Answer by Eric5h5 · May 24, 2012 at 06:32 PM
Use Debug.DrawRay to visualize what you're doing. In this case it's likely you meant transform.forward and not Vector3.forward.
This the way to go. I've had some crazy raycasts in the past (my mistakes) and being able to visualize them in the editor usually solved the problem. I'm going to add an answer with a quick bit of code as well.
Your answer
![](https://koobas.hobune.stream/wayback/20220613071909im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to use a Raycast to see the distance traveled within a collider. 2 Answers
Move player to specific location and avoid obstacle. 2 Answers
Im trying to create a word game with a twist 0 Answers
Raycast not detecting ANY HITS AT ALL when starting inside a collider. 0 Answers
Raycast Destroy(hit.collider.gameObject); (Still need help) 1 Answer