Yoohao
Yoohao

Reputation: 39

How to move an gameobject in Unity properly?

I want to move an Text object, and the part of code is as follows.

GameObject.transform.position = new Vector3(-210, -200, 0);

When I execute and check the posX of GameObject in Unity, its value becomes -1170(in 1920x1080), -1653.566(16:9). But posY can work properly. I've set the reference convolution to 1920x1080, and I think it may it have something to do with the resolution settings. Is there any thing wrong? Thanks.

Upvotes: 2

Views: 3964

Answers (3)

Farhan
Farhan

Reputation: 1278

In Unity, the transform values you see in the inspector are relative to the gameobject's parent. However, when you try to set value for a gameobject's position (by assigning transform.position = ...), you are dealing with position relative to world's center (i.e Vector(0,0,0)). This holds true for whether you are dealing with 3d or 2d.

So, if the parent object is at Vector(0,0,0), world and local positions would be same. There isn't anything wrong with the resolution. You need to set values relative to your parent.

This is how you set values for objects.

    anObject.transform.localPosition = new Vector3 (X, Y, Z);

Although there is nothing stopping you from using the same for 2d workflow, RectTransforms are used over simple Transform.

Upvotes: 0

Mukesh Saini
Mukesh Saini

Reputation: 1498

All the UI objects(text, image etc.) are parented by canvas object in unity. Canvas behaves differently based on it's screen space setting as follows -

  1. Screen Space - Overlay : If the screen is resized or changes resolution, the Canvas will automatically change size to match this.

  2. Screen Space - Camera : If the screen is resized, changes resolution, or the camera frustum changes, the Canvas will automatically change size to match as well.

  3. Screen Space - World : The Canvas will behave as any other object in the scene. The size of the Canvas can be set manually using its Rect Transform.

The default setting is Screen Space - Overlay Which is the reason you are getting different position values for your text object on different resolutions.

The unity UI elements uses RectTransform. From unity docs

The Rect Transform component is the 2D layout counterpart of the Transform component. Where Transform represents a single point, Rect Transform represent a rectangle that a UI element can be placed inside. If the parent of a Rect Transform is also a Rect Transform, the child Rect Transform can also specify how it should be positioned and sized relative to the parent rectangle.

So, to set position of UI elements use RectTransform's anchoredPosition variable, which sets the position of the pivot of this RectTransform relative to the anchor reference point.

textObject.rectTransform.anchoredPosition = new Vector3 (-10, -10, 0);

Reference to rect transform script API.

Upvotes: 1

Ognjen Marceta
Ognjen Marceta

Reputation: 151

If you are talking about unity ui text you should do it like this.

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

public class UITestSO : MonoBehaviour 
{

    public Text textObject;
    void Start () 
    {

        //Position relative to parent transform
        textObject.rectTransform.localPosition = new Vector3 (-210, -200, 0);

        //Position in world space
        textObject.rectTransform.position = new Vector3 (-210, -200, 0);
    }
}

Upvotes: 4

Related Questions