【问题标题】:Why the objects are moving down direction and not up?为什么物体向下移动而不是向上移动?
【发布时间】:2018-11-17 21:31:46
【问题描述】:

似乎向下或方向向后移动而不是向前移动。 我试着做 - 50 而不是 + 50

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

我也尝试在开头添加减号:

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

但到目前为止没有任何效果。物体移动缓慢而平稳,但我无法控制方向。

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);
            }
        }
    }
}

【问题讨论】:

  • 您可以分享更多关于您正在制作的东西的信息吗?否则,您所做的移动仅在 X 轴上,您必须在 2D 环境中操纵 y 值,在 3D 环境中操纵 z 值。
  • 你为什么在Y的位置上写objectstoMove[i].transform.position.x? => "new Vector3(0, objectstoMove[i].transform.position.x + 50, 0)"

标签: c# unity3d


【解决方案1】:

您将x 值放入Vector3(x,y,z)y 值中

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

应该是的

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

【讨论】:

    【解决方案2】:

    这个:

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

    创建一个只分配了 Y 组件的新向量。例如,如果你想让你的对象向右移动,你应该这样做:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-05
      • 1970-01-01
      • 2014-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多