我正在为我的 3D 游戏制作 2D 地图,但不是迷你地图。无论我的游戏环境大小如何制作二维地图?

问题描述 投票:0回答:1

我正在制作一款游戏,我需要一张显示玩家位置的地图和一些其他图标。无论我的游戏环境大小如何,我都想缩放 2d 地图。如何以玩家图标的形式在二维地图中准确显示玩家的位置。我做了一个这样的脚本,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Finalmap : MonoBehaviour
{
    public Transform mapIcon;

    private Vector2 mapOffsetInUnits;
    private float mapScaleFactor;

    public GameObject mapPlane1;
    public GameObject mapPlane2;
    public GameObject player;

    void Start()
    {
        Renderer mapRenderer1 = mapPlane1.GetComponent<Renderer>();
        Vector3 mapSize1 = mapRenderer1.bounds.size;
        float mapWidth1 = mapSize1.x;
        float mapHeight1 = mapSize1.z;

        Renderer mapRenderer2 = mapPlane2.GetComponent<Renderer>();
        Vector3 mapSize2 = mapRenderer2.bounds.size;
        float mapWidth2 = mapSize2.x;
        float mapHeight2 = mapSize2.z;

        mapScaleFactor = Mathf.Min(mapWidth2 / mapWidth1, mapHeight2 / mapHeight1);

        float offsetX = (mapWidth2  - mapWidth1 * mapScaleFactor) / 2f;
        float offsetY = (mapHeight2  - mapHeight1 * mapScaleFactor) / 2f;
        mapOffsetInUnits = new Vector2(offsetX, offsetY);

        Debug.Log("Map offset in units: " + mapOffsetInUnits);

        Debug.Log("Map 1 width: " + mapWidth1 + ", height: " + mapHeight1);
        Debug.Log("Map 2 width: " + mapWidth2 + ", height: " + mapHeight2);
        player = GameObject.FindGameObjectWithTag("Player");
    }
    void Update()
    {
        Vector3 playerPos = player.transform.position;
        Vector3 mapPlane1Pos = mapPlane1.transform.position;
        Quaternion mapPlane1Rot = mapPlane1.transform.rotation;
        Vector3 mapPlane2Pos = mapPlane2.transform.position;
        Quaternion mapPlane2Rot = mapPlane2.transform.rotation;

        float x = ((playerPos.x - mapPlane1Pos.x) * Mathf.Cos(mapPlane1Rot.eulerAngles.y * Mathf.Deg2Rad) + (playerPos.z - mapPlane1Pos.z) * Mathf.Sin(mapPlane1Rot.eulerAngles.y * Mathf.Deg2Rad) - mapOffsetInUnits.x) * mapScaleFactor;
        float y = ((playerPos.z - mapPlane1Pos.z) * Mathf.Cos(mapPlane1Rot.eulerAngles.y * Mathf.Deg2Rad) - (playerPos.x - mapPlane1Pos.x) * Mathf.Sin(mapPlane1Rot.eulerAngles.y * Mathf.Deg2Rad) - mapOffsetInUnits.y) * mapScaleFactor;

        Vector3 iconPos = new Vector3(y, 0f,-x);
        iconPos = Quaternion.Euler(0f, -mapPlane2Rot.eulerAngles.y, 0f) * iconPos + mapPlane2Pos;

        mapIcon.position = iconPos ;
    }
}

当我应用此代码时,播放器图标的位置显示有些相同,但当我缩放地图时,它会发生变化。

我希望当我使 2d 地图变大和变小时,播放器图标需要保持在同一位置,即拉伸地图之前的位置。

英语不是我的第一语言,请不要介意。

c# unity3d game-development unityscript
1个回答
0
投票

每次更新都需要重新计算

mapScaleFactor
mapOffsetInUnits
以保持在正确的位置。

Start()
函数中的部分代码移动到另一个函数中,并在每次更新时调用它

void Start()
{
    RecalculateParameters();
    Debug.Log("Map offset in units: " + mapOffsetInUnits);

    player = GameObject.FindGameObjectWithTag("Player");
}

void RecalculateParameters()
{
    Renderer mapRenderer1 = mapPlane1.GetComponent<Renderer>();
    Vector3 mapSize1 = mapRenderer1.bounds.size;
    float mapWidth1 = mapSize1.x;
    float mapHeight1 = mapSize1.z;

    Renderer mapRenderer2 = mapPlane2.GetComponent<Renderer>();
    Vector3 mapSize2 = mapRenderer2.bounds.size;
    float mapWidth2 = mapSize2.x;
    float mapHeight2 = mapSize2.z;

    mapScaleFactor = Mathf.Min(mapWidth2 / mapWidth1, mapHeight2 / mapHeight1);

    float offsetX = (mapWidth2  - mapWidth1 * mapScaleFactor) / 2f;
    float offsetY = (mapHeight2  - mapHeight1 * mapScaleFactor) / 2f;
    mapOffsetInUnits = new Vector2(offsetX, offsetY);

}

void Update()
{
    RecalculateParameters();
    ...
    // your Update function
}

}

© www.soinside.com 2019 - 2024. All rights reserved.