Shooting (Single Player)
射击(单人)
A common feature in multiplayer games is to have the player be able to shoot bullets and have these bullets work across all Clients in the game.
多人游戏的一个共同特点是让玩家可以在游戏中使用子弹和子弹来对付所有的客户。
This section will add shooting, but in a single-player, non-networked manner.
这一节将增加射击,但在单人,非网络的方式。
How to make shooting “network aware” will be covered in the next section of this lesson.
如何使射击“网络意识”将在本课的下一节讨论。
Create a new Sphere primitive GameObject.
创建一个新的球体原始游戏对象。
Rename the Sphere GameObject “Bullet”.
重命名球体游戏对象“子弹”。
With the Bullet GameObject selected,
选择了Bullet GameObject,
...
change the scale of the GameObject’s Transform to (0.2, 0.2, 0.2).
改变游戏对象转换的规模(0.2,0.2,0.2)。
...
find and add the component: Physics > Rigidbody.
发现并添加组件:物理>刚体。
On the Rigidbody component, set the Use Gravity checkbox to false.
在刚体组件上,将使用重力复选框设置为false。
Drag the Bullet GameObject into the Project Window to make a prefab asset.
将Bullet GameObject拖到项目窗口中,使其成为预置资产。
Delete the Bullet GameObject from the scene.
从场景中删除Bullet GameObject。
Save the scene.
保存场景。
The PlayerController script will now need to be updated for shooting.
PlayerController脚本现在需要更新用于拍摄。
The script will need to have a reference to the Bullet prefab and have code to do the actual shooting logic.
该脚本需要参考子弹预置,并有代码来执行实际的拍摄逻辑。
Open the PlayerController script for editing.
打开PlayerController脚本进行编辑。
Add a public field for the Bullet prefab.
为子弹预制板添加一个公共区域。
public GameObject bulletPrefab;
Add a public field for the location of the Bullet Spawn.
添加一个公共字段,以确定子弹产生的位置。
public Transform bulletSpawn;
公共变换bulletSpawn;
Add Input handling in the Update function.
在更新函数中添加输入处理。
if (Input.GetKeyDown(KeyCode.Space))
{
Fire();
}
Add a “Fire” function to fire a Bullet.
void Fire()
{
// Create the Bullet from the Bullet Prefab
var bullet = (GameObject)Instantiate (
bulletPrefab,
bulletSpawn.position,
bulletSpawn.rotation);
// Add velocity to the bullet
bullet.GetComponent().velocity = bullet.transform.forward * 6;
// Destroy the bullet after 2 seconds
Destroy(bullet, 2.0f);
}
The final script will look like this:
最后的脚本是这样的:
PlayerController:
usingUnityEngine;
usingUnityEngine.Networking;
publicclassPlayerController:NetworkBehaviour
{
publicGameObject bulletPrefab;
publicTransform bulletSpawn;
voidUpdate()
{
if(!isLocalPlayer)
{
return;
}
var x=Input.GetAxis("Horizontal")*Time.deltaTime* 150.0f;
var z=Input.GetAxis("Vertical")*Time.deltaTime* 3.0f;
transform.Rotate(0, x, 0); transform.Translate(0, 0, z);
if(Input.GetKeyDown(KeyCode.Space))
{
Fire();
}
}
void Fire()
{
// Create the Bullet from the Bullet Prefab
var bullet = (GameObject)Instantiate(
bulletPrefab,
bulletSpawn.position,
bulletSpawn.rotation);
// Add velocity to the bullet
bullet.GetComponent().velocity = bullet.transform.forward * 6;
// Destroy the bullet after 2 seconds
Destroy(bullet, 2.0f);
}
publicoverridevoidOnStartLocalPlayer ()
{
GetComponent().material.color=Color.blue;
}
}
Save the script.
保存脚本。
Return to Unity.
回到Unity。
In the next step, we need to set up the Player GameObject to reflect changes in the PlayerController script.
下一步,我们需要设置Player GameObject来反映PlayerController脚本中的更改。
Drag the Player prefab into the Hierarchy View to be able to edit the Player GameObject.
将播放器预置到层次视图中,以编辑播放器的游戏对象。
With the Player GameObject selected,
选择了玩家的游戏对象,
...
create a new Cylinder primitive as a child.
作为一个孩子创建一个新的圆柱原语。
Rename the Cylinder GameObject to “Gun”.
将圆筒游戏对象重命名为“枪”。
With the Gun GameObject selected,
选择了枪的游戏对象,
…
remove the Capsule Collider Component.
拆去太空舱碰撞部件。
…
set the Transform's Position to: (0.5, 0.0, 0.5).
将转换的位置设置为:(0.5,0.0,0.5)。
…
set the Transform's Rotation to: (90.0, 0.0, 0.0).
将转换的旋转设置为:(90.0,0.0,0.0)。
…
set the Transform's Scale to (0.25, 0.5, 0.25).
将转换的规模设置为(0.25,0.5,0.25)。
…
set the Material to the Black Material.
将材料设置为黑色材料。
The Player GameObject should look like this:
玩家的游戏对象应该是这样的:
With the Player GameObject selected,
选择了玩家的游戏对象,
...
create a new empty GameObject as a child.
作为一个孩子创建一个新的空的游戏对象。
Rename this empty GameObject “Bullet Spawn”.
将这个空的游戏对象重命名为“Bullet Spawn”。
Set the Bullet Spawn’s Position to (0.5, 0.0, 1.0).
将弹珠的位置设置为(0.5,0.0,1.0)。
This should place the Bullet Spawn’s Transform Position to the end of the Gun GameObject.
这应该将子弹的转换位置放置到枪的游戏对象的末端。
With the Player GameObject selected,
选择了玩家的游戏对象,
...
…
apply the changes to the Prefab asset.
将更改应用于预置资产。
Delete the Player GameObject from the scene.
从场景中删除玩家的游戏对象。
Save the scene.
保存场景。
The next step is to set up the PlayerController component on the Player prefab.
下一步是在播放器预置上设置PlayerController组件。
Select the Player prefab.
选择球员预制。
With the Player prefab selected:
与玩家预制选择:
...
…
add the Bullet prefab to the Bullet Prefab field in the PlayerController component.
在PlayerController组件中向子弹预置字段添加子弹预置。
...
…
add the Bullet Spawn child to the BulletSpawn field in the PlayerMovement component.
在PlayerMovement组件中,将“子”子元素添加到“弹药”字段中。
Save the project.
保存项目。
Test the current state of the project.
测试项目的当前状态。
Build and Run this scene as a standalone application.
构建并运行这个场景作为一个独立的应用程序。
Click the Host button from the in-game UI to start this game as a Host.
单击游戏内UI中的主机按钮以作为主机启动此游戏。
Move the Player GameObject.
玩家GameObject移动。
Return to Unity.
回到Unity。
Enter Play Mode.
进入播放模式。
Click the LAN Client button from the in-game UI to connect to the Host as a Client.
单击游戏内UI中的LAN客户端按钮以连接到主机作为客户端。
Pressing the spacebar should create a new Bullet GameObject at the Bullet Spawn position for the local Player GameObject.
按下空格键应该为本地玩家的游戏对象创建一个新的子弹游戏对象。
The bullet is not created on other Clients, as the bullets and shooting code are not network aware.
子弹并不是在其他客户端上创建的,因为子弹和射击代码没有网络意识。
Close the standalone player.
关闭独立的球员。
Return to Unity.
回到Unity。
Exit Play Mode.
退出播放模式。
网友评论