Search code examples
unity-game-engineunity3d-2dtools

Unity3D 2D Resetting 2D object position


I have the below script attached to an object. When it hits the left or right wall, I want to recenter the object but it doesn't seem to reset the position.

I do see 'hit wall' in the debug window.

function OnTriggerEnter2D (hitInfo : Collider2D)
{
var hitSide : boolean = false;

if (hitInfo.name == "leftWall")
{
    hitSide = true;
}
else if (hitInfo.name == "rightWall")
{
    hitSide = true;
}

if (hitSide)
{
            Debug.Log("Hit wall");
    transform.position.x = Screen.width /2;
    transform.position.y = Screen.height / 2;
}
}

Solution

  • Are you aware of Unity Answers, a forum site similar to this? I am not sure about the behavior of Screen.width / 2. Screen.width is just a count of units of how wide the screen is. Setting it to this position is telling the coordinate system to use half of those units as the x coordinate. Based on your camera's current position and other factors, this is not the desired way to go about doing this. http://answers.unity3d.com/questions/466665/placing-a-gameobject-in-the-exact-center-of-the-ca.html