当前位置: 移动技术网 > IT编程>开发语言>c# > Unity实现3D贪吃蛇的移动代码

Unity实现3D贪吃蛇的移动代码

2020年05月10日  | 移动技术网IT编程  | 我要评论
本文实例为大家分享了unity实现3d贪吃蛇移动的具体代码,供大家参考,具体内容如下记录一下前段时间写到的一个3d贪吃蛇的移动代码。链接:unity实现3d贪吃蛇using system.collec

本文实例为大家分享了unity实现3d贪吃蛇移动的具体代码,供大家参考,具体内容如下

记录一下前段时间写到的一个3d贪吃蛇的移动代码。

链接:

using system.collections;
using system.collections.generic;
using unityengine;
using unityengine.ui;


public class gamemanager : monobehaviour
{
 list<transform> bodylist = new list<transform>();//身体位置的列表
 private float speed = 2f;//移动速度
 public gameobject bodyobj;//用来生成的身体对象
 list<transform> bodycreatepostion = new list<transform>();//身体部分的transfrom 列表
 private float transoffset=1f;//位置间隔
 private list<vector2> mapaddress=new list<vector2>();//前进目标在地图上的位置,
 public transform firstbody;//第一个身体部件的transfrom
 const int row = 100;//地图宽
 const int col = 100;//地图高
 int step=1;//步伐
 int x=0, z=0;//记录x和z轴的移动量

 // start is called before the first frame update
 public vector3[,] map = new vector3[row, col];//地图
 // start is called before the first frame update
 void start()
 {
  
  for (int i = 0; i < row; i++)//生成地图
  {
   for (int j = 0; j < col; j++)
   {
    map[i, j] = new vector3(i, 1.5f, j);
   }
  }
  transform.position =map[0,0];将当前位置设为原点
  mapaddress.add(new vector2(1,0));//增加第一个目标
  bodylist.add(transform);//刷新第一个头和一个身体的transform
  bodylist.add(firstbody);
  mapaddress.add(new vector2(0,0));
 }

 // update is called once per frame
 void update()
 {
  for (int i = 0; i < row - 1; i++)
  {
   for (int j = 0; j < col - 1; j++)//绘制地图格子
   {
    debug.drawline(map[i, j], map[i + 1, j], color.red);
    debug.drawline(map[i, j], map[i, j + 1], color.red);
   }
  }
  if (input.anykeydown)//有任何键按下
  {
   if (input.getkeydown(keycode.w) && z != -step)//上
   {
    z = step;
    x = 0;


   }
   if (input.getkeydown(keycode.s) && z != step)//下
   {
    z = -step;
    x = 0;
    
   
   }
   if (input.getkeydown(keycode.a) && x != step)//左
   {
    x = -step;
    z = 0;

   
   }
   if (input.getkeydown(keycode.d) && x != -step)//右
   {
    x = step;
    z = 0;
   
    
   }
   

  }
  move();


 }
 private void move()//移动
 {


  if (vector3.distance(bodylist[0].position, map[(int)mapaddress[0].x + x, (int)mapaddress[0].y + z]) < 0.7f)//当前坐标和目标位置的距离小于0.5f
  {
   
   for (int i = mapaddress.count - 1; i > 0; i--)//刷新后一个的目标为前一个的目标
   {
    mapaddress[i] = mapaddress[i-1];
   }
   
   mapaddress[0] = new vector2(mapaddress[0].x + x, mapaddress[0].y + z);//刷新第一个目标的位置

  }
  else
  {
   for (int i = bodylist.count - 1; i > 0; i--)//移动
   {
    bodylist[i].position = vector3.movetowards(bodylist[i].position,
              map[(int)mapaddress[i - 1].x, (int)mapaddress[i - 1].y], time.deltatime * speed);

   }
   bodylist[0].position = vector3.movetowards(transform.position,
             map[(int)mapaddress[0].x + x, (int)mapaddress[0].y + z], time.deltatime * speed);
  }
 }

 private void oncollisionenter(collision collision)//碰撞到了食物就增加身体长度
 {

  if (collision.collider.tag == "food")
  {
   
   destroy(collision.collider.gameobject);
   gameobject tmpgameobject=new gameobject();
   vector2 tmpvec = new vector2();
   
   tmpgameobject = instantiate(bodyobj, map[(int)mapaddress[mapaddress.count - 1].x+x, (int)mapaddress[mapaddress.count - 1].y +z], quaternion.identity);//生成body物体
   tmpvec = new vector2(mapaddress[mapaddress.count - 1].x+x, mapaddress[mapaddress.count - 1].y +z);
   //增加身体的transform和目标向量
   bodylist.add(tmpgameobject.transform);
   mapaddress.add(tmpvec);
  }
  
 }
}

更多有趣的经典小游戏实现专题,分享给大家:

c++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

javascript经典游戏 玩不停

java经典小游戏汇总

javascript经典小游戏汇总

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网