- Home /
How to detect collision on only one side of an object? [C#]
Hi
I am making a platformer, and I was wondering to make it possible to detect a collision only on one side of an object. You see, when I jump and I hit a platform on the side I can also "wall jump" and I don't want that. Is there a(n easy) way to detect collision on one side? (Preferabely in C#)
This is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMovement : MonoBehaviour {
public Rigidbody rb;
public float Speed = 25f;
public float JumpForce = 50f;
private bool CanJump = false;
// Update is called once per frame
void FixedUpdate () {
if ( Input.GetKey("right") )
{
rb.AddForce(Speed, 0, 0);
}
if ( Input.GetKey("left") )
{
rb.AddForce(-Speed, 0, 0);
}
if ( Input.GetKey("space") )
{
Jump ();
}
}
void Jump(){
if (CanJump) {
CanJump = false;
rb.AddForce(0, JumpForce, 0);
}
}
void OnCollisionEnter (Collision collidingObject) {
if (collidingObject.gameObject.name == "Platform") {
CanJump = true;
}
}
}
Answer by TreyH · Sep 25, 2018 at 07:48 PM
It might be easier to just use a trigger collider to provide a jump, covering only that one side.
That said, you can get the collision points for any collision, so you can check the outward facing surface direction for each contact point. In the event you wanted to check that someone touched the "top" of your platform, you might try something like this:
private Vector3 validDirection = Vector3.up; // What you consider to be upwards
private float contactThreshold = 30; // Acceptable difference in degrees
void OnCollisionEnter (Collision col)
{
if (col.gameObject.name == "Platform")
{
for (int k=0; k < col.contacts.Length; k++)
{
if (Vector3.Angle(col.contacts[k].normal, validDirection) <= contactThreshold)
{
// Collided with a surface facing mostly upwards
CanJump = true;
break;
}
}
}
}
Answer by Sabre-Runner · Sep 25, 2018 at 07:36 PM
I see only two options for doing what you want: 1. Check the direction of the collision when it is detected. 2. Make up your object with several colliders and check which one is hit.
The first one is more difficult, implementation-wise, the second could be more performance intensive.
Answer by ozdenmurat · Sep 19, 2020 at 04:54 PM
You can add to your player two different colliders in different places, forexample like this; capsule collider for head, box collider for legs.
public class Player : MonoBehaviour
{
Rigidbody2D rb;
CapsuleCollider2D myhead;
BoxCollider2D myfeet;
void Start()
{
rb = GetComponent<Rigidbody2D>();
mycollider = GetComponent<CapsuleCollider2D>();
myfeet = GetComponent<BoxCollider2D>();
}
Then you can use colliders for different purpose ;
private void Jump()
{
if (myfeet.IsTouchingLayers(LayerMask.GetMask("Foreground")))
{
if (CrossPlatformInputManager.GetButtonDown("Jump"))
{
//rb.AddForce(Vector2.up * jumpSpeed);
rb.velocity += Vector2.up * jumpSpeed;
}
}
i hope this will help you.
Answer by nomanbangash97 · Mar 25 at 06:39 AM
You can check the direction of a collision where it hits an object.
public enum hitX { left, mid, right, none}
public enum hitY { up, mid, down, none }
public enum hitZ { forward, mid, backward, none }
public hitX hitx = hitX.none;
public hitY hity = hitY.none; public hitZ hitz = hitZ.none;
public void OnCharacterColliderHit(Collider col)
{
hitx = GetHitX(col);
hity = GetHitY(col);
hitz = GetHitZ(col);
if(hitz == hitZ.forward && hitx == hitX.mid)
{
if(hity == hitY.mid)
{
Debug.Log("you died");
}
}
}
public hitX GetHitX(Collider col)
{
Bounds char_bounds = m_char.bounds;
Bounds col_bounds = col.bounds;
float min_x = Mathf.Max(col_bounds.min.x, char_bounds.min.x);
float max_x = Mathf.Min(col_bounds.max.x, char_bounds.max.x);
float average = (min_x + max_x) / 2f - col_bounds.min.x;
hitX hit;
if (average > col_bounds.size.x - 0.33f)
hit = hitX.right;
else if(average < col_bounds.size.x - 0.66f)
{
hit = hitX.left;
}
else
{
hit = hitX.mid;
}
return hit;
}
public hitY GetHitY(Collider col)
{
Bounds char_bounds = m_char.bounds;
Bounds col_bounds = col.bounds;
float min_y = Mathf.Max(col_bounds.min.y, char_bounds.min.y);
float max_y = Mathf.Min(col_bounds.max.y, char_bounds.max.y);
float average = ((min_y + max_y) / 2f - char_bounds.min.y)/ char_bounds.size.y;
Debug.Log("from y hit"+average);
hitY hit;
if (average < 0.3f)
hit = hitY.up;
else if (average < 0.5f)
{
hit = hitY.mid;
}
else
{
hit = hitY.down;
}
return hit;
}
public hitZ GetHitZ(Collider col)
{
Bounds char_bounds = m_char.bounds;
Bounds col_bounds = col.bounds;
float min_z = Mathf.Max(col_bounds.min.z, char_bounds.min.z);
float max_z = Mathf.Min(col_bounds.max.z, char_bounds.max.z);
float average = ((min_z + max_z) / 2f - col_bounds.min.z) / char_bounds.size.z;
Debug.Log("from z hit"+average);
hitZ hit;
if (average < 0.33f)
hit = hitZ.backward;
else if (average < 0.66f)
{
hit = hitZ.mid;
}
else
{
hit = hitZ.forward;
}
return hit;
}
after that you can call this function (OnCharacterColliderHit(Collider col) ) in onCollisionEnter method. I hope it helps. @ESENK @Lapraniteon