SPACE SHOOTER TUTORIAL

开始时间:2016年7月31日21:37:37

参考链接:http://unity3d.com/learn/tutorials/projects/space-shooter-tutorial

The player GameObject

add Rigidbody Component

To detect collision, the physics engine, through the rigid body, needs to know the volume of our objects.We need to know how much space these objects take up in our game to calculate the collisions. We give this information to the rigid body by using a cage that we wrap around our game objects. This cage defines the volume of that object. The cage is called a Collider.

add CapsuleCollider Component

remove it

add MeshCollider Component, select "Convex"

The Mesh Collider component will not participate properly in physics
collisions and will not be visible in the scene view unless we select
“Convex” on the Mesh Collider Component.
There are some limitations when using the Mesh Collider. Non-convex Mesh Colliders are only supported on GameObjects without a rigidbody. If you want to use a Mesh Collider on a rigidbody, it needs to be marked as Convex.

select "Is Trigger"

We simply need our collision to trigger an action.

打卡:2016年8月1日21:25:18

CAMERA AND LIGHTING

Set Camera

  • Our game needs to feel like an upright arcade game. These did not have any perspective. So we will choose orthographic as our projection mode.
  • set Size to 10
  • We want the player in the origin, so we move the camera to let player at the bottom at the start.

by default in Unity 5, a Skybox is included in thescene. This skybox will influence the ambient light affecting the shipand will be used as the background image by the Main Camera.

Set Light
main light, a fill light, a rim light

We can organize our hierarchy by using empty Game Objects.
And It is important to reset the transform of this empty game object.

打卡:2016年8月2日14:08:19(昨天看的有点少)

Adding a background

add a quad object, rotate, remove mesh collider component.

What is a matte paint?
Glossy and flat (or matte) are typical extreme levels of glossiness of a finish. Glossypaints are shiny and reflect most light in the specular (mirror-like) direction, while onflat paints most of the light diffuses in a range of angles. The gloss level of paintcan also affect its apparent colour.

  1. Mesh Filter holds the mesh data: Quad.
  2. The Mesh Renderer renders that mesh using the materials in the Mesh Renderer's Materials Array.
  3. The renderer is only able to use a Texture if it's a part of a Material.
    In this case, we did not create a material, we simply dragged the texture on the Quad. It was Unity that created the Material for us.
  4. For our Background, let's change the Shader. Let's choose Unlit/Texture for the Shader on the nebula materail. Now our Background is independent of our lighting system and it display the texture exactly as it looks in the original image and it uses no lights at all.

move the background down.

读了Lighting Overview

Moving the Player

add Script named PlayerController.cs to Player GameObject

"camelCase" isn't PascalCase, but "PascalCase" is.

use Input.GetAxis() to 得到运动的分量 来 设置 rb.velocity() 从而使飞船运动。

设置飞船运行的限制区域
学习 Class Boundary 的用法

Mathf - A collection of common math functions.

Mathf.Clamp(float value, float min, float max)
将value限制在 min 和 max 之间

设置 飞船的 tilt,让飞船在左右移动的时候有适当的倾斜。

Quaternion Euler(float x, float y, float z)
Returns a rotation that rotates z degrees around the z axis, x degrees around the x axis, and y degrees around the y axis (in that order).

Creating shots

Creating shots by assembling artwork, physics components and custom C# code.

  1. Create a Object named Bolt, this will be the parent object for our shot.

We are going to separate the game logic from our visual effect of the shot. This will allow us to easily make new weapons with different visual effects by reusing the parent game object with the logic and replacing the visual effect layer.

  1. Create a Quad VFX to hold the visual effect image. Add the VFX game object as a child of Bolt.

  2. Create a Material fx_bolt_orange for the Quad VFX. Drag the Material on to the Quad.

  3. Look at Quad VFX game object, We Change the shader on the material fx_bolt_orange to Particles - Additive

With shader particles/additive, black has a value of 0 and will add nothing to the scene. And white has a value of 255 on all channels and will add full white to the scene. All of the other colours will be added on top of the existing background. This will give us a strong, hot laser bolt.

We can also change Shader to mobile/particle/additive.
In general the mobile shader will be more efficient with our game's resource budget, but in some cases may sacrifice either quality or control. The main control that we will lose by using this mobile shader is the ability to change the tint colour, which we don't need on our laser bolt.

with our visual effect set up, let's move on to setting up our logic.也就是说配置 Bolt 对象。

  1. Add rigid body component to Bolt game object.
  2. Remove collider component of the VFX game object.
  3. Add Capsule Collider to Bolt game object. And adjust it's size and orientation.
  4. Click is trigger of this collider to make this collider a trigger collider.
  5. Add Mover.cs component to Bolt game object.
  6. our player is going to shoot many copies or clones of this shot, so let's save this game object as a prefab. And we set the speed value in this prefab not the INSTANCE in the scene.
  7. delete our working instance of Bolt from the scene.
  8. to test the Bolt, as we don't have any shooting code, we simply drag the copies of the prefab into the hierarchy window while the game is running.

Shooting shots

Writing the code and setting up the scene to shoot shots.

What we need to do is instantiate a copy of clone of this Shot prefab when we hit a button or click a mouse during our gameplay.

  1. create a empty game object named Shot Spawn. We can use this empty game object's transform as a spawn point in our game. This spawn point should move with our player ship. So let's drag Shot Spawn on to our player game object and drop it as a child. As it's a child of the player ship, our Shot Spawn's position will be relative to the player ship.
  2. drag the Shot Spawn out along it's Z axis until it's in front of the ship.

打卡:2016年8月3日20:29:59

Boundary

Creating a bounding box to destroy any object that leaves the game area.

We are going to create a box around our game and we will destroy these shots as they leave the box.(所以我们使用 OnTriggerExit(Collider))

注:The number of units from the top of the screen to the bottom is always twice the value of our camera's orthographic size.

Creating hazards

Create an asteroid hazard to challenge the player.

注:We will have a parent game object for the logic and the artwork will be a child.

让小行星随机旋转用到 Random.insideUnitSphere
Returns a random point inside a sphere with radius 1 (Read Only).

Set Angular Drag value to 0. 避免小行星停止自转。

Write trigger collider code for our 2 colliders to have any effect.

我们发现小行星由于和Boundary接触而消失,为了避免这一效果,tag our boundary.

// DestroyByContact.cs
using UnityEngine;
using System.Collections;

public class DestroyByContact : MonoBehaviour
{ 
    void OnTriggerEnter(Collider other)
    {
        // Debug.Log(other.name);
        if(other.tag == "Boundary") // 当 other 的 tag 是 Boundary 的时候,跳过
        {
            return;
        }
        Destroy(other.gameObject);  // Destroy the laser bolt when it hits the asteroid.
        Destroy(gameObject);        // Destroy the asteroid itself.
    }
}

Explosions

Add explosions to the scene when hazards or the player is destroyed.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容

  • 因为data js一般会在document load之后加载,所以需要先将js代码执行后才可以做html pars...
    肖雨San阅读 2,731评论 0 1
  • 莫言得诺贝尔文学奖那年,我开始读莫言的小说。从《丰乳肥臀》到《檀香刑》,我几乎读完了他的所有小说。每部小说都是走马...
    凤之子阅读 247评论 0 1
  • 达尔文进化论提出:物尽天泽,优胜劣汰,适者生存。一次无意间听到还有一句:随机应变。往往幸存的不是最强大或是最顺从,...
    君未见阅读 487评论 0 2