Unity3D实现人物移动示例
程序员文章站
2022-05-14 22:15:54
一个是通过w、a、s、d来移动人物(示例一),另个是按屏幕上的按钮来移动人物(示例二)。很简单,只改了几行代码。
下面是“assets”文件夹里面的资源。
示例一:...
一个是通过w、a、s、d来移动人物(示例一),另个是按屏幕上的按钮来移动人物(示例二)。很简单,只改了几行代码。
下面是“assets”文件夹里面的资源。
示例一:
using system.collections; using system.collections.generic; using unityengine; public class e3_07keyboard : monobehaviour { //动画数组 private object[] animup; private object[] animdown; private object[] animleft; private object[] animright; //地图贴图 private texture2d map; //当前人物动画 private object[] tex; //人物x坐标 private int x; //人物y坐标 private int y; //帧序列 private int nowfram; //动画帧的总数 private int mframecount; //限制一秒多少帧 private float fps = 5; //限制帧的时间 private float time = 0; void start() { //得到帧动画中的所有图片资源 animup = resources.loadall("up"); animdown = resources.loadall("down"); animleft = resources.loadall("left"); animright = resources.loadall("right"); //得到地图资源 map = (texture2d)resources.load("map/map"); //设置默认动画 tex = animup; } void ongui() { //绘制贴图 gui.drawtexture(new rect(0, 0, screen.width, screen.height), map, scalemode.stretchtofill, true, 0); //绘制帧动画 drawanimation(tex, new rect(x, y, 32, 48)); //点击按钮移动人物 if (input.getkey(keycode.w)) { y -= 2; tex = animup; } if (input.getkey(keycode.s)) { y += 2; tex = animdown; } if (input.getkey(keycode.a)) { x -= 2; tex = animleft; } if (input.getkey(keycode.d)) { x += 2; tex = animright; } } void drawanimation(object[] tex, rect rect) { //绘制当前帧 gui.drawtexture(rect, (texture)tex[nowfram], scalemode.stretchtofill, true, 0); //计算限制帧时间 time += time.deltatime; //超过限制帧则切换图片 if (time >= 1.0 / fps) { //帧序列切换 nowfram++; //限制帧清空 time = 0; //超过帧动画总数从第0帧开始 if (nowfram >= tex.length) { nowfram = 0; } } } }
示例二
using system.collections; using system.collections.generic; using unityengine; public class e3_07button : monobehaviour { //动画数组 private object[] animup; private object[] animdown; private object[] animleft; private object[] animright; //地图贴图 private texture2d map; //当前人物动画 private object[] tex; //人物x坐标 private int x; //人物y坐标 private int y; //帧序列 private int nowfram; //动画帧的总数 private int mframecount; //限制一秒多少帧 private float fps = 5; //限制帧的时间 private float time = 0; void start() { //得到帧动画中的所有图片资源 animup = resources.loadall("up"); animdown = resources.loadall("down"); animleft = resources.loadall("left"); animright = resources.loadall("right"); //得到地图资源 map = (texture2d)resources.load("map/map"); //设置默认动画 tex = animup; } void ongui() { //绘制贴图 gui.drawtexture(new rect(0, 0, screen.width, screen.height), map, scalemode.stretchtofill, true, 0); //绘制帧动画 drawanimation(tex, new rect(x, y, 32, 48)); //点击按钮移动人物 if (guilayout.repeatbutton("向上")) { y -= 2; tex = animup; } if (guilayout.repeatbutton("向下")) { y += 2; tex = animdown; } if (guilayout.repeatbutton("向左")) { x -= 2; tex = animleft; } if (guilayout.repeatbutton("向右")) { x += 2; tex = animright; } } void drawanimation(object[] tex, rect rect) { //绘制当前帧 gui.drawtexture(rect, (texture)tex[nowfram], scalemode.stretchtofill, true, 0); //计算限制帧时间 time += time.deltatime; //超过限制帧则切换图片 if (time >= 1.0 / fps) { //帧序列切换 nowfram++; //限制帧清空 time = 0; //超过帧动画总数从第0帧开始 if (nowfram >= tex.length) { nowfram = 0; } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。