Limit Player Movement in Unity

In this example we define the limits where the player can move in the game, in the image below you can see the player moving freely in the scene with any restriction:

We gonna create two different limits, the player only will be able to move between two values in the Y axis and for the horizontal movement if the player get out of the screen he gonna appear in the other side:

Let’s start with the vertical movement, for this I gonna use an if statement to check if the position in the Y axis is out of the limits and keep the same position in case the condition is true:

Ok now we check when the player reach one of the limits in the X axis we set multiply the X axis by -1 to make the player appear in the other side:

And that it’s, you have defined limits for your player movement:

--

--

--

A Software Engineer passionate about game dev and interactive products with Unity. I consider video games to be the artistic expression of programming.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

IOT -Embedded Systems Processors

Research on use cases of AWS SQS

A look into RealTime, WebSockets, and Warp Cable

Building A Bridge Hand Generator With Python Lesson 2

Young university student carrying text books to class

Welcome to HackGuild!

Illustration by Sara Georgas

How to calculate the cost of CI/CD on top of Azure VSTS

$OCEANS Listed on CG and CMC

Make your own CLI!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rusben Guzman

Rusben Guzman

A Software Engineer passionate about game dev and interactive products with Unity. I consider video games to be the artistic expression of programming.

More from Medium

Adding a player ammo indicator to the UI

Add a custom template to Unity for new projects

Using the Unity Animation System (Game Dev Day 59)

Creating a HUD (part 2)