每個人!我在 Unity 2D 上制作游戲時遇到了問題。當玩家按住 LeftShift 并處于帶有“Snow”標簽的觸發器中時,我需要挖掘特定的雪塊(瓷磚地圖確實有這樣的標簽)。我決定改變比例,因為它是我認為變體玩家最容易理解的(使精靈變暗,破壞它等)。現在我有這個代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class SnowTaking : MonoBehaviour
{ //Tile variables
public Tilemap tilemap;
public ITilemap iTilemap;
public Tile whiteTile;
public Tile redTile;
public Tile greenTile;
public Tile blueTile;
void OnTriggerStay2D(Collider2D other) {
if(other.gameObject.tag == "Snow") {
if(Input.GetKey(KeyCode.LeftShift)) {
//При нажатии LeftShift
float x = gameObject.transform.position.x;
float y = gameObject.transform.position.y;
float z = 0;
Vector3 position = new Vector3(x, y, z);
Vector3Int tilePosition = tilemap.WorldToCell(position);
Tile currentTile = tilemap.GetTile<Tile>(tilePosition);
//Conditions for each type of snow
if(whiteTile == currentTile) {
//Scaling tile here
Debug.Log("White");
} else if(redTile == currentTile) {
//Scaling tile here
Debug.Log("Red");
} else if(greenTile == currentTile) {
//Scaling tile here
Debug.Log("Green");
} else if(blueTile == currentTile) {
//Scaling tile here
Debug.Log("Blue");
} else {
Debug.Log("None");
}
}
}
}
}
我可以使用什么來縮放磁貼?先感謝您!
我已經嘗試了一些事情:
- 首先,我通過網路(尤其是在檔案中)搜索了一些功能,這些功能對確切的瓷磚做了一些事情;
- 然后我嘗試使用Matrix4x4來縮放tile,但是沒有按預期作業(根本沒有作業,但至少沒有錯誤);
currentTile.transform = Matrix4x4.Scale(new Vector3(0.5f, 0.5f, 1));
- 當我別無選擇時,我嘗試自己做一些事情,并使用了 Sprites。當然,它沒有用;
Sprite currentSprite = currentTile.sprite;
currentSprite.localScale -= new Vector3(0.01f, 0.01f, 0);
- 然后我在這里搜索了這樣一個問題,在 StackOverflow 上,但沒有找到任何對我有幫助的東西,所以我的問題是!
uj5u.com熱心網友回復:
您可以使用tilemap.SetTransformMatrix();
在你的情況下,而不是
currentTile.transform = Matrix4x4.Scale(new Vector3(0.5f, 0.5f, 1));
您可以使用tilemap.SetTransformMatrix(tilePosition, Matrix4x4.Scale(new Vector3(0.5f, 0.5f, 1)));
如果你想影片它:
void Update() {
if (Input.GetKey(KeyCode.LeftShift)) {
time = 0f;
}
if (time < scaleDuration) {
time = Time.deltaTime;
var scaleValue = _animationCurve.Evaluate(time / scaleDuration);
tilemap.SetTransformMatrix(tilePosition, Matrix4x4.Scale(new Vector3(scaleValue, scaleValue, 1)));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/536474.html
標籤:C#unity3d2d
