使用transform.Translate()可以直接对物体进行移动,而且其中还可以设置Space.Self参数,这就是相对于物体自身来运动,这样无论怎么旋转物体,物体都会朝着相对自身而言的z轴前进。
但如果是Space.World的话,那么设置的就是世界坐标,一旦旋转物体后物体的移动就会变得很诡异。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class SimpleLogic : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
GameObject obj = this.gameObject;
obj.transform.Translate(0,0,0.01f,Space.Self);
}
}