How would I go about keeping the player on screen?
I’m new to Unity/C#. In AS3 we would just check if the player’s x was less than the stages x position but with Unity/C# I have no idea how to do this or if it is even what you are suppose to do.
How would I go about keeping the player on screen?
I’m new to Unity/C#. In AS3 we would just check if the player’s x was less than the stages x position but with Unity/C# I have no idea how to do this or if it is even what you are suppose to do.
Are you making a 2D or 3D game?
Is your project in 2D mode or 3D mode?
What is your Scene setup?
Hey thanks for the quick reply. It is a 3D game with an orthographic camera view. The camera is stationary and never moves.
…so 3D game in 2D mode…
No it’s a 3D game in 3D mode, orthographic view has nothing to do with a game being 2D or 3D. When I ask if it is 2D I mean if the Scene has the “2D” option highlighted
I am going to assume it is an isometric game, but problem remains now that you have to deal with 3D.
I am also going to assume you only have 1 player, meaning its not an RTS or RPG with a team of characters, it only has 1 sole player which the camera has to look at.
I am also going to assume your camera never has to rotate.
One general way which worked pretty well as a beginner was to create an empty game object, and make the camera a child of it, and have the empty game object follow the position of the player at run time. The relative position of the camera to this empty game object would be considered the offset.
Sorry I didn’t understand. I’m an endless runner similar to the old arcade game Moon Patrol
https://www.youtube.com/watch?v=39EsNumG3Fc
The camera never moves but the player can move around the environment which is why I need to make it so he can’t leave the screen. Don’t want to have the camera follow the player because the ground the player moves on is instantiating below him and moving screen left where it eventually gets destroyed after moving a certain distance to the left.
You should have explained the kind of game you were making in the first place since different games have different interpretations for this same issue.
Usually an endless runner does the running for you, so the player position relative is rarely an issue(looking at temple-run and subway-surfers).
The player has only vague control over the movement, in temple run its tilting the device to move left and right, in subway-surfers is swiping towards the left or right to go to the adjacent lane, both swipe up to jump and down to dive.
I’d say the same thing has to happen in your game. The player does not actually move. I can’t tell from this if the car in moon-patrol’s movement is based on player control. It does move slightly more left or right, and does slow down and speed up slightly so I guess that is player controlled, however, the environment doesn’t seem ever stop which leads me to believe there is some constant movement that is beyond the player’s control.
The car moving more left or more right has bounds to it, and it never leaves those bounds. this is quite easily controlled by scripting such that the player cannot move beyond 2 certain X positions defined by you.
The speeding up and slowing down would the rate at which the environment moves.
My game is similar to Moon Patrol but it’s not exactly like it. Different types of ground are instantiated and move beneath the player like an endless runner but the player can also control themselves and move left and right (in my game) which is why I need to prevent them from exiting the camera view.
I’ve tried this:
pos = transform.position;
if (transform.position.x < screenLeft)
{
pos.x = screenLeft;
}
else if (transform.position.x > screenRight)
{
pos.x = screenRight;
}
(that code is in the update function)
but it doesn’t seem to work…
for your current script, you are missing this line after checking the bounds
transform.position = pos;
One option is that you could make the environment move only when the player is at the edge of either of these bounds if you want the movement to be completely player controlled, meaning the player can stop completely.
As for the nature of your game, an endless runner usually it is the environment that is moving, not the player. Rumor has it that weird stuff can potentially happen when things get too far away from the origin point due to the nature of floating point numbers which is why endless runners do not make a character run in an endless environment.
Typically an endless runner only goes forward never allowing the player to backtrack since the gameplay doesn’t allow it, and it would break the impression that the environment is endless.
Backtracking would not be too big of a problem if your levels are defined by you, but if it is completely random generated, it may get weird when the player moves forward, then back, then forward again, or vice versa, since the environment is completely different depending on the time the player entered the new environment.