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

所属分类: 软件编程 / C#教程 阅读数: 98
收藏 0 赞 0 分享

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

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

链接:Unity实现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经典小游戏汇总

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

更多精彩内容其他人还在看

C# SendInput 模拟鼠标操作的实现方法

C# SendInput 模拟鼠标操作的实现方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#中 paint()与Onpaint()的区别

paint是事件onpaint方法onpaint方法是调用paint事件的,用哪一个,效果是一样,就看那一个方便了内部是这样实现的:
收藏 0 赞 0 分享

c#中GetType()与Typeof()的区别

c#中GetType()与Typeof()的区别,需要的朋友可以参考一下
收藏 0 赞 0 分享

将字符串转换成System.Drawing.Color类型的方法

将字符串转换成System.Drawing.Color类型的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# 抓取网页内容的方法

C# 抓取网页内容的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

基于C#后台调用跨域MVC服务及带Cookie验证的实现

本篇文章介绍了,基于C#后台调用跨域MVC服务及带Cookie验证的实现。需要的朋友参考下
收藏 0 赞 0 分享

使用C#获取远程图片 Form用户名与密码Authorization认证的实现

本篇文章介绍了,使用C#获取远程图片 Form用户名与密码Authorization认证的实现。需要的朋友参考下
收藏 0 赞 0 分享

Winform跨线程操作的简单方法

线程间操作无效:从不是创建控件“label1”的线程访问它
收藏 0 赞 0 分享

C# WINFORM 强制让窗体获得焦点的方法代码

C# WINFORM 强制让窗体获得焦点的方法代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#中方括号[]的语法及作用介绍

C#中方括号[]可用于数组,索引、属性,更重要的是用于外部DLL类库的引用。
收藏 0 赞 0 分享
查看更多