在大一的暑假花了半个多月学习了Unity3D的基础,这是我总结的一些基础知识,希望和大家分享,也希望大家能够提出自己的看法。
组件开发:强调完成这一个事情的对象身上具有的模块功能
- 脚本(代码)也是一个组件
面向过程:强调的完成一件事情的步骤经过,侧重功能的实现步骤
面向对象:强调的是完成一件事情需要参与的对象以及对象分别负责的功能,侧重类的设计
游戏物体:场景中看得见或看不见的物体都叫做游戏物体
Unity当中游戏物体的基本操作:
- Q:手形工具 移动场景视图
- W:平移工具 对游戏物体进行平移操作
- E:旋转工具 对游戏物体进行旋转
- R:缩放工具 对游戏物体进行缩放
- Alt+左键:调整视图视角
- 右键+wasd:场景漫游快捷键
基础知识:
-
材质球:用来显示游戏物体的表面细节,可以给它设置一些贴图纹理
创建方式:到Asset下面右键Creat->Material -
本地坐标系与世界坐标系
世界坐标系:游戏物体在场景当中的位置
本地坐标系:游戏物体在父物体当中的位置
游戏代码相关操作:
- 给游戏物体添加tag值,获取tag
调用transform组件的三种方式
(1)gameObject.GetComponent<Transform>()
(2)gameObject.transform
(3)transform
- GameObject:游戏对象类
(1)GameObject.Find("name")
(2)GameObject.FindGameObjectWithTag(" ")
-
获取一个组件:游戏物体.GetCompenent<组件名>()
-
脚本生命周期
(1)Awake:加载的时候调用一次
(2)OnEnable:激活的时候调用
(3)Stare:脚本运行的时候调用
(4)Update:每帧都调用
(5)LateUpdate:Update执行结束后调用
(6)OnDisable:脚本不激活的时候调用
(7)OnDestroy:脚本销毁的时候调用
-
Time.deltaTime:上一次LateUpdate结束到这次Update开始的时间间隔
-
监听键盘输入
(1)Input.GetKey(KeyCode.X):某个按键X持续按下的时候触发
(2)Input.GetKeyDown(KeyCode.X):某个按键X按下的时候触发
(3)Input.GetKeyUp(KeyCode.X):某个按键X弹起的时候触发
- 监听鼠标输入
(1)Input.GetMouseButtonDown ()鼠标按下
(2)Input.GetMouseButton ()鼠标持续点击
(3)Input.GetMouseButtonUp ()鼠标弹起
- 刚体组件
RigidBody:用来模仿现实世界的物理效果
给刚体组件设置一个速度:velocity
施加爆炸力作用:
//参数1:表示爆炸力大小
//参数2:表示爆炸力施加的点
//参数3:表示爆炸半径
rb.AddExplosionForce (1500, Vector3.zero, 4);
- 预设体
生成预设体:将场景的游戏物体拖拽到Asset当中即生成预设体
根据预设体生成游戏对象:
GameObject.Instantiate(prefab,position,rotattion)
Quaternion.identity:旋转角度都为零
触发器
- 背景制作
飞机制作(移动,发射子弹)
敌机制作
让敌机有规律的产生(1秒产生1只)
设计模式(单例设计模式,观察者模式)
私密了默认构造方法,提供一个对外界可以访问的获取实例的方法
记分系统
重新开始游戏,退出游戏
- 规则:游戏开始的时候10s无敌时间
阵亡的敌机超过30只子弹的伤害加10
敌机是一波一波产生的,每一波的间隔时间为5s,每秒产生1只 第一波为5只 第二波为10只 第三波为15只
这是我做的简单小游戏雷霆战机代码:
快打飞机游戏代码
((EnemyController)):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyController : MonoBehaviour {
float speed=2;
public int HP = 30;
public int damage = 10;
public GameObject explosionPrefab;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.position += Vector3.down * speed * Time.deltaTime;
if (transform.position.y <-6) {
GameObject.Destroy (gameObject);
}
}
void OnTriggerEnter (Collider other){
if (other.gameObject.tag == "Player") {
GameObject.Destroy (gameObject);
GameController.Instance.score += 10;
print (GameController.Instance.score);
GameObject.Instantiate (explosionPrefab, gameObject.transform.position, Quaternion.identity);
other.gameObject.GetComponent<PlayerController> ().HP -= damage;
print (other.gameObject.GetComponent<PlayerController> ().HP);
if (other.gameObject.GetComponent<PlayerController> ().HP <= 0) {
GameObject.Destroy (other.gameObject);
GameObject.Instantiate (explosionPrefab, other.gameObject.transform.position, Quaternion.identity);
}
}
}
//敌机碰到飞机销毁 玩家飞机扣血 判断剩余血量小于等于0,销毁玩家飞机
}
((GameController.cs)):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour {
GameObject zd;
//敌机预设体
public GameObject Enemy;
public int score = 0;
GameObject player;
//玩家飞机预设体
public GameObject playerPrefab;
#region 单例模式
//静态变量
static GameController instance;
//静态变量实例化
void Awake(){
instance = this;
}
//对外界提供一个可以访问静态实例的属性或方法
public static GameController Instance{
get{
return instance;
}
}
#endregion
// Use this for initialization
void Start () {
player=GameObject.Find("Player");
}
// Update is called once per frame
void Update () {
if (player != null) {
GameRule ();
}
}
float timer=0;
float timer2=0;
//游戏规则:0.5秒产生1只敌机
void GameRule(){
timer += Time.deltaTime;
timer2 += Time.deltaTime;
if (timer <= 5) {
if (timer2 >=1f) {
timer2 = 0;
float randomX = Random.Range (-4.5f, 4.5f);
//敌机位置
Vector3 enemyPos = new Vector3 (randomX, 6, -1);
GameObject.Instantiate (Enemy, enemyPos, Enemy.transform.rotation);
}
}
if (timer > 5 && timer <= 10) {
if (timer2 >=0.5f) {
timer2=0;
float randomX = Random.Range (-4.5f, 4.5f);
//敌机位置
Vector3 enemyPos = new Vector3 (randomX, 6, -1);
GameObject.Instantiate (Enemy, enemyPos, Enemy.transform.rotation);
}
}
if (timer > 10&&timer<20) {
if (timer2 >= 0.2f) {
timer2=0;
float randomX = Random.Range (-4.5f, 4.5f);
//敌机位置
Vector3 enemyPos = new Vector3 (randomX, 6, -1);
GameObject.Instantiate (Enemy, enemyPos, Enemy.transform.rotation);
}
}
if (timer > 20&&timer<40) {
if (timer2 >=0.05f) {
timer2=0;
float randomX = Random.Range (-4.5f, 4.5f);
//敌机位置
Vector3 enemyPos = new Vector3 (randomX, 6, -1);
GameObject.Instantiate (Enemy, enemyPos, Enemy.transform.rotation);
}
}
if (timer > 40) {
if (timer2 >=0.01f) {
timer2=0;
float randomX = Random.Range (-4.5f, 4.5f);
//敌机位置
Vector3 enemyPos = new Vector3 (randomX, 6, -1);
GameObject.Instantiate (Enemy, enemyPos, Enemy.transform.rotation);
}
}
}
//持续调用 用于刷新界面UI
void OnGUI(){
//显示分数:
Rect scoreRect=new Rect(Screen.width-130,50,200,80);
GUI.Label (scoreRect, "Score:" + score);
//退出按钮
Rect QuitBtnRect=new Rect(20,50,100,30);
if (GUI.Button (QuitBtnRect, "退出游戏")) {
Application.Quit ();
}
//退出游戏
//中间文本 (200*60)字体大小25
if (player == null) {
//中间文本 (200*60)字体大小25
Rect textRect = new Rect (Screen.width / 2 - 100, Screen.height / 2 - 30, 200, 60);
//文本样式
GUIStyle textStyle = new GUIStyle ();
textStyle.normal.textColor = Color.white;
textStyle.fontSize = 30;
textStyle.alignment = TextAnchor.MiddleCenter;
if (score <=200) {
GUI.Label (textRect, "hh 笑死:" + score,textStyle);
}
if (score >= 200 && score < 500) {
GUI.Label (textRect, "小垃圾:" + score,textStyle);
}
if (score >= 500 && score <1000) {
GUI.Label (textRect, "一般般喽:" + score,textStyle);
}
if (score >=1000 && score < 1500) {
GUI.Label (textRect, "再多一点就超过你哥了:" + score,textStyle);
}
if (score >=1500) {
GUI.Label (textRect, "真的能到这??老司机,手速六 :" + score,textStyle);
}
Rect ReStartBtnRect = new Rect (Screen.width / 2 - 50, textRect.y + 70, 100, 20);
if (GUI.Button (ReStartBtnRect, "ReStart!!")) {
//点击了重新开始按钮:生成玩家飞机 分数清零
Vector3 playerPos=new Vector3(0,-4.5f,-1);
player=GameObject.Instantiate (playerPrefab, playerPos, playerPrefab.transform.rotation);
score = 0;
timer = 0;
timer2 = 0;
}
}
}
}
((PlayerController.cs)):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
AudioSource shootAS;
float speed=8;
//子弹预设体
public GameObject bulletPrefab;
public Transform bulletTransform;
public int HP=30;
//控制飞机左右上下移动 速度8
// Use this for initialization
void Start () {
//获取射击的音频片段
shootAS = gameObject.GetComponent<AudioSource> ();
}
// Update is called once per frame
void Update () {
GameRule ();
Mobe ();
Shoot ();
}
float timer=0;
//游戏规则:前10s无敌
void GameRule(){
timer += Time.deltaTime;
if (timer <5f) {
HP = 30;
}
}
void OnGUI(){
if (timer < 5f) {
Rect textRect = new Rect (Screen.width / 2 - 100, Screen.height / 2 - 30, 200, 60);
//文本样式
GUIStyle textStyle = new GUIStyle ();
textStyle.normal.textColor = Color.blue;
textStyle.fontSize = 30;
textStyle.alignment = TextAnchor.MiddleCenter;
GUI.Label (textRect, "无敌时间" + (5 - (int)timer), textStyle);
}
}
void Mobe(){
float vertical = Input.GetAxis ("Vertical");
float Horizontal = Input.GetAxis ("Horizontal");
Vector3 direction = new Vector3 (Horizontal, vertical, 0);
transform.position += direction * speed * Time.deltaTime;
//transform.Translate (Vector3.back * speed * vertical * Time.deltaTime);
//transform.Translate (Vector3.left* speed * Horizontal * Time.deltaTime);
//限定飞机位置x(-4.5,4.5) y(-4.5,3)
Vector3 tempPos = transform.position;
tempPos.x = Mathf.Clamp (tempPos.x, -4.5f, 4.5f);
tempPos.y = Mathf.Clamp (tempPos.y, -4.5f, 4f);
transform.position = tempPos;
/*if(transform.position.x<-4.5f){
Vector3 tempPos = new Vector3 (-4.5f, transform.position.y, transform.position.z);
transform.position=tempPos;
}
if(transform.position.x>4.5f){
Vector3 tempPos2 = new Vector3 (4.5f, transform.position.y, transform.position.z);
transform.position= tempPos2;
}
if(transform.position.y<-4.5f){
Vector3 tempPos3 = new Vector3 ( transform.position.x, -4.5f
,transform.position.z);
transform.position=tempPos3;
}
if(transform.position.y>4f){
Vector3 tempPos4 = new Vector3 (transform.position.x,4f, transform.position.z);
transform.position=tempPos4;
}*/
}
void Shoot(){
if (Input.GetKeyDown(KeyCode.J)) {
//使用音频组件播放音频片段
shootAS.Play();
GameObject.Instantiate (bulletPrefab, bulletTransform.position, bulletPrefab.transform.rotation);
}
}
}
((RocketController.cs)):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RocketController : MonoBehaviour {
//让子弹向y正飞行 速度13
float speed=15;
public int damage = 10;
public GameObject explosionPrefab;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.position += Vector3.up * speed * Time.deltaTime;
//超出一定范围后立即销毁
if (transform.position.y > 6) {
GameObject.Destroy (gameObject);
}
}
void OnTriggerEnter(Collider other){
GameObject.Destroy(gameObject);
GameObject.Instantiate (explosionPrefab, transform.position, Quaternion.identity);
other.gameObject.GetComponent<EnemyController> ().HP-=damage;
if (other.gameObject.GetComponent<EnemyController> ().HP <= 0) {
GameObject.Destroy (other.gameObject);
//累加分数
GameController.Instance.score+=10;
print (GameController.Instance.score);
GameObject.Instantiate (explosionPrefab, other.transform.position, Quaternion.identity);
}
}
}
((StarBGController.cs)):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StarBGController : MonoBehaviour {
//控制背景往y轴负方向移动,速度为1
//当第一个看不见时放置到最上面去
float moveSpeed=1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.position += Vector3.down * moveSpeed * Time.deltaTime;
if (transform.position.y <= -10) {
Vector3 newPos = new Vector3 (transform.position.x, 9, transform.position.z);
transform.position = newPos;
}
}
}
希望大家多多指教。
相关专业知识推荐
- 博客:简书
- github:全球最大的代码托管仓库
- 拉勾网 薪资 boss直聘
- 编程语言排行榜
- 前端:ios android h5 Unity3d 开发语言 UI 网络 线程 全栈工程师
网友评论