Topic was automatically imported from the old Question2Answer platform.
Asked By
THE HELIX
I need to make the viewport as a barrier / a wall to hold the blocks which are falling along the y-axis from above.I put four staticbody 2D with collission shape around the viewport to create a barrier and attached the code to the player as below
When I execute it the viewport acts as a wall/barrier.The spawned blocks are held by the viewport wall.After some time more blocks fall . When the lower blocks are cleared the upper blocks do not come in the place of the cleared blocks but stay where they where and comes down only when more blocks fall on them.When I make velocity.y *= 1 and velocity.x *= 1 ; the blocks come down if the lower blocks are cleared.But when more blocks fall on them they pass through the collission body and fail to detect collissions .I even put staticbody around the viewport but they fail to detect collission.
Can you please help me out ?
The easiest way to do this would be to have 2 raycasts one facing down to the left and one down to the right. Make sure the cast_to.y of the raycasts are just far enough to only detect one block down. You can then use is_colliding() to check if anything is below it and have it move across to the empty space. Get rid of velocity.x & y *= 1 and move it accordingly.
if left_ray.is_colliding() and right_ray.is_colliding():
//Both have returned an object, no empty spaces.
velocity.x = 0
elif !left_ray.is_colliding() and !right_ray.is_colliding():
//Both have not returned any object, block still falling.
velocity.x = 0
elif !left_ray.is_colliding():
//Left slide is empty
velocity.x -= your_movement_speed
elif !right_ray.is_colliding():
//Right slide is empty
velocity.x += your_movement_speed
Here is the project . Here in the Circle script I have used the code which I put in the question. If you change velocity. x and velocity. y with 0 & 1 and run the spawner.tscn you can see that the circles fail to detect collision by moving through the collision body with velocity. y = 1 and with velocity. y = 0 they do not come down on clearing lower circles. I need to make the viewport as a barrier / container to hold/prevent the falling circles from leaving the screen. If you make visible collision on you can see the problem. Can you please look into the project and tell me where I have done wrong ?
THE HELIX | 2019-08-03 18:06
I’m unauthorised to download it apparently. What are the inspector settings on both circle and the staticbodies?
Magso | 2019-08-03 19:35
Here is the project New Project . I uploaded it from another website.The inspector settings on both circle and the staticbodies are in default values.I haven’t changed anything.You can see in the project.
THE HELIX | 2019-08-04 14:16
I didn’t get any collision problems, nor did any circle get cleared.
Magso | 2019-08-04 23:40
Here is the project Project . Now if you press the numbers they are cleared.If you change velocity. x = 0 and velocity. y = 0 in the circle script the circle do not come down on clearing lower circles.If you change velocity. x = 1 and velocity. y = 1 the circles go through the static body.You can see it by not clicking on the circles and click on them when more circles fall on them. When you put velocity = 1 the circles merge with each other and circles on bottom fall of when circles above them increase.You can see the circles passing through static body if you make visible collision on , Press on the circles after few moments of the circles falling down