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 $$anonymous$$ · Mar 31, 2014 at 09:33 PM · raycastenemydetectionspherecast

spherecast wont work

 var enemyNearing: RaycastHit;
 var start : Vector3 = transform.position;
         
 if (Physics.SphereCast(start, view, transform.forward, enemyNearing, 0.000001) && enemyNearing.transform.tag == "EnemyUnit")
 {
 enemyNear = true;
 target = enemyNearing.collider.gameObject.transform.position;
 }
 else
 {
 enemyNear = false;
 }


This is my code that i am using for the spherecast but for some reason enemyNear isnt becoming true. It seems as though the raycast isnt even firing. How can i fix this?

Comment
Add comment · Show 3
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 getyour411 · Mar 31, 2014 at 10:11 PM 0
Share

This looks odd

 var start : Vector3 = transform.position;

why do that only to turn around and use start as 1st parameter; why not transform.position (or a true cached of Transform)

What is view, there's no definition shown here? Why is the sweep distance so tiny?

avatar image $$anonymous$$ · Mar 31, 2014 at 11:14 PM 0
Share

i like to keep things clean and simple and having a variable in the parameters helps do that. View is just a simple float that changes based on the type of unit, in this case it is 50. The sweep distance is tiny because i don't want the ray to move forward i want it to stay relatively right on top of the object casting it.

avatar image getyour411 · Mar 31, 2014 at 11:16 PM 0
Share

Assu$$anonymous$$g your tag is correct, the issue is SphereCast so you just need to evaluate those parameters. Is the origin perhaps inside a collider? And the enemy has true Colliders (not isTrigger checked)?

You should get rid of transform.forward and make that a parameter too eh?

2 Replies

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

Answer by CatchCo · Mar 31, 2014 at 11:28 PM

So the reason this is not working is because SphereCast doesn't work how you think it does. Spherecast will ignore any colliders that it touches at it's starting point. It's meant to detect objects as it moves.

If you want to detect nearby colliders manually you should look into Physics.OverlapSphere. It does not require movement and will detect collisions as you are aiming to do here.

Comment
Add comment · Show 4 · 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 $$anonymous$$ · Apr 01, 2014 at 12:17 AM 0
Share

i tried overlapSphere but i dont get it at all. I spent hours looking and couldnt figure out how i am supposed to define the parameters and only return true the tag that I want.

avatar image CatchCo · Apr 01, 2014 at 12:27 AM 0
Share

Here is how you would do that: (assu$$anonymous$$g you are using javascript)

 function EnemyIsNear() : bool
 {
   var hitColliders = Physics.OverlapSphere(transform.position, view);
    
   // search through all hit colliders for an enemy
   for (var i = 0; i < hitColliders.Length; i++)
   {
     if (hitCollider.tag == "EnemyUnit")
     {
       return true; // we found an enemy. exit early
     }
   }
 
   return false; // looked through all hit colliders but none was an enemy
 } 
avatar image $$anonymous$$ · Apr 01, 2014 at 01:26 AM 0
Share

thx i will try that

avatar image $$anonymous$$ · Apr 01, 2014 at 08:43 PM 0
Share

it says if (hitCollider.tag == "EnemyUnit") { return true; // we found an enemy. exit early } doesnt work because it doesnt know what hitCollider is. If this isnt a raycast how do i define the hit?

avatar image
0

Answer by marwanable · May 18, 2020 at 09:42 AM

if (hit[i].tag == "something") { // do stuff }

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

21 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

Related Questions

How to make enemy check if it doesn`t see player for certain times it changes state 1 Answer

enemy raycast to detect player 1 Answer

2d raycast enemy detection problem. 0 Answers

Making bullet holes in enemies. 1 Answer

Raycast hit ignores everything except for the terrain 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