unity3d 贪吃蛇移动
来源:互联网 发布:js修改button的文字 编辑:程序博客网 时间:2024/06/05 14:21
头部控制
using UnityEngine;using System.Collections;using System.Threading;public class HeadMove : MonoBehaviour{ public GameObject snack_Body; private GameObject firstSnackBody; private GameObject lastSnackBody; private GameObject CreatedBody; //随机生成食物球 private float foodMaxWidth = 48f; private float foodMinWidth = -48f; private float foodMaxForward = 48f; private float foodMinForwaed = -48f; public GameObject cube_food; private float timer = 0f; private float time = 0.3f; private GameObject cc; void RandomFood() { //随机生成食物 float foodX = Random.Range(foodMinWidth, foodMaxWidth); float foodZ = Random.Range(foodMinForwaed, foodMaxForward); cc = Instantiate(cube_food, new Vector3(foodX, 0, foodZ), Quaternion.identity) as GameObject; } void Start() { RandomFood(); } void Update() { if (timer > time) { Vector3 old = this.transform.position; this.transform.position += transform.forward; if (firstSnackBody != null) { firstSnackBody.GetComponent<CreateBody>().moveTo(old); } timer = 0; } else { timer += Time.deltaTime; } //当它得尾巴在前,头在后时,此时,它就不能往前走了(尾巴不能拖着头走) if (Input.GetKeyDown(KeyCode.W) && Vector3.Angle(Vector3.forward, this.transform.forward) < 160f) { //此时可以移动了 //类似于单链表,头移动,头的坐标传给第一个身体,第一个身体传给第二个身体...... //写一个移动的方法 this.transform.forward = Vector3.forward * 1; } if (Input.GetKeyDown(KeyCode.S) && Vector3.Angle(Vector3.back, this.transform.forward) < 160f) { this.transform.forward = Vector3.back * 1; } if (Input.GetKeyDown(KeyCode.A) && Vector3.Angle(Vector3.left, this.transform.forward) < 160f) { this.transform.forward = Vector3.left * 1; } if (Input.GetKeyDown(KeyCode.D) && Vector3.Angle(Vector3.right, this.transform.forward) < 160f) { this.transform.forward = Vector3.right * 1; } } void CreateSnackBody() { GameObject CreateSnackBodys = Instantiate(snack_Body, new Vector3(100f, 100f, 100f), Quaternion.identity) as GameObject; if (lastSnackBody != null) { lastSnackBody.GetComponent<CreateBody>().nextSnackBody = CreateSnackBodys; print("lastBody==null"); } if (firstSnackBody == null) { firstSnackBody = CreateSnackBodys; print("firstBody==null"); } lastSnackBody = CreateSnackBodys; } void OnTriggerEnter(Collider other) { if (other.gameObject.tag.CompareTo("food") == 0) { //创建一个身体 Destroy(cc); RandomFood(); CreateSnackBody(); } }}
身体控制
using UnityEngine;using System.Collections;using System.Threading;public class CreateBody : MonoBehaviour{ public GameObject nextSnackBody; void Start() { } void Update() { } public void moveTo(Vector3 pos) { Vector3 old = this.transform.position; this.transform.position = pos;//当前坐标变为pos. if (nextSnackBody != null) { nextSnackBody.GetComponent<CreateBody>().moveTo(old); } }}
阅读全文
1 0
- unity3d 贪吃蛇移动
- unity3d之贪吃蛇
- Unity3D 协程实现贪吃蛇
- unity3d之3D贪吃蛇
- 贪吃蛇身子移动的问题
- 贪吃蛇(移动光标实现)
- 总结反思-贪吃蛇的移动
- Unity 蛇形移动(经典贪吃蛇方式移动)
- 初学Unity3D之制作贪吃蛇(一)
- 贪吃蛇蛇移动代码keil+c+proteus实现
- MFC实现贪吃蛇游戏之蛇的移动
- 基于H5的移动端贪吃蛇游戏开发
- JAVA 贪吃蛇 控制台版 自动移动 set LinkedList实现
- 关于Js实现贪吃蛇的移动的一些感想
- Unity3D-移动
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- javascript内置对象常用属性和方法(笔记三)
- vue项目的开发流程
- 按钮上传文件js
- 机器码和字节码
- 项目组成员开发注意事项
- unity3d 贪吃蛇移动
- Maven 使用Nexus搭建Maven私服
- JAVA设计模式之享元模式
- HDU1013 水题字符串 九余数定理
- bitblt与StretchDIBits
- 一个轻量级的java web开发框架,0配置
- 自媒体淘客的优势
- 搜索方法中的剪枝优化
- 日常笔记