簡體   English   中英

為什么這段代碼以如此奇怪的方式移動我的游戲對象?

[英]Why this code moving my gameobject in such an odd way?

我的代碼

        Vector2 moveDirection;
        moveDirection = (Sensitive.transform.position - gameObject.transform.position).normalized;
        float deg = Mathf.Rad2Deg * Mathf.Atan(moveDirection.x / -moveDirection.y);
        if (moveDirection.y < 0) deg -= 180;
        Vector3 to = new Vector3(0, 0, deg);
        transform.eulerAngles = to;
        transform.Translate(new Vector3(moveDirection.x, moveDirection.y) * speed * Time.deltaTime);

在更新 function 中,旨在查看並移動到敏感,盡管它正確指向並沒有正確移動,我可以找出原因。

transform.Translate默認將輸入解釋為局部方向和距離。 您正在提供世界方向和距離的輸入,因此您應該使用可選的第二個參數並指定Space.World

transform.Translate(new Vector3(moveDirection.x, moveDirection.y) 
    * (speed * Time.deltaTime), Space.World);

暫無
暫無

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

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