0
\$\begingroup\$

I have a Player object with a BoxCollider2D component and a Ladder object also with a BoxCollider2D component. Each object has a script, and in each script, I set up references to the box colliders in the normal way.

public BoxCollider2D box_collider;

void Start ()
{
    box_collider = GetComponent<BoxCollider2D>();   
}

My player script is called "PlayerController", so that's the class defined in the script.

In my Ladder's script I have the following:

void Update ()
{
    GameObject player = GameObject.Find("Player");
    PlayerController player_controller = player.GetComponent<PlayerController>();
    if (player_controller.box_collider.bounds.Intersects (box_collider.bounds))
        Debug.Log ("collision");
    else
        Debug.Log ("no collision");

}

This script invariably returns "no collision" and I'm not sure why.

Any ideas?

\$\endgroup\$
2
  • 1
    \$\begingroup\$ As an aside, you don't want to be searching your entire scene doing string name comparisons on every object in every single Update, the way you are with GameObject.Find here. Caching the reference locally or publishing it somewhere that it's easy to look up will be a lot faster. Or, using collision events, you might not need such a player reference at all. \$\endgroup\$
    – DMGregory
    Jul 6, 2018 at 12:48
  • \$\begingroup\$ @DMGregory, thanks! I figured as much, but chose to present it this way just for the sake of asking the question. \$\endgroup\$
    – DyingIsFun
    Jul 6, 2018 at 16:56

2 Answers 2

2
\$\begingroup\$

I believe what you want to achieve is to set a ladder to be a trigger on the box collider and set up a method on the ladder OnTriggerEnter2D. When player will collide/trigger entering into ladder, the ladder will be informed about that.

https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html

void OnTriggerEnter2D(Collider2D col)
{
     // check tag of col and process
}

Of course you can also check collison of objects, there are few other methods that might help (remmember to turn of isTrigger on collider in this case):

OnCollisionEnter2D - Sent when an incoming collider makes contact with this object's collider (2D physics only).

\$\endgroup\$
3
  • \$\begingroup\$ Thanks, I will look into triggers. They seem to be similar to Godot's notion of signals. I would still like to know how to test for collision directly using just rectangles. Maybe associating a Rect with each object would be better than trying to do it through the BoxCollider2Ds. \$\endgroup\$
    – DyingIsFun
    Jul 6, 2018 at 16:58
  • \$\begingroup\$ @dietestus Have you taken a look at Collider2D.IsTouching, Physics2D.OverlapBox, and similar methods? \$\endgroup\$
    – DMGregory
    Jul 6, 2018 at 19:37
  • \$\begingroup\$ @DMGregory, I have just taken a look, and they seem very useful. Thanks again! Part of the reason I was attracted to rects is because in the past, I have associated custom rect classes with my game objects, and the rects had attributes like topleft, midbottom, etc. I always found that having access to these points was very helpful to me. But maybe I won't need them in Unity. \$\endgroup\$
    – DyingIsFun
    Jul 6, 2018 at 20:46
1
\$\begingroup\$

Another way, aside from using Ontrigger or OnCollision to find a intersection like ground, water, enemies ect.. Is to use layers and cast a check to see if you contacted one.

Physics2D.OverlapCircle(playerPosition, collisionRadius, ladderLayer)

here is a example of a bool returning true if I am touching the layer

[SerializeField]
private LayerMask groundLayer;
private float collisionRadius = .23f;
Transform playerFeet
public bool grounded = false;

groundLayer

 public bool GroundCheck()
{
    return grounded = Physics2D.OverlapCircle(playerFeet.position, collisionRadius, groundLayer);
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .