0
\$\begingroup\$

I'm attempting to set the camera position in Unity through code. I have a simple 2D game and there's not much programming so far. I've attached a script to the camera, and in that script I've added this function:

  void Update()
    {
    Transform transform = gameObject.GetComponent<Transform>();
    Vector3 position = transform.position;
    transform.position.Set(position.x + 1, position.y + 1, position.z);
    }

My expectation is that this would create a smooth scrolling toward the bottom right corner. However, after checking via breakpoints, the transform.position always reports (0, 0, -10), even after setting it. The camera object is a stand alone and not a child of anything else in the scene. Am I setting the position wrong or getting the camera's GameObject wrong?

I've also tried:

  • transform.SetPosition(); - doesn't exist
  • transform.SetPositionAndRotation(); - no effect
  • transform.position.x = 0; - not allowed
  • transform.position = new Vector3(position.x + 1, position.y + 1, position.z) - no effect
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Have you tried not to create a local variable called transform but use transform directly? \$\endgroup\$
    – Zibelas
    Commented Jan 17 at 14:39

2 Answers 2

4
\$\begingroup\$

There are several misconceptions here.

First off, every Component and GameObject already has a pre-cached transform, property, so you never have to write GetComponent<Transform>(). Deleting this line changes nothing, since all it does is shadow the existing transform property that gives the same value:

Transform transform = gameObject.GetComponent<Transform>();

Next, transform.position is a getter/setter property. When you read or write to it, you're really calling a function. The getter you call on a read...

  1. Takes the transform's internally stored local position...

  2. Gets the corresponding world position by transforming it by any transformations higher up in the hierarchy...

  3. Returns to you a copy of that value.

Calling position.Set() on a copy of the data does not make the change propagate back to the original transform: it has no idea you changed anything, because you didn't ask it to change.

Thirdly, you never want to add a constant offset to a position in Update(), because that function is called at a variable framerate. To get smooth motion, you should scale the movement by the amount of time elapsed since the previous Update() frame.

Lastly, moving +1 in y is moving up, not down, in Unity's coordinate system. If you want to move down, decrease y.

Putting these together:

public float movementSpeed = 1f;

void Update() {
    Vector3 position = transform.position;
    Vector3 shift = new Vector3(1, -1, 0) * movementSpeed * Time.deltaTime;
    transform.position = position + shift;

    // Or, as another answer notes,
    // transform.Translate(shift, Space.World)
    // accomplishes the same thing.
}
\$\endgroup\$
0
\$\begingroup\$

Try: transform.Translate(movement_dir) where movement_dir is a vector you can define outside of Update if it's going to be a constant.

However you might want to multiply movement_dir by Time.deltaTime, otherwise it might not be as smooth as you'd like due to variations in FPS

see this

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .