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 Coaster-Mind · Apr 29, 2016 at 03:10 PM · c#2dmovementcolliderraycasting

RaycastHit2D not getting any information?

Hi again.

I try to make a character movement and control out of raycasting but now I got into a problem.

Its not hitting anything although there is a collider there. ![alt text][1]

![alt text][2]

And this is the script:

 using UnityEngine;
 using System.Collections;
 
 public class Controller : MonoBehaviour {
 
     public float OnGround = 2.5f;
 
     //Planing to add ((public Rigidbody2D rb2D;))
     void Start() {
         //Planing to add ((rb2D = GetComponent<Rigidbody2D>();))
     }
     void FixedUpdate() {
         transform.Translate (Vector2.down * 0.1f);
 
         RaycastHit2D hit = Physics2D.Raycast(this.gameObject.transform.position, Vector2.down);
         if (hit.distance <= OnGround) {
             print ("Stop!");
             transform.Translate (Vector2.zero);
         }
     }
 }


As I said it doesn't detect the collider. any ideas?

Thanks for reading. -Coaster Mind

test-222.png (66.9 kB)
test-222.png (71.5 kB)
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 incorrect · Apr 29, 2016 at 03:10 PM 0
Share

Are you raycasting inside this collider?

avatar image Quertie · Apr 29, 2016 at 03:18 PM 0
Share

Thanks for the pictures. Perhaps you could add a DrawRay() method in your FixedUpdate function, with a length of OnGround so that we can see exactly where the ray is ?(just to make sure we are not missing the obvious)

avatar image Coaster-Mind Quertie · Apr 30, 2016 at 08:37 AM 0
Share

Thanks for re$$anonymous$$ding. I forgot to post this picture to. Hope it helps. alt text

There's no collider on the raycast sprite.

test-222.png (10.4 kB)

2 Replies

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

Answer by Coaster-Mind · Apr 30, 2016 at 11:42 AM

Solved!

The transform.Translate (Vector2.down * 0.1f); was first without a else so the script didn't know that it did need to stop it when the ray gave information to do it.

Updated and working script: RaycastHit2D hit = Physics2D.Raycast(this.gameObject.transform.position, Vector2.down); if (hit.distance <= OnGround) { print ("Stop!"); transform.Translate (Vector2.zero); } else transform.Translate (Vector2.down * 0.5f);

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 Quertie · Apr 30, 2016 at 11:53 AM

Hello,

As it turns out, 3d Raycasting does not work with 2D colliders. A good alternative to raycasting could be to add a 2D collider to your character (or 2dTrigger, depending on how you plan on doing it).

Hope this works :)

Comment
Add comment · Show 4 · 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 Coaster-Mind · Apr 30, 2016 at 01:12 PM 0
Share

Why does my raycast work on colliders then? ;) And yes, all 2d

avatar image Quertie · Apr 30, 2016 at 01:22 PM 0
Share

I am glad it does ^^ Surprised though! The only thing is, I mistook the print("stop!"); line for a Debug.Log("Stop!"); line... So I thought you were supposed to get a "stop" message and you didn't, which would have been proof that no collision was detected ^^

avatar image incorrect Quertie · Apr 30, 2016 at 03:10 PM 0
Share

Surprised? Really? **Physics**.Raycast() works with 3D physics and does not with 2D physics, **Physics2D**.Raycast() works with 2D physics and does not with 3D physics. Those are methods of different classes.

avatar image Quertie incorrect · May 01, 2016 at 11:03 AM 0
Share

Oh I didn't realise this was a Physics2D Raycast ! Thanks for pointing that out; I was getting lost :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Player ignores tilemap collider on fall 1 Answer

wasd movement rigidbody no bouncing 0 Answers

Game object should move like a fish in 2D C# 1 Answer

Unusual error regarding movement in a top down 2d sidescroller 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