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 siddharth3322 · Nov 05, 2015 at 04:54 PM · playerunity 2dcollision detectioncollider2dunity 4.6

Wall checking not working correctly

Basically I am working on side scrolling 2d game. For that I want to check whether player is collided with wall. I have written some code for it but can't able to decide where I have done stupid mistake. So please guide me in this. If you need any more details then I am always available.

Code for detecting wall :

             RaycastHit2D hit = Physics2D.Raycast (wallCheck.position, Vector2.right, wallCheckDistance);
 
         if (Physics2D.Raycast (wallCheck.position, Vector2.right, wallCheckDistance)) {
             Debug.Log ("collider name : " + hit.collider.name);
             isDead = true;
         }
 
         Debug.DrawRay (wallCheck.position, Vector3.right * wallCheckDistance, Color.red);

Following image gives you all idea about situation. In that I have allocated player with groundCheck and wallCheck flag. At present in wallCheck section, I am detecting ground as collider response after execution of above code. Above code I have placed in Update method for continuous checking.

alt text

screen-shot-2015-11-05-at-102217-pm.png (298.6 kB)
Comment
Add comment · Show 2
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 ijidau · Nov 05, 2015 at 11:59 PM 0
Share

Is there a reason you are using RayCasts ins$$anonymous$$d of just RigidBody physics with colliders? Sounds to me like you are using the wrong tool for the job.

avatar image siddharth3322 · Nov 06, 2015 at 09:36 AM 0
Share

@ijidau Thanks for reply. For this game, I want to do game over if wallCheck object detect any object in collision because it is some what above the ground.

Why wallCheck object detecting collision of ground object (stage1a)? That is big question of $$anonymous$$e.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by $$anonymous$$ · Dec 04, 2015 at 04:05 PM

OK< I think I get what is being asked. The question is put wrong though.

The question is how does he set the collision to detect and when colliding with said object to cause it to more or less kill the player as a Game Over Notice. The problem is he is using a Debug checker for detection of collision notice instead f calling the function when actually making the contact.

siddharth3322, you don't use collision detection that way. Collision detection is simply to make sure your character is actually interacting with all the objects in the environment and the debug part is to simply give you a notice to tell you the detection and interactions are working or not.

You need to create script like DestroyByContact that then after your character is "Killed/destroyed" by a hazard object, enemy, or weapon, it displays a Game Over notice in the UI (User Interface), such as when setting up a menu with a canvas Panel and having the script pull that notice.

Otherwise collision detection is just that and nothing more than that. Its not for destroying object or putting up Game Over UI elements. The Debug is only to tell you if something is working or not working.

These tutorial may be closer to what you are wanting to do.

https://www.youtube.com/watch?v=FRmKagHdfUE https://www.youtube.com/watch?v=9z7_tlX8OTg

I do not know though what version of Unity is being used, so the first one is older using JavaScript and 4.6. while the second is more current using C# and a version of Unity5.

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 SeasiaInfotechind · Nov 06, 2015 at 04:59 AM

I think it should be like this because you are no where comparing whether hitting a wall or any other object.

  RaycastHit2D hit = Physics2D.Raycast (wallCheck.position, Vector2.right, wallCheckDistance);
  
          if (hit.collider.name =="Your Wall name")) {
              Debug.Log ("collider name : " + hit.collider.name);
              isDead = true;
          }
  
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 siddharth3322 · Nov 06, 2015 at 09:32 AM 0
Share

@SeasiaInfotechind, Thanks for your reply. Collision with any object in environment, I want to do game over. $$anonymous$$y wallCheck object is very much above the ground so why it is detecting stage as collision object? That is big question for me.

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

34 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

Related Questions

Unity2D collider of children object always hits in the same direction even after being flipped. 0 Answers

How Do I Ignore a Collision Between an Enemy and the Player? 1 Answer

Platform Collider Not Working When Enemy Collides With It 1 Answer

Why do GameObjects need Rigidbodies to collide? 0 Answers

How to resolve colliders being within each other without firing them off in all directions at maximum velocity 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