1
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraMove : MonoBehaviour
{

    public float smoothSpeed = 3f;

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    private void FixedUpdate()
    {
        transform.localPosition = Vector3.MoveTowards(transform.localPosition, new Vector3(0, 500, 0), smoothSpeed * Time.deltaTime);
    }
}

The script is attached to the Main Camera.

I want the camera to move directly up and a little bit also to the back. But the way it is now it's moving up but also to the right. Why it's moving also to the right ? I tried transform.localPosition and also only transform.position

\$\endgroup\$
3
  • \$\begingroup\$ What are your camera coordinates? It's very likely that your camera is just on the "left" side of the coordinates you're telling it to go to. \$\endgroup\$ Oct 4, 2017 at 5:18
  • \$\begingroup\$ I tested your code it is only moving upward, then what is the problem? what do you mean by "up and a littel bit also to the back" \$\endgroup\$ Oct 4, 2017 at 5:30
  • \$\begingroup\$ What's the starting position, is it by any chance to the left of 0,500,0? \$\endgroup\$
    – user253751
    Jun 17, 2018 at 23:15

2 Answers 2

2
\$\begingroup\$

Is your camera rotated to look at a specific angle maybe?

You are moving your camera based on its localPosition which means that it moves in its own object space. If the camera is rotated, it's Y axis is also rotated, so moving along the Y-axis will cause it to move diagonally most likely, depending on its rotation.

If you want to move your camera upwards in world space, replace

transform.localPosition = Vector3.MoveTowards(transform.localPosition, new Vector3(0, 500, 0), smoothSpeed * Time.deltaTime);

with

transform.position = Vector3.MoveTowards(transform.position, new Vector3(0, 500, 0), smoothSpeed * Time.deltaTime);

Now the object will move towards the +Y axis, based on your world.

\$\endgroup\$
0
\$\begingroup\$

The argument to MoveTowards is an absolute position, not a relative position.

You need to figure out where you want to move, then provide it as an argument.

public class CameraMove : MonoBehaviour
{    
    public float smoothSpeed = 3f;
    public bool isInitialized = false;
    public Vector3 moveHere;

    private void FixedUpdate() {
        if (!isInitialized) {
            moveHere = transform.localPosition + new Vector3(0, 500, 0);
            isInitialized = true;
        }
        transform.localPosition = Vector3.MoveTowards(transform.localPosition, moveHere , smoothSpeed * Time.deltaTime);
    }
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You might prefer to put the initialization routine into a Start() method so that you don't need to track an extra flag and check it in every FixedUpdate(). \$\endgroup\$
    – DMGregory
    Feb 17, 2018 at 13:48

You must log in to answer this question.

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