Hello,
I’m making a game using the 2D Beat Em Up Template by Osarion with a team. We’ve customized a few things so far but we wanted to use the new Unity Input System since it will be easier for our combat design to use that.
The problem is, the movement ended up resulting in strange behavior after this. The player character now can walk off of the screen while holding a movement key, and no longer stops based on the code in the KeepPlayerInCameraView() function, even though when debugging the function, it’s clear that this is purely aesthetic because the players position is not logged to go past the screen edge.
Further, when you stop holding the movement button, the player will snap back to the edge of the screen. Which means the code is working, just not… totally?
Anyway, I’ve spent like 4 hours on this issue and I can’t figure out what’s wrong. Can anyone help?
Code for Input (mine)
public void OnMove(InputValue value)
{
if (playerState.currentState != PLAYERSTATE.KNOCKDOWN && !isDead)
{
v = value.Get<Vector2>();
v = new Vector2(v.x, v.y * .7f);
Move(v * walkSpeed);
}
}
Code For Movement: (came with the package)
public void Move(Vector3 vector) {
if (playerState.currentState != PLAYERSTATE.JUMPING && playerState.currentState != PLAYERSTATE.JUMPKICK) {
//removes any existing y offset
if(isGrounded) GFX.transform.localPosition = Vector3.zero;
//move player
if(rb != null) rb.velocity = vector;
//play walk or idle animation
if (Mathf.Abs (vector.x + vector.y) > 0) {
//calculate current direction
int i = Mathf.Clamp (Mathf.RoundToInt (vector.x), -1, 1); //change direction based on the current movement vector
if(i == 0) i = Mathf.RoundToInt(GFX.transform.localScale.x); //stay in the same direction while going up or down
currentDirection = (DIRECTION)i;
animator.Walk();
} else {
animator.Idle ();
}
LookToDir (currentDirection);
isGrounded = true;
}
KeepPlayerInCameraView();
}
Code for keeping player in camera view: (Came with the package)
public void KeepPlayerInCameraView(){
Vector2 playerPosScreen = Camera.main.WorldToScreenPoint(transform.position);
Debug.Log(playerPosScreen);
if(playerPosScreen.x + screenEdgeHorizontal > Mathf.Abs(Screen.width) && (playerPosScreen.y - screenEdgeVertical < 0)){
transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Mathf.Abs(Screen.width)-screenEdgeHorizontal, screenEdgeVertical, transform.position.z - Camera.main.transform.position.z));
} else if(playerPosScreen.x + screenEdgeHorizontal > Mathf.Abs(Screen.width))
{
transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Mathf.Abs(Screen.width)-screenEdgeHorizontal, playerPosScreen.y, transform.position.z - Camera.main.transform.position.z));
} else if(playerPosScreen.x - screenEdgeHorizontal < 0f && (playerPosScreen.y - screenEdgeVertical < 0)){
transform.position = Camera.main.ScreenToWorldPoint(new Vector3(screenEdgeHorizontal, screenEdgeVertical, transform.position.z - Camera.main.transform.position.z));
} else if(playerPosScreen.x - screenEdgeHorizontal < 0f){
transform.position = Camera.main.ScreenToWorldPoint( new Vector3(screenEdgeHorizontal, playerPosScreen.y, transform.position.z - Camera.main.transform.position.z));
} else if((playerPosScreen.y - screenEdgeVertical < 0) && (playerPosScreen.x + screenEdgeHorizontal > Mathf.Abs(Screen.width))){
transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width-screenEdgeHorizontal, screenEdgeVertical, transform.position.z - Camera.main.transform.position.z));
} else if((playerPosScreen.y - screenEdgeVertical < 0) && (playerPosScreen.x - screenEdgeHorizontal < 0f)){
transform.position = Camera.main.ScreenToWorldPoint(new Vector3(screenEdgeHorizontal, screenEdgeVertical, transform.position.z - Camera.main.transform.position.z));
} else if(playerPosScreen.y - screenEdgeVertical < 0){
transform.position = Camera.main.ScreenToWorldPoint(new Vector3(playerPosScreen.x, screenEdgeVertical, transform.position.z - Camera.main.transform.position.z));
}
}