Using soft body physics in Unity 2D, can't get colliders to detect ground for movement script.

I’m using soft body physics in my 2D game, but I can’t get the objects used in the soft body physics to detect ground with isTriggered colliders to make the movement script work. I attached screenshots of the project window to give a clear understanding of how I have things set up. I’m not sure if the movement script should be on Asset1(1), the parent, or Center Point, the second child. Here is the code in the movement script:

private Rigidbody2D rb;
public CircleCollider2D[ ] col;

public Vector3 pos { get { return transform.position; } }

public bool canJump;

// public AudioSource audio;

private void Awake()
{
rb = GetComponent<Rigidbody2D>();
col = GetComponentsInChildren<CircleCollider2D>();
}

private void Start()
{
print(col);
}

//adds movement force to character
public void Push(Vector2 force)
{
rb.AddForce(force, ForceMode2D.Impulse);

}

public void ActiviateRB()
{
rb.isKinematic = false;
}

public void DeactiviateRB()
{
rb.velocity = Vector3.zero;
rb.angularVelocity = 0f;
rb.isKinematic = true;
}

//checks if player is on ground and lets them jump again/landing
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.transform.tag != "Player")
{
if (collision.CompareTag("Ground"))
{
print("on ground/ln 47");
//audio.Play();

if (!Input.GetMouseButtonDown(0))
{
rb.constraints = RigidbodyConstraints2D.FreezePositionY |
RigidbodyConstraints2D.FreezePositionX |
RigidbodyConstraints2D.FreezeRotation;

canJump = true;
}
}
}
}

//checks when player is in the air
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.transform.tag != "Player")
{
if (collision.CompareTag("Ground"))
{
rb.constraints = RigidbodyConstraints2D.FreezeRotation;

canJump = false;
}
}
}

private void OnCollisionStay2D(Collision2D collision)
{
if(collision.transform.tag != "Player")
{
if (Input.GetMouseButtonDown(0))
{
rb.constraints = RigidbodyConstraints2D.FreezeRotation;

canJump = true;
}
}
}


If the thing that is moving is marked as trigger, it won’t collide with the ground but rather just fall through.

Are you trying to have the objects sense their nearness to the ground without actually triggering the hard-bounce physics? I think the way to do this is to make the ground be the trigger, and then the colliders on your moving blob(s) would be non-trigger, but would get trigger calls.

I’m trying to have the thing that is moving touch the ground, recognize it hit the ground and freeze the position and rotation, so I don’t think having the trigger on the ground objects would work. Unless, I added a character reference in the script and used that to freeze the position and rotation. Do you think that would work?

Actually I kinda think it might. The difference might be if the blob is going really fast, it will penetrate the trigger much farther, where you’ll see it frozen. If you want it to stick on the outside, I think you need to use a collider, not the trigger.

Either way, what is ON the rigidbody object has got to be a non-trigger to get the messages, I believe. Certainly seems that way to me just tinkering here in 2D physics.

If after all that things are not working, it’s probably a tag problem or something else in your logics. First make sure those triggers and/or collider functions are being called at all (see below), and check out the Unity docs for what criteria you need to meet to be called.

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

So just to be clear, you’re saying leave my code as it is and just take off the trigger colliders from the blob and add triggers to the ground objects?

This is what worked for me:

using UnityEngine;

// I put this on my falling ball (with Rigidbody2D)
// the ball has a CircleCollider2D and it is is NOT trigger
// the ground is trigger (a Box2D in this case)
// the faster this hits the ground trigger, the deeper it goes in before freezing, on average

public class FreezeThereBuddy : MonoBehaviour
{
    Rigidbody2D rb2d;

    void Start()
    {
        rb2d = GetComponent<Rigidbody2D>();
    }

    void OnTriggerEnter2D( Collider2D col)
    {
        Debug.Log( "Hol up...");

        rb2d.constraints = RigidbodyConstraints2D.FreezeAll;
    }
}

Just threw this in on the above:

    void OnTriggerExit2D( Collider2D col)
    {
        Debug.Log( "As you were...");

        rb2d.constraints = RigidbodyConstraints2D.None;
    }

and now when I yank the collider away, my ball continues to fall.

I tried it the way you have it and it’s not working.

here is a link to whats going on and what i did Blob project test - Album on Imgur

Wait… so its good and working now?