If you run through your Step code, as if you were explaining it to someone, you will probably spot your problem much easier,
Add the dirspeed to the direction
Then check every point on the line for a collision.
If there is a collition, set hit to true, otherwise, let it stay at false.
If there was no collision, reverse the dirspeed.
As you may notice, you are adding the dirspeed before doing collision checks, so the code is checking the position that the laser just moved into for collisions, and then taking action.Rather than checking if it should reverse before moving to the new position. There is a very simple way to remedy this.
When you check if there was a collision, at the end of the code, move the direction back to a place you know had a collision, from the checks that were run last step:
direction += dirspeed; //Add to direction
var hit=false; //Set a default hit=false
for(i=0;i<=dist;i+=1) //Check every point on the line (dist=200 in this case)
{
xx = x + lengthdir_x(i,direction);
yy = y + lengthdir_y(i,direction);
if (collision_point(xx,yy,obj_box_parent,0,1)) //Check for collision
{
hit=true; //If we find a hit......
break;
}
}
if(hit==false)
{
dirspeed=-dirspeed; //.....we change direction
direction+=dirspeed; // Jump back to a direction that did have a collision
}
Your welcome, Vlad ![]()



Find content
Male


