3

I am trying to run a simple script to get an object to move forward within unity.

My code is:

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

As far as I can see, the transform.position should work.

Any ideas?

4

2 回答 2

3

Try changing :

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

to :

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

Sometimes with unity2D the forward axis is the Z so you're pushing it inside the Z axis which you won't see. Right will move it on the x axis.

于 2017-03-13T23:11:25.317 回答
2

I think you need to apply your position to the RigidBody object rather than the aircraft. If I'm guessing right, that should be your aircraft's parent. Try:

Aircraft.parent.transform.position += transform.forward * moveSpeed * Time.deltaTime;
于 2017-03-13T23:34:28.200 回答