簡體   English   中英

為什么我的 c# 代碼使左右移動比前進和后退快得多

[英]Why is my c# code making going left and right much faster than forward and backwards

出於某種原因,我正在嘗試編寫一些自定義控件,並且出於某種原因,左右移動比前進和后退快 100 倍。 我一直在努力修復它。 我變得非常生氣和疲倦試圖修復它,所以請幫助我。

    void Start()
    {
        xVelocity = 0f;
        yVelocity = 0f;
        zVelocity = 0f;
    }

    void Update()
    {
        yVelocity--;
        Control();
        transform.position += transform.up * Time.deltaTime * yVelocity;
        transform.position += transform.forward * Time.deltaTime * zVelocity;
        transform.position += transform.TransformVector(-90, 0, 0) * Time.deltaTime * xVelocity;
    }

    private void Control()
    {
        if (Input.GetKey(KeyCode.W))
        {
            zVelocity += speed;
        }
        if (Input.GetKey(KeyCode.S))
        {
            zVelocity += -speed;
        }
        if (Input.GetKey(KeyCode.A))
        {
            xVelocity += speed;
        }
        if (Input.GetKey(KeyCode.D))
        {
            xVelocity += -speed;
        }
        if (Input.GetKey(KeyCode.W) == false && Input.GetKey(KeyCode.S) == false)
        {

            if (zVelocity == 0 == false)
            {
                if (zVelocity < 0)
                {
                    zVelocity += friction;
                }
                else
                {
                    zVelocity -= friction;
                }
            }
        }
        if (Input.GetKey(KeyCode.A) == false && Input.GetKey(KeyCode.D) == false)
        {
            if (xVelocity == 0 == false)
            {
                if (xVelocity < 0)
                {
                    xVelocity += friction;
                }
                else
                {
                    xVelocity -= friction;
                }
            }

        }

出於某種原因,我正在嘗試編寫一些自定義控件,並且出於某種原因,左右移動比前進和后退快 100 倍

如果沒有完整的代碼示例,就無法確定問題所在。 但…

我敢打賭,這不是 100X,而是 90X。

upforward向量是單位向量,即幅度為 1。但是您正在使用幅度為 90 的顯式構造的向量( transform.TransformVector(-90, 0, 0) )沿 X 方向轉換 position。 所以你的左/右運動是前/后和上/下幅度的 90 倍。

您應該能夠將代碼更改為transform.TransformVector(-1, 0, 0) (單位向量)並獲得您想要的行為。

暫無
暫無

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

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