记录步骤:win+R→PSR.exe
一、竖屏设置
分辨率设为9:16
二、主控脚本
- 添加一个空节点,命名“游戏主控”
- 新建游戏的主控脚本,命名为MyGame.cs,方便管理(即,一些全局性的逻辑控制)
- 如有必要,修改主控脚本的Execution Order(优先级)
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class MyGame : MonoBehaviour 6 { 7 //添加全局性的设置 8 // Start is called before the first frame update 9 void Start() 10 { 11 Application.targetFrameRate = 60;//帧数设为60 12 } 13 14 // Update is called once per frame 15 void Update() 16 { 17 18 } 19 }
三、制作子弹
- 制作预制体

子弹运动:
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class Mybullet : MonoBehaviour 6 { 7 public float speed = 3.5f; 8 // Start is called before the first frame update 9 void Start() 10 { 11 12 } 13 14 // Update is called once per frame 15 void Update() 16 { 17 float dy = speed * Time.deltaTime; 18 transform.Translate(0, dy, 0, Space.Self); 19 Vector3 sp = Camera.main.WorldToScreenPoint(transform.position); 20 if (sp.y > Screen.height) 21 { 22 Destroy(this.gameObject); 23 } 24 }
四、制作飞机
- 控制飞机左右移动
- 添加预制体资源:public GameObject myPrefab;
- 断开预制体:unpack Prefab
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class Myplane : MonoBehaviour 6 { 7 public GameObject MyPrefab; 8 private float interval=0.4f;//间隔时间 9 private float count = 0; 10 11 // Start is called before the first frame update 12 void Start() 13 { 14 Application.targetFrameRate = 60; 15 } 16 17 // Update is called once per frame 18 void Update() 19 { 20 count += Time.deltaTime; 21 if (count >= interval) 22 { 23 count = 0; 24 Fire(); 25 } 26 //键盘控制飞机的左右移动 27 float step = 2.5f * Time.deltaTime; 28 if (Input.GetKey(KeyCode.LeftArrow)) 29 { 30 transform.Translate(-step, 0, 0); 31 } 32 if (Input.GetKey(KeyCode.RightArrow)) 33 { 34 transform.Translate(step, 0, 0); 35 } 36 } 37 void Fire() 38 { 39 GameObject bullet = Instantiate(MyPrefab); 40 bullet.transform.position = transform.position + new Vector3(0, 1f, 0); 41 } 42 }
五、制作怪物
- 添加到游戏主控MonsterCtrl.cs
- 不断生成
- 随机头像
- 大小优化
- 随机位置
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class Mymonster : MonoBehaviour 6 { 7 public float speed = 1.0f; 8 // Start is called before the first frame update 9 void Start() 10 { 11 12 } 13 14 // Update is called once per frame 15 void Update() 16 { 17 float dy = speed * Time.deltaTime; 18 transform.Translate(0, -dy, 0,Space.Self); 19 Vector3 sp = Camera.main.WorldToScreenPoint(transform.position); 20 if (sp.y <0) 21 { 22 Destroy(this.gameObject); 23 } 24 } 25 }
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class MonsterCtrl : MonoBehaviour 6 { 7 public GameObject monsterPerfab; 8 9 public Sprite[] images; 10 11 // Start is called before the first frame update 12 void Start() 13 { 14 InvokeRepeating("CreatMonster", 0.1f, 2f); 15 } 16 17 // Update is called once per frame 18 void Update() 19 { 20 21 } 22 23 void CreatMonster() 24 { 25 float x = Random.Range(-2, 2); 26 float y = 5; 27 GameObject monster = Instantiate(monsterPerfab); 28 monster.transform.position = new Vector3(x, y, 0); 29 30 31 //随机怪物 32 int index = Random.Range(0, images.Length); 33 34 SpriteRenderer renderer = monster.GetComponent<SpriteRenderer>(); 35 renderer.sprite = this.images[index]; 36 37 //怪物大小设为100px(像素) 38 Sprite sprite = this.images[index]; 39 float imgWidth = sprite.rect.width; // 图像的实际宽度 40 float scale = 100/imgWidth; //缩放比例 41 monster.transform.localScale = new Vector3(scale, scale, scale); 42 } 43 }
六、击中目标
- “子弹”与“怪物”之间的碰撞处理:
- 添加Rigidbody 2D及Collider 2D
- 添加Tag:player,Bullet,Monster(用于身份识别)
- 修改MyBullet.cs,添加碰撞处理
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class Mybullet : MonoBehaviour 6 { 7 public float speed = 3.5f; 8 // Start is called before the first frame update 9 void Start() 10 { 11 12 } 13 14 // Update is called once per frame 15 void Update() 16 { 17 float dy = speed * Time.deltaTime; 18 transform.Translate(0, dy, 0, Space.Self); 19 Vector3 sp = Camera.main.WorldToScreenPoint(transform.position); 20 if (sp.y > Screen.height) 21 { 22 Destroy(this.gameObject); 23 } 24 } 25 private void OnTriggerEnter2D(Collider2D collision) 26 { 27 if (collision.tag.Equals("Monster")) 28 { 29 Destroy(collision.gameObject); 30 Destroy(this.gameObject); 31 } 32 } 33 }
七、游戏背景
- 添加到游戏主控background.cs
- Order in Layor:-10
- 俩图片无缝连接:Deplicate(复制)
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class background : MonoBehaviour 6 { 7 8 Transform bg1; 9 Transform bg2; 10 11 public float speed = 1.0f;//向下运动速度 12 // Start is called before the first frame update 13 void Start() 14 { 15 //找出变化路径 16 bg1 = GameObject.Find("/背景/bg1").transform; 17 bg2 = GameObject.Find("/背景/bg2").transform; 18 19 //手动确定背景图片的位置从而无缝连接 20 bg1.position = new Vector3(0, 0, 0); 21 bg2.position = new Vector3(0, 10, 0); 22 } 23 24 // Update is called once per frame 25 void Update() 26 { 27 float dy = speed * Time.deltaTime; 28 bg1.Translate(0, -dy, 0); 29 bg2.Translate(0, -dy, 0); 30 //图片循环显示 31 if (bg1.position.y <= -10) 32 { 33 bg1.Translate(0, 20, 0); 34 35 } 36 if (bg2.position.y <= -10) 37 { 38 bg2.Translate(0, 20, 0); 39 } 40 } 41 }
- 物理系统:Physics
由物理规律起作用的系统:刚体(RigidBody)→指物理系统中的物体(具有质量、速度、可反弹...)
Add Component→Physics 2D→Rigidbody 2D
刚体有三种类型:
Dynamic:普通刚体,有质量,有速度
Static:静态刚体,质量无穷大,无速度(适用于建筑物、地面等固定不动的物体)
Kinematic:运动学刚体,无质量(忽略物理规律的刚体,一般用于碰撞检测)
只有添加撞体(Collider)组件后,才有碰撞的计算
Add Component→Physics 2D→Box Collider 2D
-
- 方形:Box Collider 2D
- 圆形:Circle Collider 2D
- 不规则边缘:Edge Collider 2D
- 胶囊形状:Capsule Collider 2D
刚体的反弹:
1.在project窗口里,Create→Physics Material 2D...
-
- Friction:摩擦系数
- Bounciness:弹性系数,设为1(完全反弹(无能量损耗))
2.选择“皮球”的Rigidbody2D组件
-
- Material:选择....材质
3.运行游戏,观察反弹
我一直想从你的窗户里看月亮。
浙公网安备 33010602011771号