Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ian-McCleary · Jun 12, 2015 at 08:28 AM · javascriptraycastfunctiondirection

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);
     }
 }

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image The_Toller · Jun 12, 2015 at 08:42 AM 0
Share

"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?

avatar image AlucardJay · Jun 12, 2015 at 08:58 AM 2
Share

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 );
 }

avatar image HarshadK · Jun 12, 2015 at 09:18 AM 0
Share

@alucardj loved the first sentence, man. But why is such a detailed response posted as a comment, I think this should be an answer.

avatar image fafase · Jun 12, 2015 at 10:18 AM 0
Share

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.

avatar image AlucardJay · Jun 12, 2015 at 12:16 PM 0
Share

@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 :

alt text

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

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges