Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.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
Unity3d 简单2D探路器_Unity3d_Unityscript - Fatal编程技术网

Unity3d 简单2D探路器

Unity3d 简单2D探路器,unity3d,unityscript,Unity3d,Unityscript,好的,我是一个很好的程序员,但我是Unity和游戏开发的初学者,所以说实话,我真的不知道如何实现简单的交互式2D地图 我想创建一个带有地图屏幕的2D平台,我已经完成了3D教程的一半,所以我认为2D开发不会有太多的学习曲线 然而,我不知道我怎样才能绘制地图。以下是我希望它在游戏中的外观: 形象归功于 以下是我认为我需要做的: 1) 创建一个长方体并对其应用贴图纹理 2) 为每个位置创建一个透明框,将它们放置在与纹理上的透视居住地相同的XY坐标处,并将框标记为“Plip”、“Peacemas”等

好的,我是一个很好的程序员,但我是Unity和游戏开发的初学者,所以说实话,我真的不知道如何实现简单的交互式2D地图

我想创建一个带有地图屏幕的2D平台,我已经完成了3D教程的一半,所以我认为2D开发不会有太多的学习曲线

然而,我不知道我怎样才能绘制地图。以下是我希望它在游戏中的外观:

形象归功于

以下是我认为我需要做的:

1) 创建一个长方体并对其应用贴图纹理

2) 为每个位置创建一个透明框,将它们放置在与纹理上的透视居住地相同的XY坐标处,并将框标记为“Plip”、“Peacemas”等

3) 创建一个长方体并对其应用角色纹理

然而,这是我知识的极限。我需要能够做到以下几点:

1) 基于角色的当前位置将角色放置在城镇上。我认为这并不难。我们记录角色所在的位置,然后搜索该位置,然后将角色放置在同一坐标。(降低z轴以防止像素冲突)

2) 检测到达某个位置的最快路线。我所说的最快不是指距离,而是指我们需要经过的最少的地点。我认为最好的方法是在所有位置之间手动创建轨迹:

这个功能在Unity中存在吗?这种“跟踪”方法有名字吗?如果是这样的话,那么检测哪条轨道上的位置最少,并将其作为我们将要沿着的轨道返回,就有点简单了

伙计们,在这里我真的很欣赏一些伪代码。我是说,说起来容易做起来难

假设我们在
Plip
中,我们想要得到
Turvail轨迹。最快的方法是通过两个位置。其中之一可以是:

