自己做的一个超级简单的小游戏
自己做了一个简单的uniy3d小游戏
方向键控制一个小球躲避别的小球,撞到指定的墙胜利,被别的球撞到失败。
初学者练手
Player
- move
Enemy
- move
- Islose_Trigger
- Spawn
- Camera
- follow_player
- UI
Wall
- IsWin
游戏图片
代码
player
player_move 控制小球移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class playermove : MonoBehaviour {public float speed = 20f; Vector3 movement; Rigidbody playerRigidbody;void Awake(){playerRigidbody=GetComponent<Rigidbody>(); //获取player的Rigidbody}void FixedUpdate(){float h = Input.GetAxisRaw("Horizontal");float v = Input.GetAxisRaw("Vertical");move(h,v);}void move(float h,float v){movement.Set(h,0f,v); //移动的方向movement = movement.normalized * speed * Time.deltaTime;//normalized :将向量长度归一transform.position = movement + transform.position; playerRigidbody.MovePosition(transform.position);}/* 手机中重力感应控制小球行动(不知道能不能实现)void Update(){Vector3 dir = Vector3.zero;dir.x = -Input.acceleration.y;dir.z = Input.acceleration.x;if (dir.sqrMagnitude > 1)dir.Normalize();dir *= Time.deltaTime;transform.Translate(dir * speed);}*/
}
Camera
Camerafollow 摄像机跟随小球
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class camerafollow : MonoBehaviour {public Transform target;public float speed = 5f;Vector3 offset;void Awake(){offset = transform.position - target.position;}void FixedUpdate(){Vector3 targetcampos = target.position + offset;transform.position = Vector3.Lerp(transform.position,targetcampos,speed*Time.deltaTime);}
}
Enemy
Enemy_move
敌人移动(跟随小球)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class enemymove : MonoBehaviour {Transform player; UnityEngine.AI.NavMeshAgent nav;
//要在unity编辑器中给enemy设置NavMeshAgentvoid Awake(){player = GameObject.FindGameObjectWithTag("Player").transform;nav = GetComponent<UnityEngine.AI.NavMeshAgent>();}// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {nav.SetDestination(player.position); //给敌人设置自动寻路的目标}
}
Islose
如果player撞到enemy,显示lose,并且将playermove设置为false
Enemy的碰撞器上的IsTrigger打开
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class Islose : MonoBehaviour {public GameObject player;playermove playermove;Text text;// Use this for initializationvoid Awake(){text = GameObject.Find("Canvas/Text").GetComponent<Text>();playermove = player.GetComponent<playermove>();}// Update is called once per framevoid Update(){}void OnTriggerEnter(Collider other)//如果撞到则。。。{if (other.gameObject == player){text.text = "LOSE!";playermove.enabled = false;}}}
Spawnenemy
生成敌人
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class spwanenemy : MonoBehaviour {public GameObject enemy; //敌人public float spawntime = 3f; //生成速度public Transform[] spawnpoints;//敌人刷新点// Use this for initializationvoid Start () {InvokeRepeating("Spawn",spawntime,1f);//InvokeRepeating("函数名称",调用时间,调用间隔)//InvokeRepeating函数用来重复调用某一函数}void Spawn(){int spawnPointIndex = Random.Range(0, spawnpoints.Length);Instantiate(enemy, spawnpoints[spawnPointIndex].position, spawnpoints[spawnPointIndex].rotation);}}
Wall
IsWin
小球撞到指定的墙上,显示Win
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; //导入UI包public class Iswin : MonoBehaviour {public GameObject player;playermove playermove;Text text;void Awake(){text = GameObject.Find("Canvas/Text").GetComponent<Text>();playermove = player.GetComponent<playermove>();}void Update () {}void OnTriggerEnter(Collider other){if (other.gameObject == player){text.text = "WIN!";playermove.enabled = false;}}}
代码参考官方教程
新手练手,还请大拿多多指教
自己做的一个超级简单的小游戏相关推荐
- cmd上写的java简单代码_用cmd编辑一个超级简单的小游戏,求代码
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 贪吃蛇: import java.awt.*; import java.util.LinkedList; import java.util.Scanner ...
- c#推箱子小游戏代码_C#做的一个推箱子的小游戏
C#做的一个推箱子的小游戏 c# 2021-1-6 下载地址 https://www.codedown123.com/59125.html 本推箱子游戏使用数组实现,用不同的数字标识不同的物体,从而简 ...
- 用Python做一个超简单的小游戏(一听就懂)
前言 有的人可能学过一点Python基础,但不知道干什么好.今天就教大家做一个简单的小游戏 未经允许,禁止转载 文章目录 前言 写它会用到 源码先抛出来 知识讲解 random 介绍 语法 方法参数表 ...
- 用java做一个超级马里奥的小游戏
好的,首先你需要准备一些基本的知识和工具: 了解 Java 语言的基本语法和编程概念. 安装好 Java 开发环境,比如 Eclipse 或者 IntelliJ IDEA. 准备好一些图像和音频资源, ...
- 【c语言】 我使用c语言基础做了一个老少皆宜的”国民小游戏(三字棋)“
C语言实现三字棋小游戏 前言 游戏效果 游戏实现 前言 本三字棋小游戏是依靠二维数组为核心来实现的,可以更加好理解掌握c语言数组的概念知识,依靠做小游戏项目,把学到了知识在输出出来加已巩固,最后有源代 ...
- java用二维数组编写地图_[Java] Java二维数组写一个超级简单的扫雷游戏,适合新手...
直接上代码//随机生成地雷数 int numOfMines=10; //地图尺寸 int mapSize=9; Random r=new Random(); //用二位数组做地图 int [][] m ...
- 超级简单五子棋小游戏(含代码)
五子棋简单功能实现 游戏功能演示 代码如下: #include <stdio.h> #include <stdlib.h> #include <stdbool.h> ...
- c++超级简单的小游戏(下)
来,我们接着搞双人对战 我也懒得写了 稍微改一下就好了 //头文件懒得写了自行脑补吧 int main(){int myblood = 100;//我的血量int hisblood = 100;//敌 ...
- 自己做的一个炸碉堡的小游戏(SWING)
2019独角兽企业重金招聘Python工程师标准>>> import java.awt.event.KeyAdapter; import java.awt.event.KeyEven ...
最新文章
- IDEA IntelliJ 如何设置网站的欢迎页面
- RTMPdump(libRTMP) 源代码分析 3: AMF编码
- matlab对图像信号进行频谱分析及滤波,数字信号处理课程设计---应用 Matlab对信号进行频谱分析及滤波...
- Android 中查看内存的使用情况集经常使用adb命令
- Linuxnbsp;JDK1.4卸载与1.6的安装
- java8 虚拟机调优_Java虚拟机调优(八)-典型配置举例2
- 线性代数笔记:Kronecker积
- 我的申请总结~好像创业公司啊
- java 强制向上转型,Java 转型(向上或向下转型)详解及简单实例
- matlab解带参数的积分方程组,方程组求解问题:方程组中有带参数的积分函数,求参数...
- 邓迎春绘画201702作品10
- How to change context root of a dynamic web project in Eclipse
- 绿联扩展坞拆解_拆解报告:米物3A1C七合一多功能扩展坞
- 细粒度图像分类_基于多尺度拼图块的细粒度图像分类
- c语言自动任务,【C语言训练】委派任务* (C语言代码)
- 329 矩阵中的最长递增路径
- 提高数据库查询效率的八个方法
- 一文详解:为什么隐私智能合约是Web3的未来
- chrome浏览器中使用adblockplus拦截广告
- 物联网操作系统再思考-Hello China操作系统的运营商网络协同机制
热门文章
- Python实现ALO蚁狮优化算法优化支持向量机回归模型(SVR算法)项目实战
- win 8 安装
- 区块链三加一 “成才”路上提个醒:区块链培训机构鱼龙混杂
- Coding git@e.coding.net: Permission denied (publickey)
- 2022各大厂商护网面试题
- 虚拟串口及其在串口转以太网中的应用
- 用python中re.sub()替换文件中指定字符串
- 老杨说运维 | 智能化告警在全面可观测性中的重要性
- jre_linux_64,jre linux 64下载_jdk7u79_linux_x64 免费版 1.0_零度软件园
- 安卓逆向从入门到嗝屁之一道入门级的CTF题目