Sign in to follow this  
syed samoon

Clamp Player movement

Recommended Posts


 i need a help. i want to clamp the player movement in certain range. how can i fix it.

now i'm using following method

the moveleft & moveright is a boolean function.

it is working fine. but if the character repul while colliding with collider. then the character move across the given limit.

how to clamp it. 

if (player.moveRight && player.position.x <= 42) {
player.position.x += 1;
if (player.moveLeft && player.position.x >= -42) {
player.position.x -= 1;

Share this post

Link to post
Share on other sites

It's hard to understant your code without any live example, but I'll take a shot :)

Can you try something like this ? 

if (player.moveRight && player.position.x <= 42) {
player.position.x += 1;

if (player.moveLeft && player.position.x >= -42) {
player.position.x -= 1;

if (player.position.x >= 42) {
  player.position.x = 42;

if (player.position.x <= -42) {
  player.position.x = -42;


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.