-1

嗨,我正在使用 uNet Unity Multiplayer,但我的播放器控件无法正常工作。我在 1 台设备上连接 2 台设备,它运行良好,但在其他设备上它不会向左或向右移动,也不会跳跃。只有本地玩家可以移动本地角色

这是代码

using UnityEngine;

使用 System.Collections;使用 UnityEngine.Networking;

公共类 Player1 : NetworkBehaviour {

private static Player1 playerInstance;

public static Player1 PlayerInstance {
    get {

        return  playerInstance;
    }

}


void Awake ()
{

    playerInstance = this;
}

public GameObject horseRun;
public GameObject horsesaddleregdoll;
public GameObject pauseCamera;
CharacterController controller;
public Animator anim;
public int horseNum;
public GameObject cam;

void Start ()
{
    if (isLocalPlayer) {
        controller = GetComponent<CharacterController> ();
        transform.position = position2.position;
        currentPos = 2;
        anim.SetInteger ("horse", 0);
        AudioManagerScript.AudioInstence.horseWhinny (horseNum);
    } else {
        Camera cam = GetComponentInChildren <Camera> ();
        cam.enabled = false;
        //          cam.gameObject.SetActive (false);
        //          this.enabled = false;
    }
    Time.timeScale = 0;
    position1 = GameObject.Find ("Position1").transform;
    position2 = GameObject.Find ("Position2").transform;
    position3 = GameObject.Find ("Position3").transform;
    position4 = GameObject.Find ("Position4").transform;
}

public float speed = 10.0f;
public Transform position1;
public Transform position2;
public Transform position3;
public Transform position4;
int currentPos = 1;
bool moveFromPos1ToPos2;
bool moveFromPos2ToPos1;
bool moveFromPos2ToPos3;
bool moveFromPos3ToPos2;
bool moveFromPos3ToPos4;
bool moveFromPos4ToPos3;

float timeMove = 0;
public float jumpspeed = 10.0f;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;

public Vector2 findDirection;
public Vector2 startPoint;
public Vector2 endPoint;

// Update is called once per frame
void Update ()
{
    if (isLocalPlayer) {
        timerStep -= Time.deltaTime;
        gameObject.transform.position += new Vector3 (0, 0, Time.deltaTime * 80.0f);
        if (moveFromPos1ToPos2) {
            if (timeMove >= .5f) {
                transform.position = new Vector3 (position1.position.x, transform.position.y, transform.position.z);
                transform.localEulerAngles = new Vector3 (0, 0, 0);
                moveFromPos1ToPos2 = false;
                currentPos = 2;
                Debug.Log ("Current Pos" + currentPos);
            }
            timeMove += Time.deltaTime;
            transform.position = Vector3.Lerp (new Vector3 (position1.position.x, transform.position.y, transform.position.z), new Vector3 (position2.position.x, transform.position.y, transform.position.z), timeMove * 2);
        }
        if (moveFromPos2ToPos1) {
            if (timeMove >= .5f) {
                transform.position = new Vector3 (position2.position.x, transform.position.y, transform.position.z);
                transform.localEulerAngles = new Vector3 (0, 0, 0);
                moveFromPos2ToPos1 = false;
                currentPos = 1;
                Debug.Log ("Current Pos" + currentPos);
            }
            timeMove += Time.deltaTime;
            transform.position = Vector3.Lerp (new Vector3 (position2.position.x, transform.position.y, transform.position.z), new Vector3 (position1.position.x, transform.position.y, transform.position.z), timeMove * 2);
        }
        if (moveFromPos2ToPos3) {
            if (timeMove >= .5f) {
                transform.position = new Vector3 (position2.position.x, transform.position.y, transform.position.z);
                transform.localEulerAngles = new Vector3 (0, 0, 0);
                moveFromPos2ToPos3 = false;
                currentPos = 3;
                Debug.Log ("Current Pos" + currentPos);
            }
            timeMove += Time.deltaTime;
            transform.position = Vector3.Lerp (new Vector3 (position2.position.x, transform.position.y, transform.position.z), new Vector3 (position3.position.x, transform.position.y, transform.position.z), timeMove * 2);
        }
        if (moveFromPos3ToPos2) {
            if (timeMove >= .5f) {
                transform.position = new Vector3 (position3.position.x, transform.position.y, transform.position.z);
                transform.localEulerAngles = new Vector3 (0, 0, 0);
                moveFromPos3ToPos2 = false;
                currentPos = 2;
                Debug.Log ("Current Pos" + currentPos);
            }
            timeMove += Time.deltaTime;
            transform.position = Vector3.Lerp (new Vector3 (position3.position.x, transform.position.y, transform.position.z), new Vector3 (position2.position.x, transform.position.y, transform.position.z), timeMove * 2);
        }
        if (moveFromPos3ToPos4) {
            if (timeMove >= .5f) {
                transform.position = new Vector3 (position3.position.x, transform.position.y, transform.position.z);
                transform.localEulerAngles = new Vector3 (0, 0, 0);
                moveFromPos3ToPos4 = false;
                currentPos = 4;
                Debug.Log ("Current Pos" + currentPos);
            }
            timeMove += Time.deltaTime;
            transform.position = Vector3.Lerp (new Vector3 (position3.position.x, transform.position.y, transform.position.z), new Vector3 (position4.position.x, transform.position.y, transform.position.z), timeMove * 2);
        }
        if (moveFromPos4ToPos3) {
            if (timeMove >= .5f) {
                transform.position = new Vector3 (position4.position.x, transform.position.y, transform.position.z);
                transform.localEulerAngles = new Vector3 (0, 0, 0);
                moveFromPos4ToPos3 = false;
                currentPos = 3;
                Debug.Log ("Current Pos" + currentPos);
            }
            timeMove += Time.deltaTime;
            transform.position = Vector3.Lerp (new Vector3 (position4.position.x, transform.position.y, transform.position.z), new Vector3 (position3.position.x, transform.position.y, transform.position.z), timeMove * 2);
        }
        moveDirection.y -= gravity * Time.deltaTime;
        controller.Move (moveDirection * Time.deltaTime);
    }
}

//.................................. Horse Run to Jump & Jump to Run Animation States  ......................
public void HorseJumpState ()
{
    Debug.Log ("JUMP HORSE" + gameObject.name);
    moveDirection.y = jumpspeed;
    controller.radius *= 0.5f;
    Invoke ("JumpTime", .8f);
    Invoke ("HorseRunState", 0.75f);
}

public void HorseRunState ()
{
    anim.SetInteger ("horse", 0);
}

public void HorseIdleState ()
{
    anim.SetInteger ("horse", 5);
}

float timerStep = 1.25f;

void JumpTime ()
{
    controller.radius *= 2;
}

public void JumpButton ()
{
    if (isLocalPlayer) {
        Time.timeScale = 1;
        Debug.Log ("timer" + timerStep);
        if (timerStep <= 0 && controller.isGrounded) {
            timerStep = 1.25f;
            {
                anim.SetInteger ("horse", 1);
                Invoke ("HorseJumpState", .00001f);
                AudioManagerScript.AudioInstence.jumpSound (horseNum);
            }
        }
    }
}

public void SwipeLeft ()
{
    if (position1 == null || position2 == null || position3 == null || position4 == null) {
        position1 = GameObject.Find ("Position1").transform;
        position2 = GameObject.Find ("Position2").transform;
        position3 = GameObject.Find ("Position3").transform;
        position4 = GameObject.Find ("Position4").transform;
    }
    Time.timeScale = 1;
    if (isLocalPlayer) {
        if (currentPos == 2 && controller.isGrounded && !moveFromPos1ToPos2 && !moveFromPos2ToPos1 && !moveFromPos2ToPos3 && !moveFromPos3ToPos2 && !moveFromPos3ToPos4 && !moveFromPos4ToPos3) {
            moveFromPos2ToPos1 = true;
            transform.localEulerAngles = new Vector3 (0, -15, 0);
            timeMove = 0;
        } else if (currentPos == 3 && controller.isGrounded && !moveFromPos1ToPos2 && !moveFromPos2ToPos1 && !moveFromPos2ToPos3 && !moveFromPos3ToPos2 && !moveFromPos3ToPos4 && !moveFromPos4ToPos3) {
            moveFromPos3ToPos2 = true;
            transform.localEulerAngles = new Vector3 (0, -15, 0);
            timeMove = 0;
        } else if (currentPos == 4 && controller.isGrounded && !moveFromPos1ToPos2 && !moveFromPos2ToPos1 && !moveFromPos2ToPos3 && !moveFromPos3ToPos2 && !moveFromPos3ToPos4 && !moveFromPos4ToPos3) {
            moveFromPos4ToPos3 = true;
            transform.localEulerAngles = new Vector3 (0, -15, 0);
            timeMove = 0;
        }
    }
}

public void SwipeRight ()
{
    if (position1 == null || position2 == null || position3 == null || position4 == null) {
        position1 = GameObject.Find ("Position1").transform;
        position2 = GameObject.Find ("Position2").transform;
        position3 = GameObject.Find ("Position3").transform;
        position4 = GameObject.Find ("Position4").transform;
    }
    Time.timeScale = 1;
    if (isLocalPlayer) {
        if (currentPos == 1 && controller.isGrounded && !moveFromPos1ToPos2 && !moveFromPos2ToPos1 && !moveFromPos2ToPos3 && !moveFromPos3ToPos2 && !moveFromPos3ToPos4 && !moveFromPos4ToPos3) {
            moveFromPos1ToPos2 = true;
            transform.localEulerAngles = new Vector3 (0, 15, 0);
            timeMove = 0;
        } else if (currentPos == 2 && controller.isGrounded && !moveFromPos1ToPos2 && !moveFromPos2ToPos1 && !moveFromPos2ToPos3 && !moveFromPos3ToPos2 && !moveFromPos3ToPos4 && !moveFromPos4ToPos3) {
            moveFromPos2ToPos3 = true;
            transform.localEulerAngles = new Vector3 (0, 15, 0);
            timeMove = 0;
        } else if (currentPos == 3 && controller.isGrounded && !moveFromPos1ToPos2 && !moveFromPos2ToPos1 && !moveFromPos2ToPos3 && !moveFromPos3ToPos2 && !moveFromPos3ToPos4 && !moveFromPos4ToPos3) {
            moveFromPos3ToPos4 = true;
            transform.localEulerAngles = new Vector3 (0, 15, 0);
            timeMove = 0;
        }
    }
}

bool gameOver = false;

void GameOveronTriggerEnter ()
{
    if (!gameOver) {
        gameOver = true;
        GUIManager.GuiInstance.gameOver.SetActive (true);
        GUIManager.GuiInstance.GrassObstaclesDestroy ();
        GUIManager.GuiInstance.Invoke ("AdsCall", .7f);
        AudioManagerScript.AudioInstence.pauseSound ();
        GUIManager.GuiInstance.scorecheck ();
    }
}

void DisableScript ()
{
    GameObject player = GameObject.FindGameObjectWithTag ("Player");
    player.GetComponent<Player1> ().enabled = false;
    GameObject[] anims = GameObject.FindGameObjectsWithTag ("animator");
    for (int i = 0; i < anims.Length; i++)
        anims [i].GetComponent<Animator> ().enabled = false;
}

bool gameOverfalse = false;
//.................... Obstacles Triggers .................
void OnTriggerEnter (Collider coll)
{
    int grass = GUIManager.GuiInstance.grass;
    if (coll.gameObject.tag == "hurdle" || coll.gameObject.tag == "bridgehurdles" && gameOverfalse == false) {
        gameOverfalse = true;
        horseRun.SetActive (false);
        horsesaddleregdoll.SetActive (true);
        AudioManagerScript.AudioInstence.horseDie (horseNum);
        gameObject.GetComponent<Player1> ().enabled = false;
        pauseCamera.SetActive (false);
        Invoke ("GameOveronTriggerEnter", 2.0f);
        Invoke ("DisableScript", 1.5f);
        GUIManager.GuiInstance.gamePanelSingle.SetActive (false);
        GUIManager.GuiInstance.singleWater = false;
    } else if (coll.gameObject.tag == "Single") {
        GUIManager.GuiInstance.score += 10 * grass;
    } else if (coll.gameObject.tag == "Double") {
        GUIManager.GuiInstance.score += 25 * grass;
    } else if (coll.gameObject.tag == "Tripple") {
        GUIManager.GuiInstance.score += 50 * grass;
    } else if (coll.gameObject.tag == "Forth") {
        GUIManager.GuiInstance.score += 100 * grass;
    } else if (coll.gameObject.tag == "Grass") {
        Debug.Log ("Grass", coll.gameObject);
        GUIManager.GuiInstance.grass++;
        AudioManagerScript.AudioInstence.Collectable (horseNum);
        //GUIManager.GuiInstance.score+= 250*2;
        Destroy (coll.gameObject);
    } else if (coll.gameObject.tag == "grassreset") {
        Debug.Log ("Grass", coll.gameObject);
        GUIManager.GuiInstance.grass = 1;
        //GUIManager.GuiInstance.score+= 250*2;
        Destroy (coll.gameObject);
    }


    if (coll.gameObject.tag == "snow") {
        Debug.Log ("Trigger Enter");
        GUIManager.GuiInstance.snow.SetActive (true);
    }



}

bool dontMove = false;
//..................While Horse on Bridge ......................
void OnTriggerStay (Collider colstay)
{

    if (colstay.gameObject.tag == "Bridge") {
        dontMove = true;
        Debug.Log ("Trigger Stay");
        moveFromPos1ToPos2 = false;
        moveFromPos2ToPos1 = false;
        transform.localEulerAngles = new Vector3 (0, 0, 0);
        //          cam.GetComponent<SmoothFollow> ().height = 50;
    }
}

void OnTriggerExit (Collider colexit)
{

    if (colexit.gameObject.tag == "Bridge") {
        dontMove = false;
        //          cam.GetComponent<SmoothFollow> ().height = 35;
    }


    if (colexit.gameObject.tag == "snow") {
        Debug.Log ("Trigger Exit");
        GUIManager.GuiInstance.snow.SetActive (false);
    }

}

}

4

1 回答 1

1

在 UNET 中,在对非玩家对象执行任何操作之前,您必须首先获得其权限。获得授权后,您将能够执行跨网络同步的操作。

有关详细信息,您可以查看我的这个答案

于 2017-10-04T10:02:59.567 回答