Unity 2.Space Shooter(碰撞器Collider与安装WebGL,设置刚体属性,动态实例化游戏对象)

版权声明:本文为博主原创文章,转载请声明原创网址。 https://blog.csdn.net/lagoon_lala/article/details/83933487

目录

项目介绍

WebGL发布

游戏对象设置

灯光、相机

背景

移动游戏对象

Debug

制作镜头


官方视频翻译

中英双字]Unity官方教程

Space Shooter

http://v.qq.com/vplus/4348d5936e9f34e564caa14b2714e050

https://id.tudou.com/i/UMTY3MTM2OTY1Mg==/videos?order=1&page=3&last_item=&last_pn=2&last_vid=580591452

项目介绍

设置基础游戏对象:basic player game object

设置与其他对象的互动:move.shoot.interact

设置场景的相机、灯光、背景(background)

Setup game object,附加预制组件(attach pre-made components)

使用模型(models),artwork

场景设置完成后,C#捕获玩具数据、移动

制作激光器(lazer bolts)陨石(hazard)

游戏控制器(game controller)投掷陨石、计算得分

结束游戏、重新开始时间

添加音频效果(audio effects)、背景音乐(background music track)

在web平台上传build and deploy the game to the web

添加滚动背景scroll the background

添加星域field

WebGL发布

新建项目保存主场景(main scene)、设置基础架构(basic foundations)

https://v.qq.com/x/page/b0752yhy8bn.html

new projects,设置location

导入资料包import package of assets

Web Player在几年前就被弃用了。如果要在浏览器中播放,请使用WebGL选项

安装WebGL时不能更改安装文件夹,只能在与Unity同文件夹下,否则报错failed to locate Unity.exe。安装成功后需要重启Unity

更改target后,会在右侧显示Unity图标,并在顶部显示目标平台

在Build Setting点击Player Setting,或Edit-project Settings-Player。在右侧Inspector设置项目和平台。更改分辨率:Resolution

在Game窗口设置窗口形式(纵横比)

右上角layout-save layout保存编辑器布局

游戏对象设置

添加Ship models(在models-vehicle_playerShip)

让场景对准对象(聚焦场景视图相机):F或Edit-Frame Selected或双击层次目录下的物体

Origin三维场景的中心-reset

组件:游戏对象使用网格过滤器(Mesh Filter)网格渲染器(Mesh Renderer)、材料

使用physics、刚体组件

笼子(Collider碰撞器)定义对象体积:

Physics-Capsule Collider(胶囊碰撞器)由两个球体(与它们之间的空间)定义。计算碰撞占用空间。改变碰撞器方向大小

点击右上角轴,改变视角方向

Capsule Collider(胶囊碰撞器)、Box Collider、Sphere Collider、Mesh Collider(网格碰撞器-性能较低)、Compound Collider复合碰撞器

在原始(primitive)碰撞器无法满足、复合碰撞器无法使用时可用网格碰撞器

对撞机组件不再取代现有对撞机组件。在添加网格碰撞器之后,请手动删除手动胶囊碰撞器组件使用

网格碰撞器的绿线隐藏在渲染网格下方,隐藏renderer可看到,网格碰撞器最好使用简化的网格。其中的Mesh引用该对象的网格(Mesh filter),可用其他网格代替

Update: Please check 'Convex' on the Mesh Collider component, otherwise the Mesh Collider will not participate in physics collisions and will not be visible. 选上Convex,否则网格不可见

打开Model文件(展开),选择Mesh Asset

调整碰撞器设置,is Trigger。检测碰撞触发动作

For Unity 5, we must also select "Convex" on the Mesh Collider, in order for the Mesh Collider to work correctly with the updated Physics Engine in Unity 5.必须选Convex,物理引擎才能使用

添加装置:Prefabs-VFX-发动机

场景在工具栏选择Gizmos,拖动减小装置大小

参见刚体文档

http://docs.unity3d.com/Documentation/Components/class-Rigidbody.html?_ga=2.209081493.667635352.1541845822-2004125961.1541845822

灯光、相机

在层级目录点击相机,可以在Scene视图看到摄像机小窗预览

相机在游戏中通过组件控制

相机类型:Perspective透视orthographic正交投影

透视可以改变视野(Field of View)大小

正交可以改变投影尺寸

相机背景:Clear Flags

默认为skybox(天空)。Solid Color纯色

They skybox can be found in the "Lighting" tab under "Window/Lighting".

场景默认创建directional light

Please delete the GameObject "Directional Light".

