簡體   English   中英

向2D剛體添加速度

[英]Adding Velocity to a 2D rigidbody

我剛剛開始使用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);
}

}

這是您的要求嗎?

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

要么

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM