AI Actor to change direction when hitting wall

GugsGamesGugsGames Member Posts: 54
edited November -1 in Working with GS (Mac)
In my game I have AI actors that spawn in and move in one direction only. I'm trying to get it so that when they hit the side of a wall, they will go back in the opposite direction to the one they were moving in. But only when they hit the side of it, not the top or bottom.

However, I'm not sure how to detect whether the actor has hit the 'side' of the wall in order for me to tell it to change direction at that moment.

Is there a way to do this?

Comments

  • MattGibbardMattGibbard Member Posts: 45
    I couldn't help you with detecting a side collision but I can help you with movement reversal.

    You could create an interger attribute for the actor called "MovementDirection". Then under the actor, create a couple of rules:
    ---
    Change Attribute: "MovementDirection" to Rand(0,360)
    ---
    Actor Move: Angle to "MovementDirection"
    ---
    On Collision with 'Wall': If "MovementDirection > 180 Then
    (MovementDirection = MovementDirection-180)
    Else
    (MovementDirection = MovementDirection+180)
    ---

    I'll explain a little. Let's say an actor is moving towards angle '90' which is directly up in GameSalad. On collision with the wall, it will check to see if the angle is above '180' or not. If it's above, it will minus 180 from the angle; If it's below it would add 180. In this case, the angle is 90, so it would add 180 which gives 270 which is directly down in GameSalad.

    Hope this helps a little.
  • old_kipperold_kipper Member Posts: 1,420
    Use a checking of relative position of the collision using extra conditions in the rule for collision such as position of wall= self position greater than self x and less than self x plus 32 (if your actor was 64 pixels wide).

    Or use individual walls for above/below/left and right. Then you could check for the particular collision with your hero object. In one thing I've been working on I had to do this as I had lots of paths on screen defined by walls and and want actors to set off from collision in a similar way to what I think you are talking about. By having the actors for the walls overlaying each other with a very slight protruding in their own rule direction (a top actor sticking out a couple of pixels from the bottom of a block of walls for example) I found I could get things to work. There might be problems if the hero actor was travelling at speed as it might overlap two different wall type sections, but the way I've set things up in my case this is not a problem.

    hope that helps

    kipper
  • GugsGamesGugsGames Member Posts: 54
    Thanks guys, I will give your advice a try in a bit and let you know how it goes. Much appreciated.
  • GugsGamesGugsGames Member Posts: 54
    Ok, so I tried the kipper method of using individual walls. 'Wall deflectors'. Once the game starts they are spawned in and deflect the actors in the opposite direction using self position. And it works very well! So thanks again for the help.
  • old_kipperold_kipper Member Posts: 1,420
    Glad it worked for you. cheers and good luck.
Sign In or Register to comment.