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 immersivegamer · Mar 01, 2012 at 12:42 AM · raycastairaycastingplatformer

Diagonal Raycasting to detect platform returns true, even when false

So I want to make a dynamic AI enemy using ray casting in a two dimensional platformer game. The objective is to have it detect whether it is at the edge of a platform so that it can turn around. If it worked I could just plop down the enemy anywhere on a platform and would not have to set up a bunch of triggers which can be slow. The only thing is that the physics.raycast is returning falsely. When there is no platform it still returns true, as if it is hitting the platform. I even made a debug.drawray to check that the numbers were right. Here is the code.

 function DropCheck () {
 
     
 
     drop.start = transform.position + drop.offset;
 
     var forwards = transform.TransformDirection(drop.angleForward);
 
     var back = transform.TransformDirection(drop.angleBackward);
 
 
 
     Debug.DrawRay (drop.start,forwards*drop.lenght,Color.blue);
 
     if(!Physics.Raycast(drop.start,forwards,drop.lenght)){
 
         Debug.Log("No ground");
 
     }
 
 
 
     Debug.DrawRay (drop.start,back*drop.lenght,Color.blue);
 
     if(Physics.Raycast(drop.start,back,drop.lenght)){
 
         Debug.Log("Ground");
 
         }
 
     else{
 
         Debug.Log("no ground");
 
         }
 
     
 
 }

The drop.angleForward is a Vector3(1, -1, 0), and drop.angleBackward is a Vector3(-1,-1,0). For my tests a length of 2 intersects the platform and returns as true, which is what I want. The drop.offset was to test in playmode whether changing the origin would work.

Perhaps I just not understanding how ray casting really works. I'm imagining it as a single line through the 3D space, though here it is acting, well not quite sure, more like a plane?

Comment
Add comment · Show 2
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 Kleptomaniac · Mar 01, 2012 at 06:24 AM 0
Share

Hey there, I didn't really get time to read your code, but you are misspelling .length every time! Try editing it and see if this fixes your problem.

avatar image immersivegamer · Mar 01, 2012 at 07:01 AM 0
Share

haha, yup your right. lucky, or perhaps unlucky, once I had defined that class variable I just used the IDE's auto fill. So it is consistent and doesn't effect this script.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by jspease · Mar 01, 2012 at 06:43 AM

Maybe your raycast is hitting some other collider besides the platform, such as a collider on your AI object itself. Usually you need to specify a layerMask in the last (optional) argument to Physics.Raycast to avoid that sort of problem. For example, you could set the layer on your object to something different from the platform, and then do something like:

 var layerMask = ~(Physics.kIgnoreRaycastLayer | (1 << gameObject.layer));
 if(Physics.Raycast(drop.start, back, drop.length, layerMask)) {
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 immersivegamer · Mar 01, 2012 at 06:58 AM 0
Share

Well I did think that might be the case. I was going to eventualy try layers, but the thing is if I put it on, lets say,the player character which can move and jump. If I jump then the raycast returns a false value, i.e. not hitting anything. However if I move right to the edge of a platform, and I can see that the debug.drawray is over the edge, it still returns true. I even put in the off set and no matter how far horizontally I move the ray it still says it hits, though if I move the ray up enough, or down, it returns false, go figure. I even tried one ray at a time and at both ends of the platform.

avatar image jspease · Mar 01, 2012 at 07:48 AM 1
Share

Then, I would use a version of Physics.Raycast that takes "out hitInfo : RaycastHit". The RaycastHit gives you all sorts of useful information, for example it tells you the exact point it thinks the ray hit (so you can draw a line to it with Debug.DrawLine), and it tells you which transform it hit so you can easily print out the name of which gameObject the ray hit. I don't see anything really wrong with what you posted, but maybe there's a problem with how you set up the collision or game objects in your scene.

By the way, maybe you should normalize your "forwards" and "backwards" vectors, since TransformDirection will not automatically normalize them. I think it shouldn't matter for Physics.Raycast, but it's nice to have the correct direction vector for your other calculations and debug drawing.

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

Finding RayCastHit's Origin Position 2 Answers

Will I need to use raycasts for my 2D game? 1 Answer

Raycasting and enemies ai 2 Answers

AI Raycast problem 1 Answer

Can RaycastHit2D.point be inside a BoxCollider2D if raycast was performed from outside that collider? 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