将速度添加到2D刚体

时间:2014-02-22 18:10:38

标签: c# unity3d 2d

我刚刚开始使用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);
}

}

1 个答案:

答案 0 :(得分:3)

这是你的问题吗?

[yourigidbody] .velocity = transform.forward * speed;

[yourigidbody] .velocity = transform.TransformDirection(Vector3([xspeed],[yspeed],[zspeed]));