Constraining movement to the camera
MonjiO
Member Posts: 20
Hello all, MonjiO again with another (hopefully easily solved) problem: how can I constrain the payer's movement to the camera in a side-scrolling game?
Right now (working off of the shmup template) I have the Camera Follows Me Behavior creating the scrolling level effect. I've also made an actor using the How Do I Create A Wall tutorial with three bhaviors: Collision with the player and Constrain Attributes set to the game.DisplaySize settings. I have also made top and bottom boundaries that collide with the player.
The effects of all of these are that the player is constrained in his top, bottom, and left movement but can still move past the display if moving right. Is there some piece of logic I'm missing or a behavior that could be better used than the setup I have now? The goal, of course, is to make it so that the player can't move out of the bounds of the camera.
Right now (working off of the shmup template) I have the Camera Follows Me Behavior creating the scrolling level effect. I've also made an actor using the How Do I Create A Wall tutorial with three bhaviors: Collision with the player and Constrain Attributes set to the game.DisplaySize settings. I have also made top and bottom boundaries that collide with the player.
The effects of all of these are that the player is constrained in his top, bottom, and left movement but can still move past the display if moving right. Is there some piece of logic I'm missing or a behavior that could be better used than the setup I have now? The goal, of course, is to make it so that the player can't move out of the bounds of the camera.
Comments
I have tried the Control Camera action on the player, and while it does strictly speaking accomplish the goal it also leads to a whole lot of terrible-looking scrolling that would, I'm assuming, require more work to fix than if I were able to create a moving wall around the player.
Correct me if I'm wrong, but if I've got an object that has the dimensions of the display and the player collides with it, shouldn't that be true of all 4 sides of the display? Basically I don't understand why the right-side collision isn't working.
I want the player to be unable to move past the display area, as in the size of the viewing screen or dimensions of the camera (X and Y values). I have another object controlling the camera to make the level scroll, and I DO NOT want to give the player the Control Camera behavior. The level, in this case, has a greater X amount than the game's display size.
My resolution has been in two stages so far. The first was to create actors at the top and bottom of the level, with the Collide behavior. These actors prevent the player's Y value from exceeding the display area.
The next step was to limit the player's possible X values. To do this, I made an actor with the behavior Constrain Attribute, setting the actors X and Y values equal to the game's display size. I then gave this actor the Collide behavior with the player.
The culmination of these actors is that the player cannot move outisde the display on all sides except the right side of the display.
With the camera origin now available as game attributes, you could check the position of your player against that with 4 rules.
If Attribute self.Position.X is < game.CamOriginX,
-- Constrain Attribute: self.Position.X to game.CamOriginX
If Attribute self.Position.X is > game.CamOriginX+480,
-- Constrain Attribute: self.Position.X to (game.CamOriginX+480)
If Attribute self.Position.Y is < game.CamOriginY,
-- Constrain Attribute: self.Position.Y to game.CamOriginY
If Attribute self.Position.Y is > game.CamOriginY+320,
-- Constrain Attribute: self.Position.Y to (game.CamOriginY+320)