- Home /
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.
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.
@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.
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.
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;
}
@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
Follow this Question
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