Unity2D:如何让物体向前移动

Unity2D: How to get object to move forward

我正在尝试 运行 一个简单的脚本来让对象在统一中向前移动。

我的代码是:

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

public class MoveToHold : MonoBehaviour {

    private float traveledDistance;
    public int moveSpeed = 10;
    private bool isMoving;
    public GameObject Aircraft;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        if (isMoving == true)
        {
            //Aircraft.transform.position += transform.forward * Time.deltaTime * moveSpeed;
            Aircraft.transform.position += transform.forward * moveSpeed * Time.deltaTime; 
        }

    }

    public void move ()
    {
        isMoving = true;
        Debug.Log(isMoving);
    }
}

据我所知,transform.position 应该可以。

有什么想法吗?

尝试更改:

Aircraft.transform.position += transform.forward * moveSpeed * Time.deltaTime;

至:

Aircraft.transform.position += transform.right * moveSpeed * Time.deltaTime;

有时使用 unity2D 时,前向轴是 Z,因此您将其推入您看不到的 Z 轴内。向右将在 x 轴上移动它。

我认为您需要将您的位置应用到 RigidBody 对象而不是飞机。如果我没猜错,那应该是你飞机的父级。尝试:

Aircraft.parent.transform.position += transform.forward * moveSpeed * Time.deltaTime;