- Home /
How do you change the position of a Raycast?
I have a shooting script on my player that it stupposed to delete the enemy once it hits them, identifying them by tag. I ran debug.draw to find that my Raycast was positioned at the bottom of my guys feet, shooting only the ground. Even when i jumped it wouldnt say it shot the other player.
I asked another question on how to find out where it was shooting (see here for info on problem and shooting script) http://answers.unity3d.com/questions/985101/where-does-a-raycast-come-from.html
I was told that the Raycast wont collide with a collider that it is inside of. Does this mean that when i shoot the other player (Clone of myself) that it wont collide with them, if they are tagged the same? Is there any way for me to move the Raycast in front of me so im not only shooting the terrain?
Thanks in advance for any help!
Answer by zach-r-d · Jun 13, 2015 at 01:15 PM
You can change the starting position of the raycast by adding a Vector3 to transform.position in the first argument of Physics.Raycast. So if your original raycast looked like this:
Physics.Raycast(transform.position, transform.forward);
You could change it to look like this:
Physics.Raycast(transform.position + new Vector3(0f, 1.5f, 0f), transform.forward);
Or better yet, instead of hardcoding the new Vector3, you could just declare a var at the top of the script that could be edited in the inspector. It would then be easy to play with the x, y, and z offsets to find a raycast starting position that fits your needs best.
Also, the raycast will only ignore the collider it starts inside of. It doesn't matter that the enemy is a clone of the player or that it's tagged the same.
After doing this, the origin did not change at all. Its X, Y, Z with a vector right? I changed the Y and Z to put it in front of my object. But it seems to be co$$anonymous$$g from the audio source on the object. Can I move that?
Did you make sure to add the Vector3 in the debug drawing code as well as the actual raycast?
Thank you so much, You are a life saver haha. Thanks a ton though
What if my Physics.Raycast
is taking (Ray,out RaycastHit)
!? How should I offset my z value!? Appreciated!!!