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 Weapioneer · Oct 27, 2014 at 04:50 PM · colliderplayeroverlapsphere

overlapsphere not detecting player

Hello all,

So I started using unity recently, with about 2 years basic experience in java coding. I am currently trying to make a game, but I have a problem about making an explosion damage the player. The player is made using the FPS character controller, with a "player" tag. My explosion code snippet is below,

 void explosion()
     {
 
         Collider [] colli = Physics.OverlapSphere (Vector3.zero, explosionSize);
         foreach(Collider col in colli)
         {
             if(Physics.Raycast (transform.position, (col.transform.position - transform.position), out hit, explosionSize))
             {
                 if(hit.rigidbody == null)
                 { 
                     continue;
                 }
                 else
                     hit.rigidbody.AddExplosionForce(explosionForce, gameObject.transform.position, explosionSize, 1, ForceMode.Impulse);
                 if(hit.transform.CompareTag("Player"))//player detector
                 {
                     Debug.Log("ouch");
                     HealthScriptRef.Health = HealthScriptRef.Health - (100 - (hit.distance * 2)); 
                 }
                 else
                     Debug.Log (hit.transform.tag.ToString());
                 Debug.Log (col.gameObject.tag.ToString ());
             }
 
         }
         Instantiate (explosionFireball, transform.position, Quaternion.identity);
         DestroyObject(gameObject);
     }

So the explosion uses an overlapsphere to detect all colliders in range, and using raycasting to determine LOS, in order to make using cover useful. This works well with test rigidbody targets, but the code inside the IF statement for detecting players doesn't run, and the player is never listed in the following ELSE statement tag list. When the third debug statement lists all the colliders, the player is still not mentioned, which makes me think that it might be because the overlap sphere doesn't include the player in the array. So I gave the player a capsule collider, but no luck, it still doesn't detect the player. I tried giving the player a rigid body, and it completely glitched up the physics engine, causing the player to fly upwards high. So I am now completely stumped as to what to do, so I posted this question, please assist.

Yours sincerely, Weapioneer

Comment
Add comment
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

2 Replies

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

Answer by GameVortex · Oct 28, 2014 at 08:42 AM

On line 11 you are skipping everything and continuing to the next in the list when the target does not have a rigidbody, and as you stated your player does not have a rigidbody.

You should restructure a little and instead check if there is a rigidbody, and add the explosion force if it is. Then check for the player if there is no rigidbody.

Comment
Add comment · Show 3 · 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
avatar image Weapioneer · Oct 28, 2014 at 02:13 PM 0
Share

Ah, thank you both! I just shifted my player detection code into line 11, and it worked perfectly. I have another small query though, how can I modify a variable(health) in another gameobject's script? I read that sendmessage isn't that good in terms of efficiency, and I'd like to keep my code running as fast as possible, so is there a good alternative? Thanks again, Weapioneer

avatar image Deadcow_ · Oct 28, 2014 at 02:18 PM 0
Share

if you have Health public variable in your Player script or public method Damage(int damage) to reduce health you may call it like that

 hit.gameObject.GetComponent<Player>().Damage(damage);
 or 
 hit.gameObject.GetComponent<Player>().Health -= damage;
avatar image Weapioneer · Oct 28, 2014 at 02:29 PM 0
Share

I just tried this, and again, it worked perfectly! Thank you for the speedy and useful reply!

avatar image
0

Answer by Deadcow_ · Oct 28, 2014 at 09:05 AM

Yep, just put

 if (hit.tag != "Player") continue;

instead of

 if(hit.rigidbody == null)
 { 
     continue;
 }

By the way, instead of hit.transform.tag.ToString() you may just use hit.tag since tag is already a string and hit have such variable you don't need to refer to transform

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Detecting when Player stoped to push a rigidbody? 3 Answers

Rigidbody Character Jumps Higher With At Least 2 Collisions 3 Answers

Moving colliding Object causes Player to jitter 0 Answers

Play animation when colliding with trigger! 3 Answers

my character always falls when he is walking because of the collider 0 Answers


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