• 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 Cratuss96 · Nov 02, 2020 at 02:09 AM · raycasthit2d

Raycast2D only hit Trigger Collider

Hello,
i have a problem with my switch in a simple 2D game.

I use 2 colliders:

  • one for checking if the Player is inRange(isTrigger) to use the switch (Bigger one)

  • and one and one for his Hitbox. (Smaller one)


then i use a Raycast2D whenever i click on my mouse and if the Object this Raycast hits is the switch, then it will call Use()


the problem is that the raycast always hits the trigger that is only used to determine the range. I already tried to ignore the trigger by using !hit.collider.isTrigger in an if statement but then it will do nothing because its the only collider which was hit. I also tried to deactivate the "Queries hit triggers" in the physics settings but that changed nothing.

Comment
Add comment · Show 1
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 Cratuss96 · Nov 02, 2020 at 02:11 AM 0
Share

alt text

unbenannt.png (15.2 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by I_Am_Err00r · Nov 02, 2020 at 03:01 PM

A boxcast2D method can check for player, and you wouldn’t need two separate colliders for the ray to get confused by, just the one you want the RaycastHit2D to check.


Here is a generic example from Unity API:


RaycastHit2D BoxCast(Vector2 origin, Vector2 size, float angle, Vector2 direction, float distance = Mathf.Infinity, int layerMask = Physics2D.AllLayers


Replace origin with the transform.position of the switch and the size with how big you want the boxcast to check for, and then you can plug a 0 for angle, direction, and distance; but for the layemask, you need to choose the player.


The easiest way to check for player layer is to give this switch a variable of a public layermask, and then you can choose the player in the inspector.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

136 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 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

LinecastNonAlloc, documented syntax gives error... 1 Answer

What's wrong with my raycasthit2D? 2 Answers

Unity Physics2D.Raycast always returns “the invoker” as hit 2 Answers

Problem with raycast vertical 1 Answer

hi,Why the raycast is having object reference error on the : "if (hit.collider.gameObject.tag == "Player") " line? thanks 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges