[SOLVED] Box doesnt detect collider

Hello guys i need to know why my player(box) cant detect collider in certain points, mostly in corners. My box can make a move only when it collides with a wall… on most places it works but as i said before, in some corners it doesnt detect collider.
Example:

it doesnt work in this corner and in other 2. im giving here link for game you can try it yourself also there is only one script so you can check it out if there is a problem in coding or in the scene with boxes…
But sometimes i think it bounces off the wall but i have disabled all related bouncings so not sure at all, would be so grateful for help.Thanks

https://playcanvas.com/editor/scene/1020288

I guess the issue is in the logic. I just commented this line and it started working perfectly.
image

Hello @smokys! The problem I see is the way you detect if the player can move or not. onCollisionStart and onCollisionEnd no longer go in sequence when multiple events are executed simultaneously. This can happen in the transition from one collider to the other or in this case in a corner where two and maybe even three colliders determine whether the player is free to move.

1 Like

The line has to be there because it prevents player from moving while player is moving already

1 Like

I think i understand what you tryna say, but, what could i do if i want to prevent player from moving while he is in move… this is the method i came out with yesterday and sound pretty good to me :stuck_out_tongue: … is there any other method i can use ?

I’m not sure why you want to prevent the player from moving if he is already moving? It may help to replace wasPressed with isPressed.

1 Like

Because i dont want him to change his direction until he hit the wall, its Tomb of the Mask type game

I didn’t research that game, but in that case I think you only need a collider detection once. When the player leaves a collider is irrelevant. The answer of @saif may then be the solution.

1 Like

Please note that you use two scripts that do the same.

1 Like

Set the canMove variable to false when you press the key rather than when the collision ends.

For this type of game, I would consider not using physics as it’s grid based and therefore quite easy to work out if the player is moving into a free space or not.

2 Likes

Oh yeah i totally forgot, it actually helped haha, partly… it was all god and when i tried to press all the buttons at once like psychopath it happened again :smiley:

Yes i changed it moment ago, when button is pressed then set to false. But it seem that it has a problem to detect a collision sometimes

Okey i found out why is the problem now.
When i reach the corner, and press the Up arrow it sets moving to false but as it already is in collision it wont enter it again so it wont set the canmove variable to true and it stays to false and therefore i cannot move

I probably need to set another condition if the box is in the collider then set canMove to true, i mean if box stays in collider and arrow keys are pressed it should still stay TRUE
What is the condition for “OnColliderStay” ?

A tip for building your level is using the Shift key (on Windows) when moving objects. Then everything stays nicely aligned on the grid.

1 Like

Yeah i know but somehow forgot about that, was never a good level designer haha. So is there a function for staying in collider ?

I know @LeXXik has made something for that. Maybe he can help you with this.

Yeah, I made an experimental extension for it, which you can find here: playcanvas.com/lexxik

However, I would probably not use it. As @yaustar suggested, I would go without physics engine here, since everything is grid based, you can do simple translations of the cube. If you do want to use physics, then instead of relying on colliders to report the collision, you can check the cube’s square root of linear velocity, and if it is below some low value, the cube is at rest, so you can continue moving.

var velocity = cubeEntity.rigidbody.linearVelocity.lengthSq();
if (velocity < 0.01) canMove = true;

You can tweak the minimum velocity value to find a good value that can be considered as a stop.

1 Like

First of all i tried translation but it ignores walls and go through and it seem to be much easier to work with collision and rigidbodies. cubeEntity should be this.entity when the script is attached to the cube? Also, do you have any tutorials with velocity as i actually have no idea what it is and how it works

EDIT: okay i found out and learned a bit about and it looks good but tweaking is so annoying :smiley:

  • need to know how to freeze box’s Y position

Yes, you access the linear velocity of the rigid body the same way as you apply an impulse to it. Currently you are looking at the linear vlocity at Y axis:

    if(this.entity.rigidbody.linearVelocity.y < 0.00005 ){
        canMove = true;
    }

That is going to check a vertical (from ground to the sky) velocity of the cube. You want the overall movement, so I’d recommend a .lengthSq():

    if(this.entity.rigidbody.linearVelocity.lengthSq() < 0.001 ){
        canMove = true;
    }

That works perfectly, thanks… The last thing i need to know is how to freeze Y position of the box… is there any way ?