Unet学习

1.创建NetworkManager(空物体),在上面添加NetworkManager组件,添加NetWorkManagerHUD组件

2.创建预制体PlayerPrefab,在上面添加NetworkIdentity,勾选Local Player Authority,在NetWorkManager组件下的SpawnInfo里,将预制件拖入PlayerPrefab上

3。控制移动:添加PlayerController脚本,

4.在Player预制体上添加NetworkTransform,实时同步

5.在子弹上面加NetworkIdentity,同时添加NetworkTransform,将子弹添加到SpawnInfo中

6.制作敌人,添加预制件Enemy

7.添加EnemySpawner

8.添加两个NetworkStartPosition,在NetworkManager里选择RoundRobin

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(-6, 6), 0, Random.Range(-6, 6));
            Quaternion rotation = Quaternion.Euler(0, Random.Range(0, 360), 0);

            GameObject enemy = GameObject.Instantiate(enemyPrefab, position, rotation) as GameObject;

            NetworkServer.Spawn(enemy);
        }
    }
}



using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class PlayerController : NetworkBehaviour {

    public GameObject bulletPrefab;
    public Transform firePos;
	
	// Update is called once per frame
	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))
        {
            CmdFire();
        }
	}

    [Command]  //called in client run in server
    void CmdFire() //这个方法在Server里面调用
    {
        //子弹生成 需要Server完成 , 然后把子弹同步到Client中
        GameObject bullet = GameObject.Instantiate(bulletPrefab, firePos.position, firePos.rotation);
        bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 10;
        Destroy(bullet, 2);

        NetworkServer.Spawn(bullet);   //同步到各个客户端
    }
    public override void OnStartLocalPlayer()
    {
        //这个方法只会在本地角色那里调用 当角色被创建的时候
        GetComponent<MeshRenderer>().material.color = Color.blue;
    }
}


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

public class Bullet : MonoBehaviour {

    private void OnCollisionEnter(Collision collision)
    {
        GameObject hit = collision.gameObject;
        Health headlth = hit.GetComponent<Health>();
        if(headlth!=null)
        {
            headlth.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")]
    public int currentHealth = maxHealth;
    public Slider healthSlider;
    public bool DestroyOnDeath = false;

    private NetworkStartPosition[] spawnPoints;

    private 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;
            Debug.Log("Dead");
            RpcRespawn();
        }
       
    }
    void OnChangeHealth(int health)
    {
        healthSlider.value = health / (float)maxHealth;
    }

    [ClientRpc]     //只在客户端下进行
    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;

public class LookAt : MonoBehaviour {

	
	
	// Update is called once per frame
	void Update () {
        transform.LookAt(Camera.main.transform);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值