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 14ercooper · Apr 05, 2017 at 02:04 AM · raycastcollider2d

Raycast Does not Detect Collider

Probably going to feel stupid after getting an answer, but 4 hours of looking isn't getting me anywhere. I have a player move script that is supposed to send a raycast and not allow movement if the raycast hits a collider. However, no matter what, the raycast does not detect any colliders (the integer is always 0)

Here is my code: using UnityEngine; using System.Collections;

 namespace Player.control {
     public class PlayerMove : MonoBehaviour {
 
         [SerializeField] private float speed = 0.25f;
         [SerializeField] private Rigidbody2D rb2d;
         [SerializeField] private BoxCollider2D bc2d;
 
         void Update () {
             if (false) {
                 // Switches colliding list
             } else {
                 // Moves the player
                 float x = Input.GetAxis ("Horizontal") * speed;
                 float y = Input.GetAxis ("Vertical") * speed;
 
                 RaycastHit2D[] results = {};
                 if (bc2d.Raycast (new Vector2 (x, y),
                         results, 1.2f, Physics2D.AllLayers, Mathf.NegativeInfinity, Mathf.Infinity) != 0)
                     return;
 
                 rb2d.MovePosition (new Vector2 (this.transform.position.x + x, this.transform.position.y + y));
             }
         }
     }
 }
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 Namey5 · Apr 05, 2017 at 06:54 AM 0
Share

For one thing; Physics.Raycast returns a boolean value, so you don't have to check "!= 0". Just as a failsafe, try moving the move function call into an else statement under your raycast/return.

avatar image MelvMay ♦♦ Namey5 · Apr 05, 2017 at 06:58 AM 0
Share

This is not Physics.Raycast (3D) but Collider2D.Raycast (2D) and it returns an int containing the number of results returned.

avatar image Namey5 MelvMay ♦♦ · Apr 05, 2017 at 08:23 AM 0
Share

$$anonymous$$y bad, misread the statement.

2 Replies

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

Answer by turndapage · Apr 06, 2017 at 02:21 PM

Maybe try a different approach. Here is a script I just wrote that works for me:

     void CheckForObstacle()
         {
             // Looks ahead of character for obstacle
             Debug.DrawRay(transform.position, fwd * viewDistance);
             RaycastHit2D hitSolid = Physics2D.Raycast(transform.position, fwd, viewDistance, 1 << LayerMask.NameToLayer("Solid"));
             if (hitSolid)
             {
                 // If a collision was made, make sure the enemy is patrolling and move to the next position after it reaches the obstacle.
                 status = Status.patroling;
                 if (IsAtPosition(hitSolid.transform.position))
                     currentPoint++;
             }
         }
 

The layer "Solid" are objects that should block the character. I believe you should just use Mathf.Infinity and make sure the direction vector is facing the towards were the character is going. Also, you probably don't need to make an array of hits as it will always just return the first collider it finds.

Comment
Add comment · Show 1 · 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 14ercooper · Apr 07, 2017 at 01:22 AM 0
Share

Thanks, I used a modified version of this code and it worked :D

avatar image
2

Answer by MelvMay · Apr 05, 2017 at 06:53 AM

Here's the documentation: https://docs.unity3d.com/ScriptReference/Collider2D.Raycast.html

Here it states:

This function is similar to the [[Physics2D::RaycastNonAlloc]] function and in the same way, the results are returned in the supplied array. The integer return value is the number of objects that intersect the ray (possibly zero) but the results array will not be resized if it doesn't contain enough elements to report all the results. The significance of this is that no memory is allocated for the results and so garbage collection performance is improved when raycasts are performed frequently.

You pass in an empty array therefore you get zero results back.

Also note, you do not need to pass in the Physics2D.AllLayers, Mathf.NegativeInfinity, Mathf.Infinity as they are use the same values by default as the documentation shows.

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 MelvMay ♦♦ · Apr 07, 2017 at 06:41 AM 0
Share

$$anonymous$$ind of confused why this wasn' t best answer being as it's the reason why your code didn't work and wasn't returning result.

Can confuse others who look at posts.

avatar image 14ercooper · Apr 07, 2017 at 12:12 PM 0
Share

The code that I had didn't use the returned array, but rather uses the integer result.

avatar image MelvMay ♦♦ 14ercooper · Apr 07, 2017 at 02:17 PM 0
Share
          RaycastHit2D[] results = {};
          if (bc2d.Raycast (new Vector2 (x, y),
                  results, 1.2f, Physics2D.AllLayers, $$anonymous$$athf.NegativeInfinity, $$anonymous$$athf.Infinity) != 0)

The code that I had didn't use the returned array, but rather uses the integer result.

Your code passed in an empty array and would therefore never return any results whether you used them or not. It would obviously also always return zero (no results) no matter what which is what my answer points out.

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

94 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Scroll view not scrolling when colliders are in the way 0 Answers

2D raycast to UI problem 1 Answer

Raycast doesn't work on edge colliders in Unity 5.5.4 0 Answers

Cutting a Sprite 0 Answers

Is my 2D Raycast set up correctly? because it does not seem to hit anything when i use a layer mask. 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