Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity TileMaps:如何返回占据特定矢量位置的tile的层名称、标记或其他标识信息?_C#_Unity3d - Fatal编程技术网

C# Unity TileMaps:如何返回占据特定矢量位置的tile的层名称、标记或其他标识信息?

C# Unity TileMaps:如何返回占据特定矢量位置的tile的层名称、标记或其他标识信息?,c#,unity3d,C#,Unity3d,我正在做一个游戏,发生在一个瓷砖地图。每个平铺是32x32,当我的地图上的字符移动时,它们一次移动32个像素 我想做的是检查Vector3位置,让它返回它是什么类型的平铺。就像,只是图层很好 我的地图有5层: 1.地板:每个人都可以在上面行走。 2.下沉地板:对于沼泽或浅水的瓷砖,玩家的精灵会变成另一个版本,当他们在瓷砖上时,会下沉到臀部。 3.低挡:像深坑、深水之类的东西。大多数角色被阻止,除非他们有飞行。 4.街区:墙。它们阻止所有字符。 5.门:有时阻挡,有时通过,取决于玩家是否有正确的钥

我正在做一个游戏,发生在一个瓷砖地图。每个平铺是32x32,当我的地图上的字符移动时,它们一次移动32个像素

我想做的是检查Vector3位置,让它返回它是什么类型的平铺。就像,只是图层很好

我的地图有5层: 1.地板:每个人都可以在上面行走。 2.下沉地板:对于沼泽或浅水的瓷砖,玩家的精灵会变成另一个版本,当他们在瓷砖上时,会下沉到臀部。 3.低挡:像深坑、深水之类的东西。大多数角色被阻止,除非他们有飞行。 4.街区:墙。它们阻止所有字符。 5.门:有时阻挡,有时通过,取决于玩家是否有正确的钥匙

这是我的角色类,其他角色从中继承

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

public abstract class Character : MonoBehaviour
{
    public int health, wounds;
    public List<Sprite> frame;

    protected BoxCollider2D boxCollider;

    protected virtual void Start()
    {
        boxCollider = GetComponent<BoxCollider2D>();
    }

    protected virtual void MoveNorth()
    { //moves the character 1 tile north
        Vector3 oldLoc = this.gameObject.transform.position;
        Vector3 newLoc = new Vector3(oldLoc.x, oldLoc.y + (Game.instance.TILESIZE / 100f), oldLoc.z);        
        this.gameObject.transform.position = newLoc;
    }

    protected virtual void MoveWest()
    {//moves the character 1 tile west
        Vector3 oldLoc = this.gameObject.transform.position;
        Vector3 newLoc = new Vector3(oldLoc.x - (Game.instance.TILESIZE / 100f), oldLoc.y, oldLoc.z);        
        this.gameObject.transform.position = newLoc;
    }

    protected virtual void MoveSouth()
    {//moves the character 1 tile south
        Vector3 oldLoc = this.gameObject.transform.position;
        Vector3 newLoc = new Vector3(oldLoc.x, oldLoc.y - (Game.instance.TILESIZE / 100f), oldLoc.z);
        this.gameObject.transform.position = newLoc;
    }

    protected virtual void MoveEast()
    {//moves the character 1 tile east
        Vector3 oldLoc = this.gameObject.transform.position;
        Vector3 newLoc = new Vector3(oldLoc.x + (Game.instance.TILESIZE / 100f), oldLoc.y, oldLoc.z);
        this.gameObject.transform.position = newLoc;
    }

