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 AFDozerman · Sep 07, 2013 at 08:10 AM · raycastdebug.log

Raycast not reporting what it hit

The issue, I believe, rests on line 46 of my code. I raycast out, and in this version of the code:

 using System.Collections;
 
 
 public class FPscript : MonoBehaviour {
     
     //public GameObject untitled;
     float bulletImpulse = 100f;
     bool test = false;
     RaycastHit hitt;
     
     void Start (){
         
     }
     
     void OnControllerColliderHit(ControllerColliderHit hit){
         
         if (hit.gameObject.tag == "Key") {
             
             test = true;
             
         }
     }
     
     void Update (){
         
         if (Input.GetKey(KeyCode.Escape)){
         
             Application.Quit ();
             
         }
         
         //if(test == true){
                 //if( Input.GetButtonDown("Fire1") ) {
                     //Camera camm = Camera.main;
                     //GameObject thebullet = (GameObject)Instantiate(untitled, camm.transform.position + camm.transform.forward, camm.transform.rotation);
             
                         //thebullet.rigidbody.AddForce( camm.transform.forward * bulletImpulse, ForceMode.Impulse);
                 
             
         Vector3 fwd = transform.TransformDirection(Vector3.forward);
         
         if( Input.GetButtonDown("Fire1") ) {        
         if (Physics.Raycast(transform.position, fwd, 10)){
                     
      // if(hitt.collider.gameObject.tag == "Enemy"){
                         
     Debug.Log("Object Hit");
                     //    }
     
         }
         
         
         }    
     
     }
 }

I get a Debug.Log report, but when I take out the comments on the line that reads // if(hitt.collider.gameObject.tag == "Enemy"){, nothing happens no matter what. I'm a little stumped, as I have triple checked and this seems to be the correct way to test what you hit. Any suggestions from anyone?

Comment
Add comment · Show 5
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 Benproductions1 · Sep 07, 2013 at 12:33 PM 0
Share

there is nothing on line 46. It's a blank line -_-

On that note. How do you know it's hitting something. The raycast function always returns a bool, so that is NOT the problem.

avatar image AFDozerman · Sep 07, 2013 at 06:57 PM 0
Share

It was 46 in $$anonymous$$onoDevelop. It's 45 in the webpage. Either way, I have several problems that I have to iorn out. It's gonna take awhile. Thanks for the input, though.

avatar image AFDozerman · Sep 07, 2013 at 07:02 PM 0
Share

Also, some other interesting things are happening. When I comment out line 45 in the code above, I get the output "Object Hit" when I fire at trees or houses, but when I fire at the enemies on the game, I still get nothing, even though it is still a game object. I've double checked my tags and layers, and everything is on the same layer and enemies are tagged "Enemy". Very confusing turn of events for this noob.

avatar image clunk47 · Sep 07, 2013 at 07:38 PM 2
Share

C# EXA$$anonymous$$PLE:

 RaycastHit hit;
 
 void Update()
 {
     if(Physics.Raycast(transform.position, transform.forward, out hit, $$anonymous$$athf.Infinity))
     {
         print(hit.collider.gameObject.name);
     }
 }
avatar image RyanZimmerman87 · Sep 07, 2013 at 11:34 PM 0
Share

Are you sure your ray is hitting the enemies? 10 meters is not very far for a Raycast you should really crank up the range to make sure you can shoot far enough to hit the enemies.

Also you might need to double check your collider set up on the enemies. $$anonymous$$ake sure the biggest or preferably only collider object on them (for troubleshooting purposes) is the one with the tag "Enemy". If you have child objects on the enemy it's possible you are hitting a child object without this tag?

You might also want to try using .name on the enemies ins$$anonymous$$d of .tag just to see if you specify the name of the collider object on enemy if it would work.

I think you need to provide some more info though for help if it still doesn't work.

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by RyanZimmerman87 · Sep 07, 2013 at 08:22 AM

You forgot to include hitt in your raycast.

You need:

if (Physics.Raycast(transform.position, fwd, out hitt, 10))

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

18 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

Related Questions

How to raycast hit empty space? 1 Answer

C# Raycast is only hitting one item and then stopping 1 Answer

Debug.log not working with raycast 1 Answer

How to register if my Raycast hits nothing(Solved) 2 Answers

Logic Required 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