设置在Edit-Render Setting或 lighting panel: Window > Lighting : Scene Tab.

Please remove the Skybox by selecting it and deleting it.

Make sure the Skybox is "None".(这样即使选择天空盒,相机看到的背景也是纯色)

光源种类:Ambient Light(没有点光源,没有方向)可改变颜色

key light、fill light、rim light组成的3点光源系统

确认默认自带的Directional Light已经删除

设置在:Directional Lights are now found under: Create/Light/Directional Light

方向(Orientation):reset rotation

改变光强Intensity

使用空游戏对象组织场景:空对象应当在原点(重置)

光源可以照亮整个场景,利用方向而不是光源位置

背景

创建背景图像:Create/3D Object/Quad(四边形)

添加纹理(Textures)点击查看图片导入器,拖动查看preview栏,底部为分辨率等信息

纹理拖拽到quad,自动创建material,quad其中的material引用图片,网格过滤器保存网格数据,网格渲染器只能使用材料中的纹理部分。(拖拽纹理到Mesh上时如果找不到Material,则自动创建)

材料着色器(shader):漫反射(Diffuse)-如同哑光涂料

Standard Shader set to default opaque.默认为不透明着色器

可以使用图层,单独制作不影响其他物品的光源

参见图层教程视频:https://unity3d.com/cn/learn/tutorials/topics/interface-essentials/layers

Specular镜面着色器

可编写自定义着色器

无照亮:Unlit-Texture独立于照明系统,完全与原始图像纹理一致

移动游戏对象

添加脚本组件

access components directly using "GetComponent".

脚本名称应以大写字母开头

物理移动对象:使用FixedUpdate(执行物理计算前调用)

Input.GetAxis获取对象轴的输入值,默认为预设轴,只返回0~1的值

刚体组件包括属性:UseGravity,Mass,Drag

we can pole, affect and manipulate

通过找到刚体组件,改变对象速度v:rigidbody.velocity速度为三维向量

三维向量vector3由xyz方向浮点数构成(y上下,x-Horizontal,z-Vertical)

浮点数表示:0.0f

保证船只在视野范围内:通过限制位置position。允许同句多行,但要保持缩进

文档中脚本参考快捷键不好用

Mathf:使用Epsilon、Pi、infinite等。Clamp钳制变量值在给定范围内

定义class,复用代码。:冒号继承

序列化serialised:存储、传递信息。序列化后可在Inspector中看到属性。

在class声明上方[System.Serializable]

在游戏界面拖动对象位置即可估出边界值

在移动时倾斜对象:XXX.rotation = Quaternion.Euler(x,y,z);

Debug

the shorthand reference "rigidbody" is obsolete. Please use the cached reference or GetComponent<Rigidbody>()

您不能再像过去那样直接访问附加到GameObject的组件。你现在必须使用GetComponent

