簡體   English   中英

如何使游戲對象朝其所面對的方向“向前”移動?

[英]How can I make a gameobject move 'forward' in the direction it's facing?

所以我有兩個圈子。 白色的應該在旋轉方向上從彩色的跳躍。 它有時可以工作,但有時會跳錯角度或幾乎不移動。 這是我的代碼:

void Update () {       
    if (Input.GetKeyDown(KeyCode.Space)) {
        jumpDirection = transform.parent.rotation * Vector3.up;
        transform.parent = null;    
        go1.AddComponent();        
        go2.AddComponent();     
    }
    if(transform.parent == null) {    
        Vector3 rrotation = jumpDirection;      
        transform.Translate(rrotation * Time.deltaTime, Space.World);    
    }    
}

void OnCollisionEnter2D(Collision2D col) {       
    if (col.collider.tag == "sun") {    
        Destroy(gameObject);    
    }    
    if(col.collider.tag == "border") {    
        Destroy(gameObject);    
    }    
    transform.SetParent(col.gameObject.transform);    
    transform.rotation = transform.parent.rotation;    
}

我正在移動設備上,如果格式有些問題,請您諒解。 有誰知道如何解決這一問題?

無需通過transform.rotation旋轉Vector3.up / Vector3.forward,transform類提供了為您計算正確矢量的吸氣劑

Vector3 forward=transform.forward;

向您提供此對象,使用transform.parent.forward獲取父標題

暫無
暫無

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

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