Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Pleroma · Oct 23, 2016 at 06:51 AM · raycast2d gamecoroutinescenes

2D Raycasting Woes

I am building a 2d Zelda-like game, where the player moves in all four directions. The player is meant to detect the presence of objects near them, and kneel (play an appropriate animation) towards the object.

In order to do this, I set up a system of Empty game objects just outside of the players box collider, and used them as the anchor points for four Physics.2D raycasts, since anchoring the rays in the player themselves would cause the rays to detect the player's own collider (there's probably a more elegant way, but I went with what I could find). The code detected when the ray hit a collider, checked it for the appropriate tag, and if everything checked out the player would play the animation. This was seperate from a system that would simply check for collisions with the player's collider, which served as a way of affecting a single instance of an object (so that picking up one pot didn't lift them all, for example). The code looked like this:

 //Raycasts to determine if objects tagged Interactable are nearby, activates anim trigger if true
     void ProxiDetect(){
         RaycastHit2D Up = Physics2D.Raycast (upcheck.position, Vector2.up, 1);
         RaycastHit2D Right = Physics2D.Raycast (rightcheck.transform.position, Vector2.right, 1);
         RaycastHit2D Down = Physics2D.Raycast (downcheck.transform.position, Vector2.down, 1);
         RaycastHit2D Left = Physics2D.Raycast (leftcheck.transform.position, Vector2.left, 1);
         if (Up.collider != null && Up.collider.gameObject.tag == "Plant") {
             anim.SetBool ("ObjU", true);
             anim.SetBool ("ObjD", false);
             anim.SetBool ("ObjRL", false);
         }else if (Right.collider != null && Right.collider.gameObject.tag == "Plant") {
             anim.SetBool ("ObjU", false);
             anim.SetBool ("ObjD", false);
             anim.SetBool ("ObjRL", true);
         }else if (Down.collider != null && Down.collider.gameObject.tag == "Plant") {
             anim.SetBool ("ObjU", false);
             anim.SetBool ("ObjD", true);
             anim.SetBool ("ObjRL", false);
         }else if (Left.collider != null && Left.collider.gameObject.tag == "Plant") {
             anim.SetBool ("Kneelleft", true);
             anim.SetBool ("ObjU", false);
             anim.SetBool ("ObjD", false);
             anim.SetBool ("ObjRL", true);
         } 
 
         if (Left.collider == null || !(Left.collider.gameObject.tag == "Plant")) {
             anim.SetBool ("Kneelleft", false);
         }
 
         if (Up.collider == null || !(Up.collider.gameObject.tag == "Plant") && Right.collider == null || !(Right.collider.gameObject.tag == "Plant")&& Down.collider == null || !(Down.collider.gameObject.tag == "Plant")&& Left.collider == null || !(Left.collider.gameObject.tag == "Plant")) {
             anim.SetBool ("ObjU", false);
             anim.SetBool ("ObjD", false);
             anim.SetBool ("ObjRL", false);
         }
     }

And it was working fine.

Until I started adding more scenes to my build.

Now, the raycast works...sometimes. Sometimes on load, the player will only recognize the left-ray, and will ignore the rest, sometimes it will only recognize the up-ray. Sometimes it simply won't recognize them at all. Moving from scene to scene sometimes helps, but as of late it has stopped working. The player is still connecting with the collider, but something in the physics 2d Raycast is not functional.

What is going on? This is really quite disheartening.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Naphier · Oct 23, 2016 at 08:21 AM

The addition of scenes, in itself, would not affect raycasting in any way. You need to debug this. The way I'd start by making public strings to view in the inspector what is actually being hit by the raycasts. So, for each of your raycasts add a public string, if the raycasthit is not null then fill the value with the hit game object's name. Could be the origin of the ray is inside of the player's collider.

Next step would be to use the Debug.DrawLine method to draw your rays, change their color when they hit something, and reduce the endpoint to the raycast hit point.

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

Why does ScreenPointToRay bug when called from IEnumerators? 1 Answer

shoot to the mouse cursos 2 Answers

Change a prefab's sprite in all scenes? 2 Answers

Use 2D Effectors with Raycast Collision Method 0 Answers

Top Down Shooter Player Controller 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