- Home /
Raycast sometimes ignore objects.
Hello, I have a problem with Raycast, when sometimes ignore object which they are between two points. I using this script:
if (Physics.Raycast (transform.position, Vector3(x2,0.5,z2),hit,Mathf.Infinity)){
//Do something
}
Sometimes it works but if I rotate a object between those points, it stop working and ignore it. Sometimes raycast detect collision when is closer to object. For example and better imagination there is two pictures:
At first one is collision detected right, because there are colored lines. Object in the middle is object on our way. The smaller one at right is moving to the point at left.
On the second is object on a way but raycast not detected collision.
So I need a help whats wrong is with raycast. Maybe I using it a bad way. Thank you for help. Sorry for my English.
Answer by Sisso · Sep 04, 2013 at 12:28 PM
Take a look better look in documentation and double check your code and variables. Especially, what is each param.
http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
static function Raycast(origin: Vector3, direction: Vector3, hitInfo: RaycastHit): bool;
The raycast function expected a direction and you are giving a point.
Change the
Vector3(x2,0.5,z2)
to
(Vector3(x2,0.5,z2) - transform.position).normalized
Well if you pointing at something just tell me because I dont see anything bad on variables. Now it looks like raycast accept only one side of cube and the other just ignore. I dont know, but thank you for your answer.
Your answer
![](https://koobas.hobune.stream/wayback/20220613113546im_/https://answers.unity.com/themes/thub/images/avi.jpg)