I am using RaycastHit and I'm using a 2d sprite to send out a ray upon left mouse click.
So when the player shoots, he shoots out a ray, then if i detect him hitting anything then I do something. I am using the debugging feature for the raycast and when I hit the left mouse I see a ray that shoots out. If I hold down left mouse I see the ray stay constant.
My question is when I shoot out the ray, it doesn't seem to be as accurate, I'm shooting a box, and if i stand right by it shooting out the ray it doesn't make contact. But if I hold down the left mouse and see the ray and use the ray to stab the box it works. Or various other times it will work if i just shoot the box.
Does the ray have to hit at a certain point on the ray to work? What if the ray shoots and the length of the ray is longer than the box? Is that a problem?
-- Edit --
I am using Debug.DrawRay to show me the raycast from the character. It shoots in the direction that I want. I can verify this because when facing the box from the right side, the ray shoots left. and eventually staying on this side and clicking the mouse left and either holding it down and moving towards the box or shooting it from different areas from this side of the box will "eventually" cause the box to disappear, BUT it will not disappear with exactly three mouse clicks from the right side.
You should include relevant code and information on your setup. A raycast works like this:
var hit : RaycastHit;
if(Physics.Raycast(transform.position, transform.forward, hit) &&
hit.transform.tag == "Box") {
var script : HealthScript = hit.transform.GetComponent(HealthScript);
if(script) script.health--;
}
//Project a ray from the position of the transform in the forward direction.
//The ray will not hit backfaces.
//Physics.Raycast will return true after the first thing it hits.
//If you want to only check against certain objects, you should use
//different layers and use a layermask.
The most likely causes for your raycast not doing what you expect are:
You are shooting from the wrong position.
You are shooting in the wrong direction.
You are hitting something other than you want first.
You are shooting at a backface.
You are shooting too short a ray to hit your target.
You are shooting at a collider that is differently shaped or sized than you expect.
You are not shooting your ray when you think you are
In order for your raycast to hit, it must pass through one of the outward facing faces of the collider that it is supposed to hit. To debug, try adding Debug.DrawRay with the same parameters as your ray to your code and see if the ray is actually doing what you think it is. With Debug information, your code might look something like:
var distance : float = 10.0f;
var hit : RaycastHit;
function Update() {
//movement+other stuff
//Will only happen the first frame the mouse button is released
if(Input.GetMouseButtonUp(0)) {
var debugColor : Color = Color.green;
//Raycast and do stuff with the health
if(Physics.Raycast(transform.position, transform.forward, hit, distance)
&& hit.transform.tag == "Box") {
var script : HealthScript = hit.transform.GetComponent(HealthScript);
if(script) {
script.health--;
Debug.Log(script.health);
}
debugColor = Color.red;
}
//Draw what we did: green missed and red hit.
Debug.DrawRay(transform.position, transform.forward * distance,
debugColor);
}
}
But if I hold down the left mouse and
see the ray and use the ray to stab
the box it works.
Well seems to be option 1 from skovacs answer is correct. If you "stab" the box that means you went to change your position. Rays are unlike lasers. They can stop in midair without cause. How did you specify the ray stop condition? I can use rays to measure at exactly 50meters distance (Sort of a proximity mine thing trigger).