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 Baenor · Aug 29, 2015 at 04:13 PM · unity 5raycastcollision detectionraycastingraycasthit

RayCast2D problems on collision detection

Hello There, I was tryng, for learning purposes, to make a frogger-like game. For collision, instead of Rigidbodies i wanted to used Raycast2D. However things doenst react as expected:

alt text as you can see from the image, i'm using Debug.DrawRay to draw the actual rays ( for now are both up and bottom ray). In the scene view, the box collider is just a boundary box used to negate the player going outside the "map". However, the collision with raycast is only detected in this situation :

alt text can you tell me why please? This is driving me crazy.. Thanks

I'm using this code http://pastebin.com/itnKXhzU

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hulahoolgames · Aug 29, 2015 at 06:38 PM

Is the raycast colliding with the black cube object? I would recommend to put a breakpoint in your collision code to see what collider the Raycast is returning is both cases.

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

Answer by tauqeerahmed · Aug 29, 2015 at 08:28 PM

Do you want to detect the collision between the car and the blackbox using raycast ??

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

Answer by Suddoha · Aug 29, 2015 at 09:56 PM

There is a mistake in your code and some other things that can or should be changed.

First of all, you set the booleans which indicate that a collision has occured always to false, whether or not the raycasts hit an object tagged 'Bounds'.

The methods return, update continues and you allow to move as long as the booleans are false, due to the inversion ( !collidingUp). Remember, you always set both to false in any case, thus this condition will always be true, because !false == true. The result: you can always move. You have to reset them at the beginning of the methods, not at the end, so that they will generally be false, as long as no raycast hit the bounds.

Moreover, you should specify a length for the raycasts, otherwise they will use Math.Infinity if i'm not mistaken, which means you'll always detect your bounds, which in turn means, you'll always 'collide'. You have to determine the length yourself, as i don't know the exact distances in your game.

And a last side not so far: You could also generalize the collision method so that you do not need one for every direction, but that's another thing. For example, you could have parameters for the direction to cast (Vector2 direction), a reference to the boolean you want to alter within the method (ref bool someName) and a length (float length).

Also note, that you may need to adjust the direction parameter for the Debug.DrawRay method, as the ray will be drawn from startPosition to startPosition + direction, at least that's what the description says.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast exit point of collider 0 Answers

How do i get local Hit position of Raycast on a collider 1 Answer

Another Raycast Issue. 0 Answers

Raycast Not Drawing In Target Direction 0 Answers

Help with Raycast C# 0 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