Unity - A计划(永久有效期) 扫二维码继续学习 二维码时效为半小时

(196评价)
价格: 4019.00元
《2DRoguelike拾荒者》角色移动不正常
开发者微光发起了问答2017-12-28
2
回复
835
浏览

问题1,角色移动不正常,有时可以移动,有时不可以移动,有时移动到某些位置后便不能移动。

问题2,角色无法攻击地图内的墙,(没有动画)墙也不会消失

问题3.食物生成位置不正确,有一定几率会生成在外围墙里

一些关键代码:

 


        //创建障碍物 食物 敌人
        //创建障碍物
        int wallCount = Random.Range(minCountWall, maxCountWall + 1);//障碍物的个数
        InstantiateItems(wallCount,wallArray);
        //创建食物 2 - level*2
        int foodCount = Random.Range(2, gameManager.level*2 + 1);
        InstantiateItems(foodCount,foodArray);

        //创建敌人 // level/2
        int enemyCount = gameManager.level/2;
        InstantiateItems(enemyCount,enemyArray);
        //创建出口
        GameObject go4 = Instantiate(exitPrefab, new Vector2(cols - 2, rows - 2), Quaternion.identity) as GameObject;
        go4.transform.SetParent(mapHolder);

private void InstantiateItems(int count, GameObject[] prefabs) {
        for (int i = 0; i < count; i++)
        {
            Vector2 pos = RandomPosition();
            GameObject enemyPrefab = RandomPrefab(prefabs);
            GameObject go = Instantiate(enemyPrefab, pos, Quaternion.identity) as GameObject;
            go.transform.SetParent(mapHolder);
        }
    }
    private Vector2 RandomPosition() {
        int positionIndex = Random.Range(0, positionList.Count);
        Vector2 pos = positionList[positionIndex];
        positionList.RemoveAt(positionIndex);
        return pos;
    }

    private GameObject RandomPrefab(GameObject[] prefabs) {
        int index = Random.Range(0, prefabs.Length);
        return prefabs[index];
    }
}

 

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

public class MapManager : MonoBehaviour {

    public GameObject[] outWallArray;
    public GameObject[] floorArray;
    public GameObject[] wallArray;
    public GameObject[] foodArray;
    public GameObject[] enemyArray;
    public GameObject exitPrefab;

    public int rows=10;
    public int cols=10;
    public int minCountWall = 2;
    public int maxCountWall = 8;
    private Transform mapHolder;
    private List<Vector2> positionList = new List<Vector2>();
    private GameManager gameManager;
    void Update () {
    
    }
    //初始化地图
    public void InitMap()
    {
        gameManager = this.GetComponent<GameManager>();
        mapHolder = new GameObject("Map").transform;
        //下面是创建围墙和地板
        for (int x = 0; x < cols; x++) {
            for (int y = 0; y < rows; y++) {
                if (x == 0 || y == 0 || x == cols - 1 || y == rows - 1) {
                    int index = Random.Range(0, outWallArray.Length);
                    GameObject go =  GameObject.Instantiate(outWallArray[index], new Vector3(x, y, 0), Quaternion.identity) as GameObject;
                    go.transform.SetParent(mapHolder);
                }
                else
                {
                    int index = Random.Range(0, floorArray.Length);
                    GameObject go =  GameObject.Instantiate(floorArray[index], new Vector3(x, y, 0), Quaternion.identity) as GameObject;
                    go.transform.SetParent(mapHolder);
                }
            }
        }
        positionList.Clear();
        for (int x = 2; x < cols-2; x++) {
            for (int y = 2; y < rows-2; y++) {
                positionList.Add(new Vector2(x,y));
            }
        }

        //创建障碍物 食物 敌人
        //创建障碍物
        int wallCount = Random.Range(minCountWall, maxCountWall + 1);//障碍物的个数
        InstantiateItems(wallCount,wallArray);
        //创建食物 2 - level*2
        int foodCount = Random.Range(2, gameManager.level*2 + 1);
        InstantiateItems(foodCount,foodArray);

        //创建敌人 // level/2
        int enemyCount = gameManager.level/2;
        InstantiateItems(enemyCount,enemyArray);
        //创建出口
        GameObject go4 = Instantiate(exitPrefab, new Vector2(cols - 2, rows - 2), Quaternion.identity) as GameObject;
        go4.transform.SetParent(mapHolder);

    }

    private void InstantiateItems(int count, GameObject[] prefabs) {
        for (int i = 0; i < count; i++)
        {
            Vector2 pos = RandomPosition();
            GameObject enemyPrefab = RandomPrefab(prefabs);
            GameObject go = Instantiate(enemyPrefab, pos, Quaternion.identity) as GameObject;
            go.transform.SetParent(mapHolder);
        }
    }
    private Vector2 RandomPosition() {
        int positionIndex = Random.Range(0, positionList.Count);
        Vector2 pos = positionList[positionIndex];
        positionList.RemoveAt(positionIndex);
        return pos;
    }

    private GameObject RandomPrefab(GameObject[] prefabs) {
        int index = Random.Range(0, prefabs.Length);
        return prefabs[index];
    }
}
 

<

 

所有回复
  • siki 2017-12-29

    1,关于食物的生成,在生成食物的时候,通过得到的生成位置,检查下食物的生成位置是否在边上就是墙上,如果在的话,就不继续生成,而去重新得到位置

    2,检查下射线检测是否碰撞到了墙,以及墙和外墙的tag写的是否正确,还有就是墙和外墙的collider是否添加,还有就是打印写射线的tag,看下是否碰撞到了什么问题

    3,在控制移动的时候,在代码中输出下,看下是否运行了控制移动的代码,还是由于某些原因检测到了人物运动的前方有墙或者别的障碍物

    还有-4条回复,点击查看
    你还没有登录,请先登录注册
发表回复
你还没有登录,请先 登录或 注册!