Daniel Lip
Daniel Lip

Reputation: 11317

Why the objects are moving down direction and not up?

It seems like down or the direction is moving backward instead to move forward. I tried to make - 50 instead + 50

objectstoMove[i].transform.position.x - 50

I also tried to add minus in the start:

-objectstoMove[i].transform.position.x + 50

But nothing worked so far. The objects are moving slow and smooth but I can't control the direction.

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

public class MoveObjects : MonoBehaviour
{
    public float speed = 3f;

    private GameObject[] objectstoMove;

    // Use this for initialization
    public void Init()
    {
        objectstoMove = GameObject.FindGameObjectsWithTag("Objecttomove");
    }

    // Update is called once per frame
    void Update()
    {
        if (objectstoMove != null)
        {
            float step = speed * Time.deltaTime;
            for (int i = 0; i < objectstoMove.Length; i++)
            {
                objectstoMove[i].transform.position = Vector3.MoveTowards(objectstoMove[i].transform.position, new Vector3(0, objectstoMove[i].transform.position.x + 50, 0), step);
            }
        }
    }
}

Upvotes: 0

Views: 90

Answers (2)

Arshia001
Arshia001

Reputation: 1872

This:

new Vector3(0, objectstoMove[i].transform.position.x + 50, 0)

creates a new vector with only the Y component assigned. If you want your object to move right, for example, you should do it like this:

myObject.position = myObject.position + Vector3.right * speed * Time.deltaTime;

Upvotes: 1

MatteoCracco97
MatteoCracco97

Reputation: 416

You put the x value in the y value of Vector3(x,y,z)

new Vector3(0, objectstoMove[i].transform.position.x + 50, 0), step);

It has to be

new Vector3(objectstoMove[i].transform.position.x + 50, 0, 0), step);

Upvotes: 3

Related Questions