Hello all. I have been having a problem when moving my player along a wall in a top-down RPG style. I’ve been using Unity’s tile map system. When moving along the wall, it works perfectly find except for the fact that the player slows down slightly and the position of the player shows that it cannot actually get all the way against the wall (as shown in video). My physics settings are the default, except I have everything running in fixed update. As well as that, I have a default physics2D material with not friction or bounciness that is applied to everything. My player has continuous collision detection, with interpolate set to interpolate. I’ve tried every solution that I could find, which is the only reason I’m looking for help now.
public IEnumerator Move(Vector3 moveVec, Action OnMoveOver = null)
{
if (PlayerMovement.MyInstance.currentState != PlayerState.paused)
{
if (gameObject.GetComponent<CharacterAnimator>() != null)
{
animator.MoveX = Mathf.Clamp(moveVec.x, -1f, 1f);
animator.MoveY = Mathf.Clamp(moveVec.y, -1f, 1f);
}
var targetPos = transform.position;
targetPos.x += moveVec.x;
targetPos.y += moveVec.y;
if (!IsPathClear(targetPos))
{
if (gameObject.CompareTag("NPC"))
{
PlayerMovement.MyInstance.canWalk = true;
}
yield break;
}
if (!IsTileClear(targetPos))
{
if (gameObject.CompareTag("NPC"))
{
PlayerMovement.MyInstance.canWalk = true;
}
yield break;
}
if (CompareTag("Player"))
{
if (PlayerMovement.MyInstance.currentState != PlayerState.stagger)
{
while ((targetPos - transform.position).sqrMagnitude > Mathf.Epsilon && GetComponent<PlayerMovement>().currentState != PlayerState.stagger)
{
IsMoving = true;
transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
yield return null;
}
SetPositionAndSnapToTile(targetPos);
IsMoving = false;
OnMoveOver?.Invoke();
}
}
else if (CompareTag("NPC"))
{
while ((targetPos - transform.position).sqrMagnitude > Mathf.Epsilon)
{
if (IsTileClear(targetPos))
{
IsMoving = true;
transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
}
else
{
IsMoving = false;
}
yield return null;
}
SetPositionAndSnapToTile(targetPos);
IsMoving = false;
if (GetComponent<NPCController>() != null && GetComponent<NPCController>().directionAfterMove != null)
{
FaceDirection(GetComponent<NPCController>().directionAfterMove);
}
OnMoveOver?.Invoke();
}
}
}