要添加的组件
主角要添加
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class PlayerController : NetworkBehaviour {
public GameObject bulletPrefab;
public Transform bulletSpawn;
void Update () {
if (isLocalPlayer == false)
{
return;
}
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
transform.Rotate(Vector3.up * h * 120 * Time.deltaTime);
transform.Translate(Vector3.forward * v * 3 * Time.deltaTime);
if (Input.GetKeyDown(KeyCode.Space))
{
Fire();
}
}
public override void OnStartLocalPlayer()
{
//这个方法只会在本地角色那里调用,当角色被创建的时候
GetComponent<MeshRenderer>().material.color = Color.blue;
}
void Fire()
{
GameObject bullet = Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation) as GameObject;
bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 10;
Destroy(bullet, 2);
}
}