Jump to content

Clamp Player movement


syed samoon
 Share

Recommended Posts

hi,

 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;
};
Link to comment
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;
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...