    protected virtual void WarpTo(int x, int y)
    {
        //Warps Sprite to specified tile.
        float trueTileSize = Game.instance.TILESIZE / 100f;
        float trueX = (x * trueTileSize + (trueTileSize / 2f));
        float trueY = (y * trueTileSize + (trueTileSize / 2f));
        this.gameObject.transform.position = new Vector3(trueX, trueY, 0f);
        Debug.Log("Tile size = " + trueTileSize + "   X: " + trueX + "   y: "+ trueY);
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共抽象类特征:单行为
{
公共卫生、伤口;
公共列表框架;
受保护的boxCollider R2D boxCollider;
受保护的虚拟void Start()
{
boxCollider=GetComponent();
}
受保护的虚拟void MoveNorth()
{//将角色1平铺向北移动
Vector3 oldLoc=this.gameObject.transform.position;
Vector3 newLoc=新Vector3(oldLoc.x,oldLoc.y+(Game.instance.TILESIZE/100f),oldLoc.z);
this.gameObject.transform.position=newLoc;
}
受保护的虚拟void MoveWest()
{//将角色1平铺向西移动
Vector3 oldLoc=this.gameObject.transform.position;
Vector3 newLoc=新Vector3(oldLoc.x-(Game.instance.TILESIZE/100f),oldLoc.y,oldLoc.z);
this.gameObject.transform.position=newLoc;
}
受保护的虚拟void MoveSouth()
{//将角色1平铺向南移动
Vector3 oldLoc=this.gameObject.transform.position;
Vector3 newLoc=新Vector3(oldLoc.x,oldLoc.y-(Game.instance.TILESIZE/100f),oldLoc.z);
this.gameObject.transform.position=newLoc;
}
受保护的虚拟void MoveEast()
{//将角色1平铺向东移动
Vector3 oldLoc=this.gameObject.transform.position;
Vector3 newLoc=新Vector3(oldLoc.x+(Game.instance.TILESIZE/100f)、oldLoc.y、oldLoc.z);
this.gameObject.transform.position=newLoc;
}
受保护的虚拟void WarpTo(int x,int y)
{
//将精灵扭曲到指定的平铺。
float trueTileSize=Game.instance.TILESIZE/100f;
浮点trueX=(x*trueTileSize+(trueTileSize/2f));
浮点trueY=(y*trueTileSize+(trueTileSize/2f));
this.gameObject.transform.position=新矢量3(trueX,trueY,0f);
Debug.Log(“Tile size=“+trueTileSize+”X:+trueX+”y:+trueY”);
}
}
计划是插入代码,检测NewLoc上的瓷砖,然后确定是否可以完成移动。我还想检测我的角色站在什么瓷砖上,这样我就可以将精灵更改为“涉水”版本,但我还没有相关的代码

我知道这可能是最基本的。我花了很多时间搜索和研究,但要么我想得太多了,要么我就是不明白我一直在寻找的答案

我认为这个解决方案可能与TileMap.GetTile有关,但我不知道如何使用它,而且我似乎找不到一个以我理解的方式使用它的示例

感谢您的帮助

根据Ignacio Alorre的建议更新: 这是我的地图:

这里是“下沉地板”层:

以下是该图层的设置:

这是我的新角色抽象类,尝试添加CheckTile函数:

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

public abstract class Character : MonoBehaviour
{
public int health, wounds;
public List<Sprite> frame;

protected BoxCollider2D boxCollider;

protected virtual void Start()
{
    boxCollider = GetComponent<BoxCollider2D>();
}

protected virtual void MoveNorth()
{ //moves the character 1 tile north
    Vector3 oldLoc = this.gameObject.transform.position;
    Vector3 newLoc = new Vector3(oldLoc.x, oldLoc.y + 
(Game.instance.TILESIZE / 100f), oldLoc.z);
    checkTile(newLoc);
    this.gameObject.transform.position = newLoc;
}

protected virtual void MoveWest()
{//moves the character 1 tile west
    Vector3 oldLoc = this.gameObject.transform.position;
    Vector3 newLoc = new Vector3(oldLoc.x - (Game.instance.TILESIZE / 
100f), oldLoc.y, oldLoc.z);        
    this.gameObject.transform.position = newLoc;
}

protected virtual void MoveSouth()
{//moves the character 1 tile south
    Vector3 oldLoc = this.gameObject.transform.position;
    Vector3 newLoc = new Vector3(oldLoc.x, oldLoc.y - 
(Game.instance.TILESIZE / 100f), oldLoc.z);
    this.gameObject.transform.position = newLoc;
}

protected virtual void MoveEast()
{//moves the character 1 tile east
    Vector3 oldLoc = this.gameObject.transform.position;
    Vector3 newLoc = new Vector3(oldLoc.x + (Game.instance.TILESIZE / 
100f), oldLoc.y, oldLoc.z);
    this.gameObject.transform.position = newLoc;
}

protected virtual void WarpTo(int x, int y)
{
    //Warps Sprite to specified tile.
    float trueTileSize = Game.instance.TILESIZE / 100f;
    float trueX = (x * trueTileSize + (trueTileSize / 2f));
    float trueY = (y * trueTileSize + (trueTileSize / 2f));
    this.gameObject.transform.position = new Vector3(trueX, trueY, 0f);
    Debug.Log("Tile size = " + trueTileSize + "   X: " + trueX + "   y: "+ 
trueY);
}

protected virtual int checkTile(Vector3 tileLoc)
{
    RaycastHit hit;
    float distance = 1f;
    Vector3 dir = new Vector3(0, -1, 0);        
    //Vector3 locAdjusted = new Vector3(tileLoc.x, tileLoc.y, tileLoc.z - 
    //0.5f);

    int layer = -1;

    if(Physics.Raycast(tileLoc, dir, out hit, distance))
    {
        layer = hit.collider.gameObject.layer;
        Debug.Log("Layer #: " + layer);
    }       

    return layer;
  }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.Tilemaps;
公共抽象类特征:单行为
{
公共卫生、伤口;
公共列表框架;
受保护的boxCollider R2D boxCollider;
受保护的虚拟void Start()
{
boxCollider=GetComponent();
}
受保护的虚拟void MoveNorth()
{//将角色1平铺向北移动
Vector3 oldLoc=this.gameObject.transform.position;
Vector3 newLoc=新Vector3(oldLoc.x,oldLoc.y+
(Game.instance.TILESIZE/100f),oldLoc.z);
方格瓷砖(newLoc);
this.gameObject.transform.position=newLoc;
}
受保护的虚拟void MoveWest()
{//将角色1平铺向西移动
Vector3 oldLoc=this.gameObject.transform.position;
Vector3 newLoc=新Vector3(oldLoc.x-(Game.instance.TILESIZE/
100f),旧位置y,旧位置z);
this.gameObject.transform.position=newLoc;
}
受保护的虚拟void MoveSouth()
{//将角色1平铺向南移动
Vector3 oldLoc=this.gameObject.transform.position;
Vector3 newLoc=新Vector3(旧位置x,旧位置y-
(Game.instance.TILESIZE/100f),oldLoc.z);
this.gameObject.transform.position=newLoc;
}
受保护的虚拟void MoveEast()
{//将角色1平铺向东移动
Vector3 oldLoc=this.gameObject.transform.position;
Vector3 newLoc=新Vector3(oldLoc.x+(Game.instance.TILESIZE/
100f),旧位置y,旧位置z);
this.gameObject.transform.position=newLoc;
}
受保护的虚拟void WarpTo(int x,int y)
{
//将精灵扭曲到指定的平铺。
float trueTileSize=Game.instance.TILESIZE/100f;
浮点trueX=(x*trueTileSize+(trueTileSize/2f));
浮点trueY=(y*trueTileSize+(trueTileSize/2f));
this.gameObject.transform.position=新矢量3(trueX,trueY,0f);
Debug.Log(“Tile size=“+trueTileSize+”X:“+trueX+”y:”+
真的);
}
受保护的虚拟整数校验分幅(矢量3分幅)
{
雷卡斯特击中;
浮动距离=1f;
Vector3 dir=新的Vector3(0,-1,0);
//Vector3 locAdjusted=新Vect
int checkTileType()
{
    RaycastHit hit;
    float distance = 1f;
    Vector3 dir = new Vector3(0, -1);

    int layer = -1;

    //With with you check what is under the game object
    if(Physics.Raycast(transform.position, dir, out hit, distance))
    {
        //With this you can extract the layer number
        layer = hit.collider.gameObject.layer
    }

    return layer;

}