I want after collide player with wall, unity set Player.y position to Wall.y position. but when player collide with wall, unity set player.y position to more than higher than Wall.y.
I attached this script to my player for set player.y equal to wall.y:
if (this.transform.position.y <= GameObject.FindWithTag ("Wall").transform.position.y || this.transform.position.y >= GameObject.FindWithTag ("Wall").transform.position.y)
{
this.transform.position = new Vector2 (this.transform.position.x, GameObject.FindWithTag ("Wall").transform.position.y);
}
It either might be that it finds the wrong wall, if there are more than one in the scene, or the issue has to do with your setup of the game models. Ensure your player-model’s and the wall’s pivot points are set correctly (you can also use a parent and place the models relative to that in order to “correct” the model’s position).
Other than that, avoid repetitive calls to GameObject.FindXXX. If that’s the way you want to get the reference to an object, use it once and cache the result.
Also, the conditions in the if statement are superflouos, as a <= b || a >= b will naturally always be true. It covers all cases, so you could actually delete that completely.