原版为:rigidbody.xxx。改为声明rigidbodyxxx用Rigidbody rigidbodyxxx。(rigidbody = GetComponent<Rigidbody> ();

且声明不能作为初始化实例变量,应在方法体内部写。

原版为:renderer.XXX;改为使用GetComponent获取渲染器 GetComponent<Renderer>().xxx

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class Boundary{
	public float xMin, xMax, zMin, zMax;
}

public class PlayerController : MonoBehaviour {
	public float speed;
	public Boundary boundary;
	public float tilt;
	void FixedUpdate(){
		float moveHorizontal = Input.GetAxis ("Horizontal");
		float moveVertical = Input.GetAxis ("Vertical");
		Vector3 movement = new Vector3 (moveHorizontal,0.0f,moveVertical);
		Rigidbody playerRigidbody=GetComponent<Rigidbody> ();
		playerRigidbody.velocity = movement*speed;

		playerRigidbody.position = new Vector3 (
			Mathf.Clamp(playerRigidbody.position.x,boundary.xMin,boundary.xMax),
			0.0f,
			Mathf.Clamp(playerRigidbody.position.z,boundary.zMin,boundary.zMax)
		);
		playerRigidbody.rotation = Quaternion.Euler(0.0f,0.0f,playerRigidbody.velocity.x*-tilt);
	}
}

制作子弹

通过重用父游戏对象逻辑和视觉效果层,从镜头上分离视觉效果

建立四边形,添加激光纹理(通过新建Material)

关联材料:

As this Shader is now the Standard Shader, there are more Properties and different Properties available.

For the purposes of this lesson, the MainTexture field is now the Albedo field.

在材料属性中选择纹理或拖拽纹理到属性栏

将制作好的材料拖到场景框或对象属性-渲染中的材料框

修改为明亮的着色器:particles-Additive(粒子-添加器)该着色器中,黑色的值为0,不会添加内容。或移动着色器mobile-xxx(通过资源预算提高效率,牺牲质量与改变颜色的能力)

为父容器添加刚体组件

删除quad自带的Mesh Collider网格碰撞器,在父对象中添加胶囊(capsule)碰撞器(由两个半球定义)改变胶囊方向Direction

选择Is Trigger,触发器

激光:生成、具有速度

Start:在对象实例化的第一帧执行

Z轴移动也为前进后退transform.forward

临时创建对象实例

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Mover : MonoBehaviour {
	public float speed;
	// Use this for initialization
	void Start () {
		Rigidbody rigidbody = GetComponent<Rigidbody> ();
		rigidbody.velocity = transform.forward*speed;
	}
}

射击动作

实例化Shot预制件的克隆副本

不需要物理计算,放在Update

实例化Instantiate需要对象、设定位置和旋转

位置position、旋转(rotation-euler)包含在transform组件

使用空对象作为实例的产生(attach)点-随着船对象一起移动

变量可以使用变换transform的类型而不是GameObject,仍然可以使用游戏对象作为Inspector中的引用对象。声明变量的类型为transform,也可以找到并使用转换组件

当玩家按下按钮时实例化(fire),限制开火率fireRate(间隔时长)、nextFire(下一次时间点)

实例化基本对象(此时不知道是什么类型的对象),作为游戏对象

GameObject clone=Instantiate(shot, shotSpawn.position, shotSpawn.rotation) as GameObject as GameObject;

实例化对象时,接收对象的引用(实例化函数将返回引用), 否则对象只是进入场景,无法很容易找到,并对它操作。

11:00

Input.GetButton("Fire1")含义:

http://docs.unity3d.com/ScriptReference/Input.html

"Fire1", "Fire2" "Fire3" are mapped to Ctrl, Alt, Cmd keys and three mouse or joystick buttons. New input axes can be added in the Input Manager.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class Boundary{
	public float xMin, xMax, zMin, zMax;
}

public class PlayerController : MonoBehaviour {
	public float speed;
	public Boundary boundary;
	public float tilt;

	public GameObject shot;
	public Transform shotSpawn;
	public float fireRate;
	private float nextFire;//初始即0
	void Update(){
		if (Input.GetButton("Fire1") && Time.time > nextFire)
		{
			nextFire = Time.time + fireRate;
			Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
		}
	}
	void FixedUpdate(){
		float moveHorizontal = Input.GetAxis ("Horizontal");
		float moveVertical = Input.GetAxis ("Vertical");
		Vector3 movement = new Vector3 (moveHorizontal,0.0f,moveVertical);
		Rigidbody playerRigidbody=GetComponent<Rigidbody> ();
		playerRigidbody.velocity = movement*speed;

		playerRigidbody.position = new Vector3 (
			Mathf.Clamp(playerRigidbody.position.x,boundary.xMin,boundary.xMax),
			0.0f,
			Mathf.Clamp(playerRigidbody.position.z,boundary.zMin,boundary.zMax)
		);
		playerRigidbody.rotation = Quaternion.Euler(0.0f,0.0f,playerRigidbody.velocity.x*-tilt);
	}
}

清理离开边界的游戏对象

 “Create/3D object/Cube”

创建Cube,作为边界框,设为触发器

覆盖正交相机视野:scale=Orthographic Size*2

对Trigger Collider编写脚本操作:OnTriggerExit

Destroy(other.gameObject);

删除网格过滤器、渲染器

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DestroyByBoundary : MonoBehaviour {

	void OnTriggerExit(Collider other)
	{
		Destroy(other.gameObject);
	}
}

制作危险物

 

 

安装standard Asset

地形

操控树木、植被、水面效果

光源、阴影

角色控制(现成控制器)

音效

内置系统

电子书第6章

Terrain地形

地形光滑

Textures纹理

Windows,lighting setting

引用方式传参:少了值传递中复制的过程,效率更高

找到其他游戏对象(通过名称、标签):Find函数比较耗时,避免在Update中使用,应当初始化时保存

跨脚本访问组件:创建未赋值对象变量,在Inspector赋值

19.6常用脚本API

协同程序19.6.5Coroutine可与主程序并行运行

猜你喜欢

转载自blog.csdn.net/lagoon_lala/article/details/83933487