- Home /
Only sideways colliding detection
Wassup, I'm making a video game (what a surprise :P ) in 2D.
When I'm jumping into wall in my game (eg. it's on my right side) and if I hold right arrow and when my character hit wall he stays in air bugging with the wall. I've tried making raycast wall detection but it worked only partly (when ray wasn't hitting platform character was still bugging with wall). How to repair this (it ain't have to be raycast solution)?
here's my working part of raycast wall detection:
bool RCheck() //does he touch wall on right?
{
Vector2 position = transform.position;
Vector2 direction = Vector2.right;
float distance = 1.0f;
RaycastHit2D hit = Physics2D.Raycast(position, direction, distance, groundLayer);
if (hit.collider != null)
{
return true;
}
return false;
}
Can you explain more what you would like to happen to it?
@Cornelis-de-Jager I want to make my character fall down while touching wall
Answer by SoshJam · Apr 12, 2018 at 01:02 AM
Create a collider that extrudes slightly from your object on the sides you want to detect it on. Run the script from those.
Answer by voytax · Apr 22, 2018 at 01:26 PM
Hi there, I've done some changes in my project, and I have another problem xD As @SoshJam wrote, I created mask on sides of my character to check colliding and I wrote some code for them, but it doesn't work, because it always return false.
Under character I created sprite mask and I gave them "background" sprite, so they are transparent.
Here's code:
public LayerMask Ground;
Collision2D coll;
public bool LC;
public void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.layer == Ground)
{
LC = true;
}
else
{
LC = false;
}
}
}
Your answer
Follow this Question
Related Questions
Player rotating after wall collision 1 Answer
How to keep player in screen? 1 Answer
Detecting collision on start in my building system 0 Answers
character controller sinks into platform while using raycast to detect collision 0 Answers
Problems defining when OnCollisionEnter should start functioning. 1 Answer