[英]Unity2D: How to get object to move forward
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. 据我所知,transform.position应该起作用。
Any ideas? 有任何想法吗?
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. 有时,使用unity2D时,前向轴是Z,因此您将其推入Z轴内而看不到。 Right will move it on the x axis. 向右将在x轴上移动它。
I think you need to apply your position to the RigidBody object rather than the aircraft. 我认为您需要将位置应用于RigidBody对象而不是飞机。 If I'm guessing right, that should be your aircraft's parent. 如果我猜对了,那应该是您飞机的父母。 Try: 尝试:
Aircraft.parent.transform.position += transform.forward * moveSpeed * Time.deltaTime;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.