我有两个设备使用PhotonEngine连接到大厅,im使用以下代码附加到两个按钮来左右移动播放器。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.SceneManagement;
public class movementScript : MonoBehaviourPun
{
Rigidbody2D rb;
// float movement = 0;
public float movementSpeed = 10;
public float upForce = 290;
public GameObject camPosition;
Scene scene;
private bool directionLeft = false; //sole purpose for sprite movement
private bool directionRight = false;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
camPosition = GameObject.FindWithTag("MainCamera");
}
public void MoveRight()
{
directionRight = true;
Vector3 characterScale = transform.localScale;
float originalScale = -1f;
characterScale.x = -originalScale;
transform.localScale = characterScale;
//move player at speed of movement
Vector2 velocity = rb.velocity;
velocity.x = (movementSpeed * Time.deltaTime);
rb.velocity = velocity;
}
public void StopRight()
{
directionRight = false;
}
void ResetX()
{
Vector2 velocity = rb.velocity;
velocity.x = 0f;
rb.velocity = velocity;
}
public void MoveLeft()
{
directionLeft = true;
Vector3 characterScale = transform.localScale;
float originalScale = -1f;
characterScale.x = originalScale;
transform.localScale = characterScale;
//move player at speed of movement
Vector2 velocity = rb.velocity;
velocity.x = -(movementSpeed * Time.deltaTime);
rb.velocity = velocity;
}
public void StopLeft()
{
directionLeft = false;
}
// Update is called once per frame
void Update()
{
if(!directionLeft && !directionRight)
{
ResetX();
}
}
public void TakeInputLeft()
{
if (photonView.IsMine)
{
MoveLeft();
}
}
public void TakeInputRight()
{
if (photonView.IsMine)
{
MoveRight();
}
}
}
当两个玩家都在游戏中时,按任意一个按钮都不会执行任何操作。但是,当一个玩家离开时,留在大厅中的玩家将拥有应有的控制权。为什么仅在玩家离开时才起作用?
更新:
https://i.gyazo.com/acabdbeb6cfca67bd6fc6f279bbda2b9.mp4
我给了每个玩家对象两个按钮,因此每个玩家都带有一个玩家对象和两个按钮。这会在本地移动播放器,但会恢复到播放器在另一台设备上的位置。在两个屏幕上,这两个播放器均保持弹回原位。有什么想法吗?
谢谢