Plip > Field Whar Battles Hap'n > Great Squid Lake > Turnvail Trail
我们如何让我们的角色跳到音轨
a
,然后
b
,然后
d
?我对玩家说他们想去图尔维尔小径旅行的过程的想法:

  • 我们需要一个循环来返回一个包含我们需要的轨迹标记的数组:

    [a'、'b'、'd'、'e']

  • 不知道这个循环会是什么样子(请给我一些想法!)。我认为每个轨迹都需要一个属性来存储它们结束位置的位置。我们将此属性称为
    end\u location
    。我认为每个位置都应该存储从它引出的轨迹。让我们将此属性称为
    location\u tracks
    。可能是这样的:


诸如此类,只是需要一些想法,伙计们。Unity初学者,如果这是Unity已经解决的简单解决方案,我不想深入讨论


  • 我们最后需要沿着数组
    ['a','b','d','e']
    移动。这一切都应该在
    Update
    功能中完成吗


这些都只是psedoo代码,给你一个想法。谢谢

这不是最好的方法,但使用MonoBehavior可以很容易地指定下一个点。
首先创建几个空游戏对象,然后将此组件附加到每个游戏对象。
使用检查器指定连接点

FindPath.js

var connectedPoint : GameObject[];
function GetPath(from : GameObject, to : GameObject, totalPath : ArrayList, oldList : ArrayList) {
    for(var go : GameObject in oldList)
        if(go == this.gameObject) return; //If this point is already add.

    oldList.Add (this.gameObject); //Add this new point to list

    if (this.gameObject == to) {
        totalPath.Add(oldList); //If this point is where you want to go, add list to totalPath and stop loop return to previous point
            return;
    }

    //Find next point
    for (var i : int = 0; i < connectedPoint.Length; i++) {
        if(connectedPoint[i] == from)
            continue; //Pass the point where you came from

        var copyList : ArrayList = new ArrayList();
        for(var go : GameObject in oldList) copyList.Add(go);

        var findPath : FindPath = connectedPoint[i].GetComponent(typeof(FindPath)) as FindPath;
        findPath.GetPath(this.gameObject, to, totalPath, copyList);
    }
}    
FindPath.js
var connectedPoint:GameObject[];
函数GetPath(from:GameObject,to:GameObject,totalPath:ArrayList,oldList:ArrayList){
for(var go:旧列表中的游戏对象)
if(go==this.gameObject)return;//如果此点已添加。
oldList.Add(this.gameObject);//将这个新点添加到列表中
if(this.gameObject==to){
totalPath.Add(oldList);//如果此点是您要去的地方,请将列表添加到totalPath并停止循环返回到上一点
返回;
}
//找到下一点
对于(变量i:int=0;i
当您需要时,只需调用GetPath()并放置开始和结束游戏对象

var from : GameObject;
var to : GameObject;
var totalPath : ArrayList = new ArrayList();
var newPath : ArrayList = new ArrayList();
var findPath : FindPath = from.GetComponent(typeof(FindPath)) as FindPath; //Use from point to start
findPath.GetPath(from, to, totalPath, newPath);

Debug.Log("Total get paths : " + totalPath.Count);
if (totalPath.Count != 0) {
    for(var i : int = 0; i < totalPath.Count; i++) {
        var path : ArrayList = totalPath[i] as ArrayList;
        Debug.Log("Path [" + i + "] step count : " + (path.Count-1));
    }
}    
var from:GameObject;
var-to:游戏对象;
var totalPath:ArrayList=new ArrayList();
var newPath:ArrayList=newarraylist();
var findPath:findPath=from.GetComponent(typeof(findPath))作为findPath//从头开始使用
GetPath(from、to、totalPath、newPath);
Log(“总获取路径:“+totalPath.Count”);
如果(totalPath.Count!=0){
for(变量i:int=0;i
然后您可以比较最小计数或距离。你可以给每个游戏对象一个标签,以便更容易地找到

FindPath.js

var connectedPoint : GameObject[];
function GetPath(from : GameObject, to : GameObject, totalPath : ArrayList, oldList : ArrayList) {
    for(var go : GameObject in oldList)
        if(go == this.gameObject) return; //If this point is already add.

    oldList.Add (this.gameObject); //Add this new point to list

    if (this.gameObject == to) {
        totalPath.Add(oldList); //If this point is where you want to go, add list to totalPath and stop loop return to previous point
            return;
    }

    //Find next point
    for (var i : int = 0; i < connectedPoint.Length; i++) {
        if(connectedPoint[i] == from)
            continue; //Pass the point where you came from

        var copyList : ArrayList = new ArrayList();
        for(var go : GameObject in oldList) copyList.Add(go);

        var findPath : FindPath = connectedPoint[i].GetComponent(typeof(FindPath)) as FindPath;
        findPath.GetPath(this.gameObject, to, totalPath, copyList);
    }
}    
var from : GameObject;
var to : GameObject;
var totalPath : ArrayList = new ArrayList();
var newPath : ArrayList = new ArrayList();
var findPath : FindPath = from.GetComponent(typeof(FindPath)) as FindPath; //Use from point to start
findPath.GetPath(from, to, totalPath, newPath);

Debug.Log("Total get paths : " + totalPath.Count);
if (totalPath.Count != 0) {
    for(var i : int = 0; i < totalPath.Count; i++) {
        var path : ArrayList = totalPath[i] as ArrayList;
        Debug.Log("Path [" + i + "] step count : " + (path.Count-1));
    }
}