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 Hektor · May 20, 2012 at 09:48 AM · raycastcolliderpathfindingcollider.raycast

collider.Raycast problem

I have this script that should turn an object when it approaches to another:

 function Update () {
     var mainRay = new Ray(transform.position, transform.TransformDirection(Vector3.forward));
     var mainHit : RaycastHit;
     var sight = 10.0;

     if (collider.Raycast(mainRay,mainHit,sight))  Turn();
     else  transform.Translate(Vector3.forward*speed*Time.deltaTime);
 }

For some reason it doesn't work and the Turn() function is never called. I don't want to use Physics.Raycast, because I don't want my character hitting corners or such things. What am I doing wrong?

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
4
Best Answer

Answer by kolban · May 20, 2012 at 04:50 PM

The Collider.Raycast() method casts a ray that will be true only if the ray hits THIS collider. The Physics.Raycast() method will cast a ray that will be true if it hits ANY collider. In your code fragment, I am assuming that you wish to cast a ray from the current game object outwards in some direction. As such, Physics.Raycast() is what you want. Using Collider.Raycast() will never be true as (if I am understanding correctly) you are always casting out from the game object trying to hit the collider which is yourself.

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 Kaze_Senshi · May 20, 2012 at 04:56 PM 0
Share

Considerating the performance and your explanation, so is the Collider.Raycast better to use than the Physics.Raycast or are they equal?

avatar image Hektor · May 20, 2012 at 05:05 PM 0
Share

Thanks for the help. I misunderstood the script reference...

avatar image kolban · May 20, 2012 at 08:18 PM 1
Share

@$$anonymous$$aze_Senshi - There is a fundamental difference between the Physics.Raycast and the Collider.Raycast. One would use Physics.Raycast to cast a ray to see if ANY collider will be struck in the direction of the casting. One would use Collider.Raycast to detect if a SPECIFIC collider (the one on which the Raycast is called) will be struck by the Raycast. With this in $$anonymous$$d, the two Raycast methods being discussed will never be "equal" as they do very different things. Obviously, if ALL you need to do is to detect if a casting hits a specific collider and no other collider, then the Collider.Raycast is likely to be more efficient.

avatar image Kaze_Senshi · May 21, 2012 at 10:12 AM 0
Share

Thanks for the answer kolban

avatar image
0
Wiki

Answer by Kaze_Senshi · May 20, 2012 at 02:16 PM

Hmmm the code compiles OK with that "Turn()" without ';' at the end? Here I use Physics.raycast instead of the collider one and it works well. I don't know well the difference between they, but you can try this one.

 if( Physics.Raycast( origin, direction, raycastHitObject, rayDistance ) )
 { /* ray collision detected */ }
Comment
Add comment · Show 2 · 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 Hektor · May 20, 2012 at 03:59 PM 0
Share

No, the compilation runs fine, the missing ';' was simply a typo. Physics.raycast doesn't do what I want. As far as I know it sends a ray that detects only the center of the objects, collider.raycast should detect also the points of the collider, but doesn't seem to work to me...

avatar image Eric5h5 · May 20, 2012 at 04:52 PM 0
Share

@Hektor: no, that's not what Physics.Raycast does.

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

Issues with Navmesh Pathfinding and casting a Ray on a transform position 0 Answers

Raycast 2D hits sprite with collider, but returns error when not hitting any collider 2 Answers

Raycast, Linecast, sphere collider or capsule collider? 0 Answers

using multiple colliders for a gameObject and it's children 1 Answer

raycasthit questions 2 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