在传统RPG 游戏中是非常常见的,怎么实现一个简单的地图卷动呢!其实做法很简单,就是检测何时需要同步相机与角色的XY轴.【第一步 创建一个scene】 BigMapBg 场景大地图, MainCamera 主相机 , role (球体) , ViewBg 可视区域。更多精彩请点击【狗刨学习网】在 MainCamera 上帮上 TestGame 脚本, 设置 role ,viewPos 预设下面附上代码[code]csharpcode:public class TestGame : MonoBehaviour{ public GameObject role; public GameObject viewPos; private GameRole _gameRole; private float _viewPosZ; GameObject cameGo; void Awake() { cameGo = GameObject.Find("Main Camera"); _gameRole = new GameRole(role, cameGo); _viewPosZ = viewPos.transform.position.z; } void Update() { Vector3 vect = Vector3.zero; if (Input.GetKeyDown(KeyCode.W)) _gameRole.Move(GameRole.MoveType.Up); else if (Input.GetKeyDown(KeyCode.A)) _gameRole.Move(GameRole.MoveType.Left); else if (Input.GetKeyDown(KeyCode.D)) _gameRole.Move(GameRole.MoveType.Right); else if (Input.GetKeyDown(KeyCode.S)) _gameRole.Move(GameRole.MoveType.Down); //Vector3 cameVect = cameGo.transform.position; //cameVect.z = _viewPosZ; //viewPos.transform.position = cameVect; }}API 代码[code]csharpcode:public class GameRole{ public enum MoveType { Up, Down, Left, Right } private GameObject _roleGo; private GameObject _cameGo; private const float SPEED = 5f; public Vector2 max_map = new Vector2(, ); public Vector2 max_came = new Vector2(, ); public GameRole(GameObject roleGo, GameObject cameGO) { _roleGo = roleGo; _cameGo = cameGO; } public void Move(MoveType type) { Vector3 speed = Vector3.zero; switch (type) { case MoveType.Up: speed = new Vector3(0, SPEED, 0); break; case MoveType.Down: speed = new Vector3(0, -SPEED, 0); break; case MoveType.Left: speed = new Vector3(-SPEED, 0, 0); break; case MoveType.Right: speed = new Vector3(SPEED, 0, 0); break; } MoveRole(speed); MoveCame(speed); } /// <summary> /// 移动相机 /// </summary> /// <param name="curSpeed"></param> private void MoveCame(Vector3 curSpeed) { Vector3 camePos = _cameGo.transform.position; Vector3 rolePos = _roleGo.transform.position; rolePos.z = camePos.z; Vector3 dVect = rolePos - camePos; float dx = max_map.x - Mathf.Abs(dVect.x); // 角色.x - 地图.x float dy = max_map.y - Mathf.Abs(dVect.y); // 角色.y - 地图.y float dxC = max_came.x - Mathf.Abs(rolePos.x); // 角色.x - 可见区域.x float dyC = max_came.y - Mathf.Abs(rolePos.y); // 角色.y - 可见区域.y if (dxC >= 0 && dyC >= 0); // x,y 都没达到最大 else if (dx > 0 && dxC >= 0) // x 没有达到最大 rolePos.y = camePos.y; else if (dy > 0 && dyC >= 0) // y 轴没有达到最大 rolePos.x = camePos.x; else return; _cameGo.transform.position = rolePos; } /// <summary> /// 移动角色 /// </summary> /// <param name="curSpeed"></param> private void MoveRole(Vector3 curSpeed) { Vector3 curPos = _roleGo.transform.position; Vector3 tagPos = curPos curSpeed; // 在角色可移动区域内 if (isInRec(tagPos, max_map)) _roleGo.transform.position = tagPos; } private bool isInRec(Vector3 pos, Vector2 Rect) { if (Rect.x - Mathf.Abs(pos.x) >= 0 && Rect.y - Mathf.Abs(pos.y) >= 0) return true; return false; }}
推荐整理分享RPG 游戏地图卷动简单API(rpg好玩地图),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:rpg好玩地图,rpg游戏大地图,rpg地图包,rpg游戏地图素材,rpg游戏大地图,rpg地图图片,地图卷轴有什么用,rpg地图包,内容如对您有帮助,希望把文章链接给更多的朋友!
关于XML的读写操作以及查询操作 我们在做一些项目的时候,往往会用XML来配置内部成员,部署他们之间的关系,使得程序运行起来不至于太混乱,今天就稍微讲解一下关于XML的一些用
设计模式--简单工厂模式在unity3d里面的使用 来自
Unity3D 帧数修改 1、在Quality(质量)设置里把帧数设定关闭之后才能在代码中修改游戏运行的帧数2、在Unity中新建脚本UpdateFrame.cs,编写如下代码usingUnityEngine;usingSystem.Collec