簡體   English   中英

固定運動方向(3d 等距游戲)

[英]Fixing the movement direction (3d isometric game)

我是游戲開發的初學者,我目前對角色的移動方向有疑問。 游戲在 3D 等軸測視圖中,相機設置為 X = 30 和 Y = 45,投影設置為正交。 問題是向前方向,當角色向前移動時(當我按 W 時),因為它處於等距視圖中,所以角色向西北方向移動 45 度或向左斜向移動。 這主要是因為相機是為等距視圖傾斜的。

我想要做的是當我向前移動時,角色會在屏幕上而不是對角線向上移動。 我想過改變整個資產的輪換來改變前進的方向,但這需要時間。 我也想過通過代碼改變運動方向,改了加了一些東西,但就是把玩家的運動搞亂了。

這是播放器移動的腳本(這是我嘗試失敗之前的代碼):

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

    public float speed, rotationSpeed;
    public Joystick joystick;

    // Update is called once per frame
    void FixedUpdate()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        Vector3 movementDirection = new Vector3(horizontalInput, 0, verticalInput);
        movementDirection.Normalize();

        transform.Translate(movementDirection * speed * Time.deltaTime, Space.World);

        if (movementDirection != Vector3.zero)
        {
            Quaternion toRotation = Quaternion.LookRotation(movementDirection, Vector3.up);
            transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
        }
    }
}

您可以通過對其應用旋轉來糾正您的movementDirection方向。

就像是:

movementDirection = Quaternion.Euler(0,45f,0) * movementDirection;//rotates by 45° on y axis

在您進行翻譯和定位之前。

暫無
暫無

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

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