0

How to use RigidBody2D.Cast

Iurii 6 days ago • updated 3 days ago 0

Hello!
i am a complete noob in C# and Bolt is an amazing tool that lets me do simple scripting.

My question is about RigidBody2D.Cast, I want to use it to check if my player will hit the wall on the next turn, since my game is turn-based i want to prevent the player from moving any further, closer to the wall, knowing that he will collide with it. I have background tiles with composite collider, as well as a collider on the player.

I am 100% sure that my player is hitting the wall at the point when screenshot is done, but RigidBody2D.Cast keeps returning me zero.
(I double check that collision is happening with regular ray.Cast, but i want to use collider shape instead for better precision)

My idea on how it should work is based on this example, which i found on Unity Forum:

    Rigidbody2D rb = GetComponent<rigidbody2d>();
    RaycastHit2D[] hitResults = new RaycastHit2D[5];
    int numberHit = rb.Cast(Vector3.right, hitResults, 5);
    if(numberHit > 0)
    {
       print("Hit " + numberHit + " colliders.");
       for (int i = 0; i < numberHit; ++i) print("Hit this: " + hitResults[i].collider.name);
    }

    So in my flow macro I want to check too if number of collisions is grater than o and prevent player from moving further.

    Not sure what am I doing wrong and if it is the best way to do it, will appreciate any advise.
    Thank you, here is the screenshot:

    Bolt Version:
    1.4.1
    Unity Version:
    2018.3
    .NET Version:
    .Net 4 x equivalent