统一获取网络对象的转换

时间:2018-07-07 13:21:15

标签: unity3d unet

我有一个游戏,您在其中选择攻击的源,然后选择该攻击的目标,从而攻击对象,而我尝​​试使用UNET进行应用,但我陷入了这个问题,游戏中的每个对象都使用同时使用相同的脚本,并且仅使用IsLocalPlayer才能将我的对象与游戏中的其他对象区分开,我尝试使用矩阵来存储我按在其中的对象/变换,但是后来我发现了每个对象都有自己的矩阵和自己的endPoint

这是我的播放器脚本:

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


public class Player : NetworkBehaviour {
    private float UniqueId;
    private bool EnemySelected = false;
    private bool LocalSelected = false;
    public GameObject Ship;
    public Transform ShipSpawnPos ;
    public Transform[] Players = new Transform[4];
    private GameObject[] OwnedPlanets = new GameObject[10];

    GameObject bullet;
    float duration = 5.0f;
    private Vector3 endPoint;
    private bool ally = false;
    private bool One = false;
    private bool Two = false;


    // Use this for initialization
    void Start () {

    }
    public override void OnStartLocalPlayer()
    {
       // ShipSpawnPos = this.gameObject.GetComponentInChildren<Transform> 
().transform;
    }
    // Update is called once per frame
    void Update()
    {

        if (!isLocalPlayer)
        {
            return;
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            CmdFire();
        }
        var x = Input.GetAxis("Horizontal") * Time.deltaTime * 3.0f;
        var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;

        transform.Translate(x, 0, 0);
        transform.Translate(0, z, 0);


    }
  

按下对象:

  void OnMouseDown()
    {
        if (!isLocalPlayer)
    {
        for(int i = 1; i <OwnedPlanets.Length; i++)
        {
            if(OwnedPlanets[i] != null)
            {
                if (this.gameObject == OwnedPlanets[i].gameObject)
                {
                    ally = true;
                }

            }

        }
        if (!ally)
        {
            this.set_EnemySelected(true);
            endPoint = this.GetComponent<Transform>().position;

        }

        return;
    }
    OwnedPlanets[0] = this.gameObject;
    this.set_LocalSelected(true);

    }
  

攻击功能:

    [Command]
    void CmdFire()
    {
        for(int i = 0; i < OwnedPlanets.Length; i++)
        {

        if(OwnedPlanets[i] != null)
        {
            if (OwnedPlanets[i].GetComponent<Player>().get_LocalSelected())
            {
                print(OwnedPlanets[i].GetComponent<Health>().get_currenthealth());
                One = true;
                for (int j = 0; j < OwnedPlanets[i].GetComponent<Health>().get_currenthealth() / 2; j++)
                {

                    // Create the Bullet from the Bullet Prefab
                    bullet = (GameObject)Instantiate(
                       Ship,
                       OwnedPlanets[i].GetComponent<Transform>().position,
                       OwnedPlanets[i].GetComponent<Transform>().rotation);
                    bullet.gameObject.GetComponent<Ship>().set_ID(OwnedPlanets[i].GetComponent<Player>().get_UniqueID());
                    // bullet.transform.position = Vector3.Lerp(OwnedPlanets[i].transform.position, endPoint,7.0f);
                    print("endpoint :" + OwnedPlanets[0].gameObject.GetComponent<Player>().get_endpoint());
                    StartCoroutine(bullet.GetComponent<Ship>().MoveObject(OwnedPlanets[i].transform.position, OwnedPlanets[0].gameObject.GetComponent<Player>().get_endpoint(), bullet, 3.0f));
                    bullet.GetComponent<Ship>().set_AP(OwnedPlanets[i].GetComponent<Health>().get_currenthealth() / 2);
                    // Spawn the bullet on the Clients
                    NetworkServer.Spawn(bullet);
                    //// Destroy the bullet after 2 seconds
                    //Destroy(bullet, 2.0f);
                }
                OwnedPlanets[i].GetComponent<Health>().set_currenthealth(OwnedPlanets[i].GetComponent<Health>().get_currenthealth() / 2);
            }
        }



    }





}




void set_EnemySelected(bool a)
{
    EnemySelected = a;
}

void set_LocalSelected(bool a)
{
    LocalSelected = a;
}

bool get_EnemySelected()
{
    return EnemySelected;
}

bool get_LocalSelected()
{
    return LocalSelected;
}

public float get_UniqueID()
{
    return UniqueId;
}

public void set_UniqueID(float a)
{
     UniqueId = a;
}

public void set_endpoint(Vector3 a)
{
    endPoint = a;
}

public Vector3 get_endpoint()
{
    return endPoint;
}
public void set_Planet(GameObject g)
{
    for(int i = 0; i <OwnedPlanets.Length; i++)
    {
        if (OwnedPlanets[i] == null)
            OwnedPlanets[i] = g;
        print("added");
    }
}
}
  

移动功能:

    public IEnumerator MoveObject(Vector3 startPostion, Vector3 endPostion, 
GameObject rb, float duration)
    {

        float counter = 0;

        while (counter < duration)
        {
            counter += Time.deltaTime;
            if (rb != null)
                rb.transform.position = Vector3.Lerp(startPostion, endPostion, counter / duration);
            yield return null;
        }


}

0 个答案:

没有答案