- Home /
Where does a Raycast come from?
To start off, I know the least about Raycasts than any other function in unity. From what i have tried to pick up, they relay information of some type (i could be totally wrong)
I am following a tutorial for a shooting script, it involves a Raycast function that i think is not doing what it should, because I placed Debugs to single out the problem.
The Raycast function is on line 51. When i "shoot" the tag it gives me is of the terrain, and not of the player that is anywhere around me. This makes me think that forward is probably not the direction in front of me, and more of a global type command (i could be wrong again)
Is my code written correctly, to "shoot" a player right in front of me? Here it is.
#pragma strict
var ableshoot = false;
var shooting = false;
var hit : RaycastHit;
var aeksound : AudioClip;
var walthersound : AudioClip;
var weaponnum : int;
var pbullets : int = 320;
var pbullets1 : int = 9;
var rbullets : int = 520;
var rbullets1 : int = 30;
var a = 1;
var nView : NetworkView;
function Start () {
nView = GetComponent(NetworkView);
if(nView.isMine)
Shoot();
}
function Update () {
weaponnum = gameObject.GetComponent(ChangeGun).weaponnum;
if(nView.isMine){
if(Input.GetMouseButtonDown(1))
Prepare();
if(Input.GetMouseButtonUp(1))
ableshoot = false;
if(Input.GetMouseButtonDown(0) && ableshoot == true)
shooting = true;
if(Input.GetMouseButtonUp(0))
shooting = false;
if(ableshoot == false){
shooting = false;
}
}
}
function Prepare(){
//yield WaitForSeconds();
ableshoot = true;
}
function Shoot(){
while(true){
if(shooting == true && weaponnum == 0 && rbullets1 > 0)
{
rbullets1--;
Physics.Raycast(transform.position,transform.forward,hit);
Debug.Log(hit.collider.tag);
//if(hit.transform !=null) // No need, all GO have Transform
//{
Debug.Log("HitPlayer");
Debug.Log(hit.collider.tag); // What is the tag it hits?
if(hit.collider.tag == "Player" || hit.collider.tag == "Vehicle")
{
Debug.Log("tagrecieved");
Network.Destroy(hit.transform.gameObject);
Debug.Log("Destroyed");
}
//}
GetComponent(AudioSource).PlayOneShot(aeksound, 1.0);
}
else if(weaponnum == 0 && rbullets1 == 0){
yield WaitForSeconds(2);
if(rbullets >= 30){
rbullets -= 30;
rbullets1 += 30;
}
else if(rbullets < 30){
rbullets1 += rbullets;
rbullets = 0;
}
}
if(shooting == true && weaponnum == 1 && pbullets1 > 0){
pbullets1--;
GetComponent(AudioSource).PlayOneShot(walthersound, 1.0);
Physics.Raycast(transform.position,transform.forward,hit);
Debug.Log(hit.collider.tag);
if(hit.transform !=null){
Debug.Log("HitPlayer");
if(hit.collider.tag == "Human" || hit.collider.tag == "Vehicle"){
Debug.Log("TagRecieved");
Network.Destroy(hit.transform.gameObject);
Debug.Log("Destroyed");
}
}
}
else if(weaponnum == 1 && pbullets1 == 0){
yield WaitForSeconds(2);
if(pbullets >= 30){
pbullets -= 30;
pbullets1 += 30;
}
else if(pbullets < 30){
pbullets1 += pbullets;
pbullets = 0;
}
}
if(weaponnum == 0)
yield WaitForSeconds(.075);
else if (weaponnum ==1)
yield WaitForSeconds(.5);
}
}
"Forward" casts a ray in the z direction, right? Are you shure that this is the direction facing forward in the scene-view? $$anonymous$$aybe you have set some weird scale or rotation for the object this script is a component of and the z-axis is facing down?
when a mummy raycast and a daddy raycast love each other very much ....
First : what tutorial are you following? That shoot function with the while loop looks very scary. Having rbullets1 AND rbullets is VERY confusing.
When i "shoot" the tag it gives me is of the terrain : This is probably because the origin if the raycast is at the base of the object, so raycasting from the feet always hits the terrain. This is probably causing the confusion in the next part :
So let's look at this portion :
Physics.Raycast(transform.position,transform.forward,hit);
Debug.Log(hit.collider.tag);
//if(hit.transform !=null) // No need, all GO have Transform
//{
Debug.Log("HitPlayer");
Debug.Log(hit.collider.tag); // What is the tag it hits?
if(hit.collider.tag == "Player" || hit.collider.tag == "Vehicle")
{
Debug.Log("tagrecieved");
Network.Destroy(hit.transform.gameObject);
Debug.Log("Destroyed");
}
//}
How you commented out :
//if(hit.transform !=null) // No need, all GO have Transform
while all gos have a transform, this is to check if you actually hit anything! Again, if your always hitting terrain, then the ray origin is probably at the feet.
Finally, your raycast has no distance parameter, so is only checking a distance of 1 unit. If the origin was not at the feet, I doubt you would hit anything, hence the need to check if(hit.transform !=null)
Debug statements are good, but you can take it one step further to visualize the raycast with a Debug.DrawLine, untested example :
// distance for ray to be cast
var rayDistance : float = 10;
// do raycast and populate hit information
Physics.Raycast( transform.position, transform.forward, hit, rayDistance );
// check if an object has been hit
if ( hit.transform != null )
{
Debug.Log( "Hit " + hit.collider.tag ); // What is the tag it hits?
// draw debug line to show raycast
Debug.DrawLine( transform.position, hit.point );
if ( hit.collider.tag == "Player" || hit.collider.tag == "Vehicle" )
{
Debug.Log("tagrecieved");
Network.Destroy(hit.transform.gameObject);
Debug.Log("Destroyed");
}
}
// ray did not hit anything, show ray
else
{
Debug.DrawRay( transform.position, transform.forward * rayDistance );
}
@alucardj loved the first sentence, man. But why is such a detailed response posted as a comment, I think this should be an answer.
Hold on I realise this is duplicate http://answers.unity3d.com/questions/984982/player-tag-not-being-detected-by-unity.html.
The removal of the transform check was from me since I would rather use the return value of the Raycast to know if it hits and then the transform check is pointless.
"Finally, your raycast has no distance parameter, so is only checking a distance of 1 unit. "
The default is infinity. So no value is like shooting real far. But the main issue here is that he seems to be shooting from within the player while trying to get the tag of the player which won't happen since Raycast ignore the collider if it starts from inside of it.
@Harshad$$anonymous$$ thanks, I didn't think the comment answered the question, more a guide on debugging raycasts.
@fafase thanks for pointing that out, not sure where my brain went on that. Regarding the player, you are correct with the the player not being hit by a raycast originating within the player collider; however this looks like it is meant for network, so the op is trying to hit the instance of another player on the network, not themselves.
@Ian $$anonymous$$cCleary not sure why you deleted your original post, it would've been easier just building from the advice you had received there than posting a new question.
Just some followup on debug lines, you may want to add colour and duration to the debug lines so they are more visible, eg :
Debug.DrawLine( transform.position, hit.point, Color.red, 0.25f ); // renders the line for 0.25 seconds
Debug.DrawRay( transform.position, transform.forward * rayDistance, Color.yellow, 0.25f ); // renders the line for 0.25 seconds
you can also toggle the gizmos tab in the game view window to see the lines in the game view window :
Scripting References :
http://docs.unity3d.com/ScriptReference/Debug.DrawLine.html
http://docs.unity3d.com/ScriptReference/Debug.DrawRay.html
http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
Answer by fafase · Jun 12, 2015 at 08:38 AM
Raycast considers the normal of the hit object, so if the Dot product indicates the normal and the cast are aligned, it means the raycast is inside the object and hence ignores it:
"Raycasts will not detect colliders for which the raycast origin is inside the collider. "
That is from the doc. So your player is ignored as the raycast starts from its position inside of it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613200911im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Raycasts don't go the right direction unless i'm really far away (javascript) 1 Answer
Use yield WaitForSeconds to delay mouseover on a gameObject? (JS) 1 Answer
How can I make a JavaScript function public? 1 Answer
GetComponent() problems 2 Answers
Unityscript: Accessing functions from other scripts 1 Answer