How to find nearby objects?
Hi, I am trying to control the four corners of the mouse with raycasting. At the distance I specify, if there is a tag, write "found! :)" on the console.
if (Physics.Raycast (hit.transform.position, Vector3.left, out hit, 1.0f)
|| Physics.Raycast (hit.transform.position, Vector3.right, out hit, 1.0f)
|| Physics.Raycast (hit.transform.position, Vector3.forward, out hit, 1.0f)
|| Physics.Raycast (hit.transform.position, Vector3.back, out hit, 1.0f)) {
if (hit.collider.transform.tag == "redbox") {
print ("found! :)");
}
}
But it does not work and it always gives the same error.
NullReferenceException: Object reference not set to an instance of an object
I want to check around the mouse.I am trying to do this using raycast.I do not know if there is any other way of doing this, It did not work in the alternative ways I thought.Below is the mouse that appears as a result of right clicking.
Is it possible to find out which tag is in the ending points of the gray colored lines?
Answer by hexagonius · Apr 15, 2017 at 05:00 PM
you're using hit.transform.position as the raycast start where hit is your out parameter for the result and it's transformis of course null because it has not been used and filled at that time. Use the mouse position instead.
Answer by eatsleepindie · Apr 15, 2017 at 05:03 PM
You are trying to raycast out from the position of 'hit', which does not exist yet. You want to raycast out from the transform itself. Physics.Raycast (in the way you are using it) does not take in the hit parameter, only a variable to store the information of the hit if there is one, hence 'out hit'. Try this:
if (Physics.Raycast (transform.position, Vector3.left, out hit, 1.0f)
|| Physics.Raycast (transform.position, Vector3.right, out hit, 1.0f)
|| Physics.Raycast (transform.position, Vector3.forward, out hit, 1.0f)
|| Physics.Raycast (transform.position, Vector3.back, out hit, 1.0f)) {
if (hit.collider.transform.tag == "redbox") {
print ("found! :)");
}
}
Also of note, you should use CompareTag rather than .tag == "redbox".
I have tried it before. There is no error message but I can not find the tag either.
I can't say with any certainty why it's not working, but my answer was the fix for your errors.
Have you tried drawing your rays in the inspector via DrawRay? Use this to make sure your rays are passing through a collider. Note that these will only show up in the Scene window, not the Game window, and you will want to multiply the direction of the drawn ray by the distance. I'd suggest:
Debug.DrawRay(transform.position, Vector3.left * 100f, Color.red);
If it looks as though your rays are sitting on the ground, then change the start point of your rays to something like this:
Physics.Raycast (transform.position + Vector3.up * 0.5f, Vector3.left,
This will move your ray up from the ground to ensure they are passing through colliders, but you may need to adjust the 0.5 based on the size of the objects you are raycasting against.
yes I'm using.All of the codes I use;
private Ray mouseRay;
private RaycastHit hit;
private Vector3 endPoint;
mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetButton ("Fire2")) {
if (Physics.Raycast(mouseRay.origin, mouseRay.direction, out hit, $$anonymous$$athf.Infinity))
{
if (Physics.Raycast (transform.position, Vector3.left, out hit, 1.0f)
|| Physics.Raycast (transform.position, Vector3.right, out hit, 1.0f)
|| Physics.Raycast (transform.position, Vector3.forward, out hit, 1.0f)
|| Physics.Raycast (transform.position, Vector3.back, out hit, 1.0f)) {
if (hit.collider.transform.tag == "redbox") {
print ("found! :)");
}
}
Debug.DrawRay (hit.transform.position, transform.right * r1, Color.gray);
Debug.DrawRay (hit.transform.position, -transform.right * r1, Color.gray);
Debug.DrawRay (hit.transform.position, transform.forward * r1, Color.gray);
Debug.DrawRay (hit.transform.position, -transform.forward * r1, Color.gray);
Debug.DrawRay (mouseRay.origin, mouseRay.direction * 50, Color.green);
endPoint = hit.point;
} else {
endPoint = mouseRay.origin + mouseRay.direction.normalized*distance;
Debug.DrawRay (mouseRay.origin, mouseRay.direction * 50, Color.black);
}
}
It looks like this.
Your answer
Follow this Question
Related Questions
Find Script and call function FROM hit.transform 0 Answers
Operator '==' is ambiguous on operands of type 'Vector2' and 'Vector3' 1 Answer
InverseTransformPoint() help 0 Answers
How To: Calculate Line Of Best Travel For Grid From Line 2 Answers
transform.localScale in Coroutine grows exponentially 0 Answers