服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - Unity实现3D贪吃蛇的移动代码

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

2022-09-02 11:44永远的小白虾 C#

这篇文章主要为大家详细介绍了Unity实现3D贪吃蛇的移动代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

链接:Unity实现3D贪吃蛇

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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);
  }
  
 }
}

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

原文链接:https://blog.csdn.net/qq_41487299/article/details/101036418

延伸 · 阅读

精彩推荐
  • C#C#知识整理

    C#知识整理

    本文主要介绍了C#的相关知识。具有很好的参考价值,下面跟着小编一起来看下吧...

    键盘上青春11272021-12-22
  • C#人脸认证源码faceIdentify详解

    人脸认证源码faceIdentify详解

    这篇文章主要为大家详细介绍了人脸认证源码faceIdentify的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    穆雄雄6862022-08-04
  • C#C#实现手机拍照并且保存水印照片

    C#实现手机拍照并且保存水印照片

    这篇文章主要介绍了C#实现手机拍照并且保存水印照片的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下...

    我的名称很霸气5062021-12-09
  • C#C#模拟链表数据结构的实例解析

    C#模拟链表数据结构的实例解析

    这篇文章主要介绍了C#模拟链表数据结构的实例解析,包括队双向链表的模拟方法,例子中队链表的操作也有很好的说明,需要的朋友可以参考下...

    灵犀6262021-11-19
  • C#C#实现获取本地内网(局域网)和外网(公网)IP地址的方法分析

    C#实现获取本地内网(局域网)和外网(公网)IP地址的方法分析

    这篇文章主要介绍了C#实现获取本地内网(局域网)和外网(公网)IP地址的方法,结合实例形式总结分析了C#获取IP地址相关原理、实现方法与操作注意事项,需要...

    willingtolove7492022-08-29
  • C#C#简单连接sql数据库的方法

    C#简单连接sql数据库的方法

    这篇文章主要介绍了C#简单连接sql数据库的方法,涉及C#基于控制台的数据库连接创建于命令执行相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    吕秀才6112021-11-25
  • C#C#中如何正确的使用字符串String

    C#中如何正确的使用字符串String

    这篇文章主要给大家介绍了关于在C#中如何正确的使用字符串String的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考...

    Fode6852022-03-06
  • C#C#学习教程之Socket的简单使用

    C#学习教程之Socket的简单使用

    这篇文章主要给大家介绍了关于C#学习教程之Socket的简单使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    张子浩8602022-03-09