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
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.
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 … is there any other method i can use ?
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.
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.
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
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” ?
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.
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
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: