我的游戏开源(多人Mirror)

player的主要代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using TMPro;
using UnityEngine;
using Mirror;

public class Playercode : NetworkBehaviour {

    public float speed = 5f;
    //cam
    public Camera cam;
    public Camera cam2;
    public Camera cam3;
    //sync
    [Header("sync")]
    public int killsum;
    [SyncVar(hook = "SetColor")]
    Color PlayerColor = Color.black;
    [SyncVar(hook = "showname")]
    private string pname;
    public string playername;
    //[SyncVar(hook = "RpcsetImagecolor1")]
    private Color image1;
    //[SyncVar(hook = "RpcsetImagecolor2")]
    private Color image2;
    //
    public GameObject anm;
    public SkinnedMeshRenderer playermesh;
    [Header("CD")]
    private float FireCD;
    private float moveCD;
    //time
    public int maxrealshijian = 10;
    private int realshijian;
    private 倒计时 djs;
    //
    Vector3 fuhuodian;
    Vector3 guanzhandian;
    //
    public GameObject killplayer;
    public GameObject bullutprefab;
    //bool
    public bool die = false;
    private bool change = true;

    [Header("UI")]
    public TMP_Text realtime;
    public TMP_Text myname;
    public Text bishiCD;
    public Text shunyiCD;

    // Use this for initialization
    public override void OnStartLocalPlayer()
    {
        base.OnStartLocalPlayer();
        //kill = GameObject.Find("kill").GetComponent<Image>();
        //beikill = GameObject.Find("beikill").GetComponent<Image>();
        djs = GameObject.Find("jishi").gameObject.GetComponent<倒计时>();
        if (djs == null) NetworkManager.singleton.StopClient();
        killsum = 1;
        killsum--;
        Application.targetFrameRate = 30;
        //Cursor.visible = false;
        //Cursor.lockState = CursorLockMode.Locked;
        guanzhandian = new Vector3(30, 10, -2.5f);
        fuhuodian = transform.position;
        cam2.gameObject.SetActive(false);
        cam3.gameObject.SetActive(false);
        cam = cam2;
        cam.gameObject.SetActive(true);
        realtime.gameObject.SetActive(false);
        realshijian = maxrealshijian;
    }
    public override void OnStartServer()
    {
        base.OnStartServer();
        float r = Random.Range(0f, 1f);
        float g = Random.Range(0f, 1f);
        float b = Random.Range(0f, 1f);
        PlayerColor = new Color(r,g,b);
        pname = playername;
    }

    private void FixedUpdate()
    {
        if (!isLocalPlayer) return;
        Move();
        MouseLook();
        if (2.0f - Time.time + FireCD >= 0) 
        {
            string t;
            t = "中指攻击冷却:" + (2.0f - Time.time + FireCD).ToString();
            bishiCD.text = t;
        }
        else
            bishiCD.text = "中指攻击冷却:0";
        if (5.0f - Time.time + moveCD >= 0)
        {
            string t;
            t = "瞬移冷却:" + (5.0f - Time.time + moveCD).ToString();
            shunyiCD.text = t;
        }
        else
            shunyiCD.text = "瞬移冷却:0";
    }
    // Update is called once per frame
    void Update () {
        if (!isLocalPlayer) return;
        if (this.transform.position.y < -13.0f)
        {
            //transform.position = guanzhandian;
            if (djs.daojishi > 0)
                transform.position = fuhuodian;
            else
            {
                transform.position = guanzhandian;
                if (killplayer != null) 
                    killplayer.GetComponent<Playercode>().killsum++;
                die = true;
                realshijian = maxrealshijian;
                StopCoroutine("reallife");
                StartCoroutine("reallife");
                transform.Translate(0, 0, Random.Range(-2.0f, 2.0f));
            }
        }
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Ray ray = new Ray(transform.position, Vector3.down);
            if (Physics.Raycast(ray,1.5f))
            {
                anm.GetComponent<Animator>().SetBool("jump?", true);
                GetComponent<Rigidbody>().velocity = Vector3.up * 8;
            }
        }
        else
        {
            Ray ray = new Ray(transform.position, Vector3.down);
            if (Physics.Raycast(ray, 1.5f))
            {
                anm.GetComponent<Animator>().SetBool("jump?", false);
            }
        }
        if(Input.GetKeyDown(KeyCode.V))
        {
            if (change)
            {
                cam = cam3;
                change = false;
                cam.gameObject.SetActive(true);
                cam2.gameObject.SetActive(false);
            }
            else if(!change)
            {
                cam = cam2;
                change = true;
                cam.gameObject.SetActive(true);
                cam3.gameObject.SetActive(false);
            }
        }

        if (die || djs.daojishi > 0) return;
        if (Input.GetMouseButtonDown(0) && Time.time - FireCD >= 2.0f) 
        {
            CmdFire();
            FireCD = Time.time;
        }
        if (Input.GetKeyDown(KeyCode.E) && Time.time - moveCD >= 5.0f)
        {
            transform.Translate(0, 0, 2.5f);
            moveCD = Time.time;
        }
    }
    /*[Command]
    void Cmdchangeimage()
    {
        image1 = killzhe.GetComponent<Player>().playermesh.material.color;
        image2 = this.playermesh.material.color;
    }*/
    void Move(){
        float x = Input.GetAxis ("Horizontal");
        float z = Input.GetAxis ("Vertical");
        if (z > 0)
            anm.GetComponent<Animator>().SetInteger("run?", 1);
        else if (z < 0)
            anm.GetComponent<Animator>().SetInteger("run?", 2);
        else
            anm.GetComponent<Animator>().SetInteger("run?", 0);

        Vector3 fwd = transform.forward;
        Vector3 f = new Vector3 (fwd.x, 0, fwd.z).normalized;
        Vector3 r = transform.right;
        Vector3 move = f * z + r * x;
        transform.position += move * speed * Time.deltaTime;
        Debug.Log(x + " " + z);
    }

    void MouseLook(){
        float mx = Input.GetAxis ("Mouse X");
        float my = Input.GetAxis ("Mouse Y");

        Quaternion qx = Quaternion.Euler (0, mx*2, 0);
        Quaternion qy = Quaternion.Euler (-my*2, 0, 0);

        transform.rotation = qx * transform.rotation;
        cam.transform.rotation = cam.transform.rotation * qy;

        float angle = transform.eulerAngles.x;
        if (angle > 180) {
            angle -= 360;
        }
        if (angle < -180) {
            angle += 360;
        }

        if (angle > 80) {
            transform.eulerAngles = new Vector3 (80, transform.eulerAngles.y, 0);
        }

        if (angle < -80) {
            transform.eulerAngles = new Vector3 (-80, transform.eulerAngles.y, 0);
        }
    }

    public void hidemouse()
    {
        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;
    }
    [Command]
    void CmdFire()
    {
        GameObject bullut = Instantiate(bullutprefab, transform.position, this.cam.transform.rotation);
        bullut.GetComponent<peng>().fasheshe = this.gameObject;
        bullut.transform.Rotate(0, -90, 0);
        bullut.transform.Translate(1, 0, 0);
        GameObject.Destroy(bullut, 2.0f);
        bullut.GetComponent<Rigidbody>().velocity = this.cam.transform.forward * 40;
        NetworkServer.Spawn(bullut);
    }

    //[ClientRpc]
    void showname(string _,string changename)
    {
        myname.text = changename;
        Debug.Log("change");
    }
    void SetColor(Color _,Color newColor)
    {
        playermesh.material.color = newColor;
        Debug.Log("change");
    }
    public IEnumerator reallife()
    {
        realtime.gameObject.SetActive(true);
        if (realshijian <= 0)
        {
            die = false;
            realtime.gameObject.SetActive(false);
            transform.position = fuhuodian;
        }
        else
        {
            string s = "距复活还有" + realshijian.ToString() + "秒";
            realtime.text = s;
            yield return new WaitForSeconds(1);
            realshijian--;
            StartCoroutine("reallife");
        }
        
    }
    //[ClientRpc]
    /*public void RpcsetImagecolor1(Color _1,Color _2)
    {
        kill.color = _2;
        Debug.Log("kill");
    }
    public void RpcsetImagecolor2(Color _1, Color _2)
    {
        beikill.color = _2;
    }
    public IEnumerator clearkill()
    {
        yield return new WaitForSeconds(8);
        killzhe = null;
    }*/

}
 

猜你喜欢

转载自blog.csdn.net/apple_59169997/article/details/124505837