- Home /
Raycast hit in unity 3d
i have a character which is moving in z direction with constant velocity.
lots of obstacle objects are occupied infront of character.
while user touches on obstacle objects infront of the character should get shooted.
camera is moving with character .
camera is child of character.
i used raycast to detect objects.
My problem is that sometimes it get destroys , and sometimes not.
Raycast not hit is my exact problem.
if(Input.GetMouseButtonDown(0)){
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (ray, out hit3, 1000.0f)){
Destroy(hit3.collider);
}
}
camera is not in (0, 0, 0) angle.
help with good solution.
Is it better to give raycast to character or camera?
Thanks in advance.
Answer by Benproductions1 · Feb 18, 2013 at 04:25 AM
Hi!
Firstly, writing out
in the parameter of the Raycast Function iis just plain wrong, get rid of that! Secondly you are deleting the collider, which is a component of a gameObject. I'm surprised it even destroyed anything. Instead you should delete the gameObject of the collider or transform.
So: Destroy(hit3.transform.gameObject);
is the way
Hope this fixes it, Benproductions1
am sorry. my raycast not reach the object properly. i dont want destroy game object. just want to change texture. main issue is raycast. this issue blessed with sleepless nights