# walking up/down slowed by z coordinate layering

I am making a 2D topdown sprite game. I got myself a controller script that handles input and makes the character move/attack etc. I eventually added objects you could walk in front of or behind, like trees, houses, and flowers, so naturally I needed a script to handle that. This answer gave me two solutions one using sprite renderer sorting layers and the other by altering the z-coordinate of objects. The former solution worked but was finicky, and the last one worked just fine.

However, now I am noticing a bug. I have two scenes and if you die, you respawn to the first scene. The bug only occurs if I die and respawn or if I go all the way back to scene 1 e.g. scene 1 > scene 2 > scene 1 (I have pathways that lead between scenes with triggers). The bug is that walking up or down is significantly slower than walking right or left and if I uncheck my Z-Layer script on the player, the speed goes back to match the left/right movement.

This bug doesn't happen in any other scenario, if I start the game in either scene it is fine, but whenever I respawn or go all the way back to scene 1, the up/down movement is slowed. It seems to me the extra work of changing the Z-Coordinate is causing the slowness but it is odd that it only occurs on looping back to scene 1, or respawning (which puts you back to scene 1, as well). I am not creating additional players each time he dies, or saving any data between loads yet.

Is there any idea on what could fix this or what is causing this without having to go deep into all my scripts?

EDIT Here is my Z-Layer script. It is attached to my Player.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ZLayer : MonoBehaviour {

//sets the Z coordinate of a sprite to its Y. if the camera settings are reversed, youll need to multiply by -1,
float initial;
Vector3 temp;

void Awake () {

SetPosition();
}
//note since Update is called every frame, you must disable the scripts that on static objects
//e.g. trees, walls etc, (disabled scripts dont get their updates called
//for moving objects keep it ticked. (note this is why we used start and not awake too.
void Update()
{
SetPosition ();
}

void SetPosition () {
temp = transform.position;
temp.z = transform.position.y;
transform.position = temp;

}
}


For reference, here is the code that moves my player

void Move(float h, float v)
{
movement.Set (h, v);
//normalize movement vector and make it proportional to speed per second

movement = movement.normalized *speed*Time.deltaTime;

//move the player to the current position

temp = new Vector2 (transform.position.x, transform.position.y);
temp2 = temp + movement;
playerRigid.MovePosition (temp2);

}


Move(h,v) is called in Update() if a boolean walking is true, and h and v are the horizontal and vertical inputs obtained from Input.GetAxisRaw.

A key part I want to reiterate is that it works fine on start, it only slows up/down movement when I respawn or get back to scene 1 from scene 2. Doing one scene transition seems to work fine, but 2>1>2 or 1>2>1 bugs out.

• Since you've narrowed it down to the Z-Layer script, post that script so we have something to dig through. It not only allows us to compare against the post you linked, but helps to prevent link rot should the question you mention be removed at some point in the future. – Stephan Apr 3 '18 at 19:40
• @Stephan thanks for the reply. I've updated the question to include my Z-layer script and the relevant movement code. Good point re potential broken links in the future too. – Nap D. Lover Apr 3 '18 at 20:00