F
F
Falconoff2021-06-28 22:36:09
C++ / C#
Falconoff, 2021-06-28 22:36:09

How to prevent a character from exiting colliders using raycast?

2d top down game. The bottom line is that the character can only walk on blocks that he can place. Colliders hang both on the character and on the blocks. How to make it so that the character could not go beyond these colliders?
hit = Physics2D.BoxCast(transform.position, boxCollider.size, 0, new Vector2(0, moveDelta.y), Mathf.Abs(moveDelta.y * Time.fixedDeltaTime), LayerMask.GetMask("Block"));
if (hit.collider != null) transform.Translate(0, moveDelta.y * playerSpeed ​​* Time.fixedDeltaTime, 0);

hit = Physics2D.BoxCast(transform.position, boxCollider.size, 0, new Vector2(moveDelta.x, 0), Mathf.Abs(moveDelta.x * Time.fixedDeltaTime), LayerMask.GetMask("Block"));
if (hit.collider != null) transform.Translate(moveDelta.x * playerSpeed ​​* Time.fixedDeltaTime, 0,

I wrote this code, but the character goes beyond the block collider until it leaves it completely. But I need the block collider not to let the character's collider out of itself, and plus if another block appears nearby, then the character could easily pass through them

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
Hixac, 2021-06-30
@Hixac

Isn't it easier to do the opposite? Limit character movement with colliders.
Why make it so difficult?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question