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 Bonnyy · Nov 12, 2014 at 01:39 PM · raycastenemydebug.log

Debug.log not working with raycast

Hi guys, its my first time here! I want an enemy that uses raycasts to detect me, the player. Im setting up the raycast and a test debug log to see if it works, but it does not. On my enemy I put a sphere and put a collider with is trigger on it. The problem is that even tho it enters the trigger, it does not show the debug message.

 using UnityEngine; 
 using System.Collections;
 
 public class EnemyAI : MonoBehaviour { 
 
     private GameObject playerTarget;
     private SphereCollider col;
     
     void Awake ()
     {
         playerTarget = GameObject.FindGameObjectWithTag ("Player");
         col = GetComponent<SphereCollider>();
     }
     
     void OnTriggerEnter (Collider other)
     {
         if(other.gameObject == playerTarget)
         {
             Vector3 direction = playerTarget.transform.position - transform.position;
             {
                 RaycastHit hit;
                 
                 if(Physics.Raycast(transform.position, direction.normalized, out hit, col.radius))
                 {
                     if(hit.collider.gameObject == playerTarget)
                     {
                         Debug.Log ("player in sight");
                         //Stuff happens that makes the enemy chase the player
                     }
                 }
             }
         }
     }
 }

I hope somebody can help me with this one because I'm completely stuck now...

Comment
Add comment · Show 4
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 Linus · Nov 12, 2014 at 01:40 PM 0
Share

I doubt that is debug.log that does not work with raycast. But insert a debug log before line 25. But you qould be hitting something unexpected. Perhaps the collider isa child of the PlayerTarget

  Debug.Log ("Hitted"+hit.transform.name);
avatar image Bonnyy · Nov 12, 2014 at 01:53 PM 0
Share

I placed the Debug.Log ("player in sight"); on multiple places in the code, but I'm not getting a response, even in the awake. I am able to see the green orb around my enemy cube, the collision orb. I enter the orb with the player... and nothing happens. Any ideas?

avatar image Fab42Zl · Mar 24, 2015 at 03:54 PM 0
Share

I have the same problem, when my raycast has detected each game object, Debug.Log return the last one, even if i put a clear line to erase it , it doesn't refresh, but the essential is that raycast run...good luck

avatar image hexagonius · Mar 24, 2015 at 04:12 PM 0
Share

If you don't get a log even if you put it into Awake, the script is probably not attached to the enemy in the scene.

Are you sure that playerTarget is not null?

Another question would be how you move the player. If by translation, then that's the problem.

BTW. you can remove the braces on line 20 and 31, they're not part of any condition.

Last but not least: why are you raycasting what you already triggered, doesn't make sense.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ericool · Mar 24, 2015 at 04:38 PM

well using OnTriggerEnter and RayCast are two differents techniques first off . If we enter in OnTriggerEnter that means that isTrigger is enable , check that first . Second , OnTriggerEnter works once your objects collide until you went out to call OnTriggerExit. Third , RayCast might not working it is calculating a distance , but if your objects are too close , dunno..

 When Update()
 {
     if(activateRayCasting)
       {
            //do your raycast stuff
       }
 
 }
 When OntriggerExit(...)
 {
        activateRayCasting = true;
 } 
 When OntriggerEnter(..)
 {
        activateRayCasting = false;
 
       if(col.gameObject.tag != "terrain")
          Debug.Log(col.gameObject.name);
 }





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

6 People are following this question.

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

Related Questions

Follow Nearest Target 1 Answer

Boolean being overwritten 1 Answer

Raycast not reporting what it hit 1 Answer

can foe hear us? 1 Answer

How to see if a transform is visible without a camera 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