Unity——Network_09.Codes

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

public class Bullet : MonoBehaviour
{
    void OnCollisionEnter(Collision col)
    {
        GameObject hit = col.gameObject;
        Health health = hit.GetComponent<Health>();

        if (health != null)
        {
            health.TakeDamage(10);
        }
        Destroy(this.gameObject);
    }
}

 

 

血量控制脚本

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;

public class Health :  NetworkBehaviour{

    public const int maxHealth = 100;
    [SyncVar(hook = "OnChangeHealth")]   //同步下面这个字段   服务端和客户端  hook 是检测到maxHealth的值的变化  之后 调用OnChangeHealth方法
    public int currentHealth = maxHealth;
    public Slider healthSlider;  //血条
    public bool destroyOnDeath = false;
    private NetworkStartPosition[] spawnPoints;
    void Start()
    {
        if (isLocalPlayer)
        {
            spawnPoints = FindObjectsOfType<NetworkStartPosition>();
        }
    }
    public void TakeDamage(int damage)
    {
        if (isServer == false)           
            return;     //仅在服务端进行判断
        currentHealth -= damage;
        if (currentHealth <= 0)
        {
            if (destroyOnDeath)
            {
                Destroy(this.gameObject); return;
            }
            currentHealth = maxHealth;
            RpcResPawn();
        }
        
    }
    void OnChangeHealth(int health)
    {
        healthSlider.value = health / (float)maxHealth;
    }
    [ClientRpc]   //客户端调用  以Rpc开头
    void RpcResPawn()
    {
        if (isLocalPlayer == false)
        {
            return;
        }
        Vector3 spawnPosition = Vector3.zero;
        if (spawnPoints != null && spawnPoints.Length > 0)
        {
            spawnPosition = spawnPoints[Random.Range(0, spawnPoints.Length)].transform.position;
        }
        transform.position = spawnPosition;
    }
}

 

 

 

玩家管理脚本

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;  //命名空间

public class PlayerController : NetworkBehaviour   //继承NetworkBehaviour
{
    public GameObject bulletPrefab; //子弹预设
    public Transform bulletSpawn;  //生成的初始位置
	void Update () {

        if (!isLocalPlayer)   //isLocalPlayer  用来判断是否是本机玩家  是的话返回True  不是返回False
        {
            return;
        }
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        transform.Rotate(Vector3.up * h * 128 * Time.deltaTime);
        transform.Translate(Vector3.forward * v * 3 * Time.deltaTime);

        if (Input.GetKeyDown(KeyCode.Space))
        {
            CmdFire();
        }
	}

    public override void OnStartLocalPlayer()
    {
        //这个方法只会在本地角色哪里调用 当角色被创建的时候
        GetComponent<MeshRenderer>().material.color = Color.blue;
    }
    /// <summary>
    /// 开火   这个方法需要在Server调用
    /// </summary>
    [Command] //call in client,run in server   在服务端调用的方法  必须 以Cmd开头
    public void CmdFire()
    {
        //子弹的生成 需要Server端,然后把子弹同步到客户端
        GameObject bullet = Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation) as GameObject;
        bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 10;
        Destroy(bullet, 2);

        NetworkServer.Spawn(bullet);    //指定一个物体  同步到所有客户端
    }
}

 

 

 

 

 

这个脚本的作用就是让画布始终面对相机    控制血条不旋转

 

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

public class LookAtCamera : MonoBehaviour {


	void Update () {
        transform.LookAt(Camera.main.transform);
	}
}

 

 

 

生成敌人

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class EnemySpawner : NetworkBehaviour {
    public GameObject enemyPrefab;  //敌人的预设
    public int numberOfEnemies;     //数量
    public override void OnStartServer()
    {
        for (int i = 0; i < numberOfEnemies; i++)
        {
            Vector3 position = new Vector3(Random.Range(-6f, 6f), 0, Random.Range(-6f, -6f));
            Quaternion rotation = Quaternion.Euler(0, Random.Range(0, 360), 0);
            GameObject enemy = Instantiate(enemyPrefab, position, rotation) as GameObject;
            NetworkServer.Spawn(enemy);
        }
    }
}

 

 

 

 

 

以上代码为学习siki老师Network视频所码,仅用作复习浏览

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值