示例就是最好的学习!
Tank
要做的游戏也是与射击相关的,就从Tank开始学习吧!看看哪些可以用上,哪些需要改进!
Scripts
Tank.cs
public class Tank : NetworkBehaviour
{
[Header("Components")]
public NavMeshAgent agent;
public Animator animator;
[Header("Movement")]
public float rotationSpeed = 100;
[Header("Firing")]
public KeyCode shootKey = KeyCode.Space;
public GameObject projectilePrefab;
public Transform projectileMount;
void Update()
{
// movement for local player
if (!isLocalPlayer) return;
// rotate
float horizontal = Input.GetAxis("Horizontal");
transform.Rotate(0, horizontal * rotationSpeed * Time.deltaTime, 0);
// move
float vertical = Input.GetAxis("Vertical");
Vector3 forward = transform.TransformDirection(Vector3.forward);
agent.velocity = forward * Mathf.Max(vertical, 0) * agent.speed;
animator.SetBool("Moving", agent.velocity != Vector3.zero);
// shoot
if (Input.GetKeyDown(shootKey))
{
CmdFire();
}
}
// this is called on the server
[Command]
void CmdFire()
{
GameObject projectile = Instantiate(projectilePrefab, projectileMount.position, transform.rotation);
NetworkServer.Spawn(projectile);
RpcOnFire();
}
// this is called on the tank that fired for all observers
[ClientRpc]
void RpcOnFire()
{
animator.SetTrigger("Shoot");
}
}
网络对象继承于NetworkBehaviour,而不是MonoBehaviour。
NavMeshAgent寻路导航组件
Update()
Update()函数中检测输入,水平轴是旋转,由rotationSpeed控制旋转速度。
*我们的游戏是需要旋转的时候保持原移动方向的惯性,例如在太空中转弯的感觉,与这个速度是不一样的。
TransformDirection转换到世界坐标系,更多参见1、2
如果按下“开火”键,则调用CmdFire()函数。
CmdFire()
这是一个[Command]修饰的函数,指明客户端命令。
Instantiate()复制一个子弹预制件对象,查看Instantiate用法。
子弹是网络对象,由NetworkServer.Spawn()生成,查看NetworkServer。
RpcOnFire()
这是一个[ClientRpc]修饰的函数,可以由服务端调用。
Animator怎么弄? 官方
Projectile.cs
public class Projectile : NetworkBehaviour
{
public float destroyAfter = 5;
public Rigidbody rigidBody;
public float force = 1000;
public override void OnStartServer()
{
Invoke(nameof(DestroySelf), destroyAfter);
}
// set velocity for server and client. this way we don't have to sync the
// position, because both the server and the client simulate it.
void Start()
{
rigidBody.AddForce(transform.forward * force);
}
// destroy for everyone on the server
[Server]
void DestroySelf()
{
NetworkServer.Destroy(gameObject);
}
// ServerCallback because we don't want a warning if OnTriggerEnter is
// called on the client
[ServerCallback]
void OnTriggerEnter(Collider co)
{
NetworkServer.Destroy(gameObject);
}
}
OnStartServer()
Invoke()方法,5秒后销毁自己DestroySelf()
Start()
Addforce直接模仿物理受力了,给物体施加一个力,也会收到其他力的作用
Pong
Scripts
NetworkManagerPong.cs
public class NetworkManagerPong : NetworkManager
{
public Transform leftRacketSpawn;
public Transform rightRacketSpawn;
GameObject ball;
public override void OnServerAddPlayer(NetworkConnection conn)
{
// add player at correct spawn position
Transform start = numPlayers == 0 ? leftRacketSpawn : rightRacketSpawn;
GameObject player = Instantiate(playerPrefab, start.position, start.rotation);
NetworkServer.AddPlayerForConnection(conn, player);
// spawn ball if two players
if (numPlayers == 2)
{
ball = Instantiate(spawnPrefabs.Find(prefab => prefab.name == "Ball"));
NetworkServer.Spawn(ball);
}
}
public override void OnServerDisconnect(NetworkConnection conn)
{
// destroy ball
if (ball != null)
NetworkServer.Destroy(ball);
// call base functionality (actually destroys the player)
base.OnServerDisconnect(conn);
}
}
继承自NetworkManager,主要用来处理玩家位置。
OnServerAddPlayer()函数根据是1个还是2个玩家数,设置左边或右边,如果2个玩家就生成小球。
OnServerDisconnect()函数销毁自己创建的对象。
Players.cs
public class Player : NetworkBehaviour
{
public float speed = 30;
public Rigidbody2D rigidbody2d;
// need to use FixedUpdate for rigidbody
void FixedUpdate()
{
// only let the local player control the racket.
// don't control other player's rackets
if (isLocalPlayer)
rigidbody2d.velocity = new Vector2(0, Input.GetAxisRaw("Vertical")) * speed * Time.fixedDeltaTime;
}
}
FixedUpdate()函数仅仅控制拍子方向。
rigidbody2d
Ball.cs
public class Ball : NetworkBehaviour
{
public float speed = 30;
public Rigidbody2D rigidbody2d;
public override void OnStartServer()
{
base.OnStartServer();
// only simulate ball physics on server
rigidbody2d.simulated = true;
// Serve the ball from left player
rigidbody2d.velocity = Vector2.right * speed;
}
float HitFactor(Vector2 ballPos, Vector2 racketPos, float racketHeight)
{
// ascii art:
// || 1 <- at the top of the racket
// ||
// || 0 <- at the middle of the racket
// ||
// || -1 <- at the bottom of the racket
return (ballPos.y - racketPos.y) / racketHeight;
}
[ServerCallback] // only call this on server
void OnCollisionEnter2D(Collision2D col)
{
// Note: 'col' holds the collision information. If the
// Ball collided with a racket, then:
// col.gameObject is the racket
// col.transform.position is the racket's position
// col.collider is the racket's collider
// did we hit a racket? then we need to calculate the hit factor
if (col.transform.GetComponent())
{
// Calculate y direction via hit Factor
float y = HitFactor(transform.position,
col.transform.position,
col.collider.bounds.size.y);
// Calculate x direction via opposite collision
float x = col.relativeVelocity.x > 0 ? 1 : -1;
// Calculate direction, make length=1 via .normalized
Vector2 dir = new Vector2(x, y).normalized;
// Set Velocity with dir * speed
rigidbody2d.velocity = dir * speed;
}
}
}
OnStartServer()
初始球方向
HitFactor()
判断是否打在球拍上
OnCollisionEnter2D()
碰撞处理
Scene
Table对象由WallTop等5个对象组成,其中除DottleLine外,都挂载了Box Collider 2D组件,支持碰撞检测。
整体流程
- 场景初始界面,直接设计;(我们的游戏可能需要能动态下载布局生成场景)
- Player预制,在NetworkManager中生成Player对象;
- Player脚本中支持用户输入,响应用户操作。
参考
https://www.cnblogs.com/eangulee/p/3572037.html