我刚刚开始使用Unity,我决定制作一个2D小行星克隆,到目前为止,我有旋转和移动,但我似乎无法弄清楚如何使船保存其速度(所以你继续移动直到你向相反方向施加相同的力量)......这就是我到目前为止所拥有的......
using UnityEngine;
using System.Collections;
public class movement : MonoBehaviour {
void Update () {
GameObject facer = GameObject.Find("facer");
//Facer is another object I am using as a sort of aiming reticle
if(Input.GetKey(KeyCode.UpArrow))
{
this.transform.position = Vector3.MoveTowards(this.transform.position, facer.transform.position, 0.1f);
}
if(Input.GetKey(KeyCode.DownArrow))
{
this.transform.position = Vector3.MoveTowards(this.transform.position, facer.transform.position, -0.1f);
}
if(Input.GetKey(KeyCode.LeftArrow))
{
RotateLeft();
}
if(Input.GetKey(KeyCode.RightArrow))
{
RotateRight();
}
}
void RotateLeft() {
transform.Rotate (Vector3.back * -5f);
}
void RotateRight() {
transform.Rotate (Vector3.forward * -5f);
}
}
答案 0 :(得分:3)
这是你的问题吗?
[yourigidbody] .velocity = transform.forward * speed;
或
[yourigidbody] .velocity = transform.TransformDirection(Vector3([xspeed],[yspeed],[zspeed]));