Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么客户看到自己从地上掉下来?_C#_Unity3d_Photon - Fatal编程技术网

C# 为什么客户看到自己从地上掉下来?

C# 为什么客户看到自己从地上掉下来?,c#,unity3d,photon,C#,Unity3d,Photon,最近我试着做一个多人游戏,我有一些问题。在我点击菜单中的“播放”并进入多人游戏场景后,我看到自己从地上摔了下来,当我在unity中查看“场景”选项卡时,我的角色正站在它应该站的地方并对我的动作做出反应(作为一名玩家,我所能看到的只是我周围的无限虚无,但我仍然可以环顾四周、移动等等)。当另一个玩家连接时,他被窃听到地面上(我可以看到另一个玩家的影子,他直接从unity托管和播放),我根本无法移动 代码: 所以在主菜单场景中,我有: NetworkController脚本: using Photo

最近我试着做一个多人游戏,我有一些问题。在我点击菜单中的“播放”并进入多人游戏场景后,我看到自己从地上摔了下来,当我在unity中查看“场景”选项卡时,我的角色正站在它应该站的地方并对我的动作做出反应(作为一名玩家,我所能看到的只是我周围的无限虚无,但我仍然可以环顾四周、移动等等)。当另一个玩家连接时,他被窃听到地面上(我可以看到另一个玩家的影子,他直接从unity托管和播放),我根本无法移动

代码:

所以在主菜单场景中,我有:

NetworkController
脚本:

using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NetworkController : MonoBehaviourPunCallbacks
{
void Start()
{
    PhotonNetwork.ConnectUsingSettings();
}

public override void OnConnectedToMaster () {
    Debug.Log("coonnected to " + PhotonNetwork.CloudRegion + " server !");
}
void Update()
{
    
}
}
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RoomController : MonoBehaviourPunCallbacks
{
[SerializeField] int multiplayerSceneIndex;

public override void OnEnable () {
    PhotonNetwork.AddCallbackTarget(this);
}

public override void OnDisable () {
    PhotonNetwork.RemoveCallbackTarget(this);
}

public override void OnJoinedRoom () {
    Debug.Log("joined room");
    StartGame();
}

private void StartGame () {
    if ( PhotonNetwork.IsMasterClient ) {
        Debug.Log("Starting game");
        PhotonNetwork.LoadLevel(multiplayerSceneIndex);
    }
}
}
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour, IPunObservable {
[SerializeField] Transform camera;
[SerializeField] float sens = 3.5f;
[SerializeField] float walkSpeed = 6.0f;
[SerializeField] [Range(0.0f, 0.5f)] float smooth = 0.3f;
[SerializeField] [Range(0.0f, 0.5f)] float smoothMouse = 0.3f;
[SerializeField] float gravity = -13.0f;
[SerializeField] float slopeForce = 6;
[SerializeField] float slopeForceRayLenght = 1.5f;
[SerializeField] float runSpeed = 10;
[SerializeField] float runBuildUp = 4;
[SerializeField] float jumpForce = 10;
[SerializeField] KeyCode jumpk;
[SerializeField] KeyCode runk;

[SerializeField] bool cursorLock = true;

public Rigidbody rb;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask grndMask;

public bool isGrounded;

float cameraPitch = 0.0f;
float velocityY = 0.0f;
float speed;

Vector2 currentDir = Vector2.zero;
Vector2 currentDirVelocity = Vector2.zero;

Vector2 currentMouseDelta = Vector2.zero;
Vector2 currentMouseDeltaVelocity = Vector2.zero;

CharacterController controller;

PhotonView photonView;

private void Awake () {
    photonView = GetComponent<PhotonView>();
    photonView.ObservedComponents.Add(this);
    if ( !photonView.IsMine ) {
        enabled = false;
    }
}
void Start () {
    speed = walkSpeed;

    controller = GetComponent<CharacterController>();
    if ( cursorLock ) {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }
}

// Update is called once per frame
void Update () {
    UpdateMouseLook();
    UpdateMovement();
    Jump();
}

void UpdateMouseLook () {
    Vector2 mouseDelta = new Vector2(Input.GetAxis("Mouse 
X"),Input.GetAxis("Mouse Y"));

    currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, mouseDelta,ref 
currentMouseDeltaVelocity, smoothMouse);

    cameraPitch -= currentMouseDelta.y * sens;

    cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);
    camera.localEulerAngles = Vector3.right * cameraPitch;

    transform.Rotate(Vector3.up * currentMouseDelta.x * sens);
}

void SetSpeed () {
    if ( Input.GetKey(runk) ) {
        speed = Mathf.Lerp(speed, runSpeed, Time.deltaTime * runBuildUp);
    } else {
        speed = Mathf.Lerp(speed, walkSpeed, Time.deltaTime * runBuildUp);
    }
}

void Jump () {
    isGrounded = Physics.CheckSphere(groundCheck.position, 
groundDistance,grndMask);
    if ( isGrounded && Input.GetKeyDown(jumpk) ) {
        Debug.Log("jumping");
        velocityY = Mathf.Sqrt(jumpForce * -2f * gravity);
        controller.Move(Vector3.up * velocityY * Time.deltaTime);
    }
}

public bool OnSlope () {
    RaycastHit hit;

    if ( Physics.Raycast(transform.position, Vector3.down, out hit, 
controller.height / 2 * slopeForceRayLenght) ) {
        if ( hit.normal != Vector3.up ) {
            return true;
        }
    }
    return false;
}

void UpdateMovement () {
    Vector2 inputDir = new Vector2(Input.GetAxisRaw("Horizontal"),
    Input.GetAxisRaw("Vertical"));
    inputDir.Normalize();

    currentDir = Vector2.SmoothDamp(currentDir, inputDir, ref
    currentDirVelocity, smooth);

    if ( controller.isGrounded ) {
        velocityY = 0.0f;
    }
    velocityY += gravity * Time.deltaTime;

    Vector3 velocity = (transform.forward * currentDir.y + transform.right * 
currentDir.x) * speed + Vector3.up * velocityY;

    controller.Move(velocity * Time.deltaTime);

    if ( (inputDir.x != 0 || inputDir.y != 0) && OnSlope() ) {
        controller.Move(Vector3.down * controller.height / 2 * slopeForce * 
Time.deltaTime);
    }
    SetSpeed();
}

void IPunObservable.OnPhotonSerializeView (PhotonStream stream, 
PhotonMessageInfo info) {
    if ( stream.IsWriting ) {

        // We own this player: send the others our data
        stream.SendNext(transform.position); //position of the character
        stream.SendNext(transform.rotation); //rotation of the character

    } else {
        // Network player, receive data
        Vector3 syncPosition = (Vector3) stream.ReceiveNext();
        Quaternion syncRotation = (Quaternion) stream.ReceiveNext();
    }
}
}
QuickStartLobbyController
脚本:

using Photon.Pun;
using Photon.Realtime;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StartLobbyController : MonoBehaviourPunCallbacks
{
[SerializeField] GameObject quickStartButton;
[SerializeField] float roomSize;



public override void OnConnectedToMaster () {
    PhotonNetwork.AutomaticallySyncScene = true;
    quickStartButton.SetActive(true);
}

public void QuickStart () {
    quickStartButton.SetActive(false);
    PhotonNetwork.JoinRandomRoom();
    Debug.Log("Quick Start!");
}

public override void OnJoinRandomFailed (short returnCode, string message) {
    Debug.Log("Failed to join a room");
    CreateRoom();
}

public void CreateRoom () {
    Debug.Log("creating room");
    int randomRoomNumber = Random.Range(0, 10000);
    RoomOptions roomOps = new RoomOptions() { IsVisible = true, IsOpen = true, MaxPlayers = (byte)         
roomSize };
    PhotonNetwork.CreateRoom("Room" + randomRoomNumber, roomOps);
    Debug.Log(randomRoomNumber);
}

public override void OnCreateRoomFailed (short returnCode, string message) {
    Debug.Log("failed to create a room");
    CreateRoom();
}
}
QuickStartRoomController
脚本:

using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NetworkController : MonoBehaviourPunCallbacks
{
void Start()
{
    PhotonNetwork.ConnectUsingSettings();
}

public override void OnConnectedToMaster () {
    Debug.Log("coonnected to " + PhotonNetwork.CloudRegion + " server !");
}
void Update()
{
    
}
}
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RoomController : MonoBehaviourPunCallbacks
{
[SerializeField] int multiplayerSceneIndex;

public override void OnEnable () {
    PhotonNetwork.AddCallbackTarget(this);
}

public override void OnDisable () {
    PhotonNetwork.RemoveCallbackTarget(this);
}

public override void OnJoinedRoom () {
    Debug.Log("joined room");
    StartGame();
}

private void StartGame () {
    if ( PhotonNetwork.IsMasterClient ) {
        Debug.Log("Starting game");
        PhotonNetwork.LoadLevel(multiplayerSceneIndex);
    }
}
}
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour, IPunObservable {
[SerializeField] Transform camera;
[SerializeField] float sens = 3.5f;
[SerializeField] float walkSpeed = 6.0f;
[SerializeField] [Range(0.0f, 0.5f)] float smooth = 0.3f;
[SerializeField] [Range(0.0f, 0.5f)] float smoothMouse = 0.3f;
[SerializeField] float gravity = -13.0f;
[SerializeField] float slopeForce = 6;
[SerializeField] float slopeForceRayLenght = 1.5f;
[SerializeField] float runSpeed = 10;
[SerializeField] float runBuildUp = 4;
[SerializeField] float jumpForce = 10;
[SerializeField] KeyCode jumpk;
[SerializeField] KeyCode runk;

[SerializeField] bool cursorLock = true;

public Rigidbody rb;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask grndMask;

public bool isGrounded;

float cameraPitch = 0.0f;
float velocityY = 0.0f;
float speed;

Vector2 currentDir = Vector2.zero;
Vector2 currentDirVelocity = Vector2.zero;

Vector2 currentMouseDelta = Vector2.zero;
Vector2 currentMouseDeltaVelocity = Vector2.zero;

CharacterController controller;

PhotonView photonView;

private void Awake () {
    photonView = GetComponent<PhotonView>();
    photonView.ObservedComponents.Add(this);
    if ( !photonView.IsMine ) {
        enabled = false;
    }
}
void Start () {
    speed = walkSpeed;

    controller = GetComponent<CharacterController>();
    if ( cursorLock ) {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }
}

// Update is called once per frame
void Update () {
    UpdateMouseLook();
    UpdateMovement();
    Jump();
}

void UpdateMouseLook () {
    Vector2 mouseDelta = new Vector2(Input.GetAxis("Mouse 
X"),Input.GetAxis("Mouse Y"));

    currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, mouseDelta,ref 
currentMouseDeltaVelocity, smoothMouse);

    cameraPitch -= currentMouseDelta.y * sens;

    cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);
    camera.localEulerAngles = Vector3.right * cameraPitch;

    transform.Rotate(Vector3.up * currentMouseDelta.x * sens);
}

void SetSpeed () {
    if ( Input.GetKey(runk) ) {
        speed = Mathf.Lerp(speed, runSpeed, Time.deltaTime * runBuildUp);
    } else {
        speed = Mathf.Lerp(speed, walkSpeed, Time.deltaTime * runBuildUp);
    }
}

void Jump () {
    isGrounded = Physics.CheckSphere(groundCheck.position, 
groundDistance,grndMask);
    if ( isGrounded && Input.GetKeyDown(jumpk) ) {
        Debug.Log("jumping");
        velocityY = Mathf.Sqrt(jumpForce * -2f * gravity);
        controller.Move(Vector3.up * velocityY * Time.deltaTime);
    }
}

public bool OnSlope () {
    RaycastHit hit;

    if ( Physics.Raycast(transform.position, Vector3.down, out hit, 
controller.height / 2 * slopeForceRayLenght) ) {
        if ( hit.normal != Vector3.up ) {
            return true;
        }
    }
    return false;
}

void UpdateMovement () {
    Vector2 inputDir = new Vector2(Input.GetAxisRaw("Horizontal"),
    Input.GetAxisRaw("Vertical"));
    inputDir.Normalize();

    currentDir = Vector2.SmoothDamp(currentDir, inputDir, ref
    currentDirVelocity, smooth);

    if ( controller.isGrounded ) {
        velocityY = 0.0f;
    }
    velocityY += gravity * Time.deltaTime;

    Vector3 velocity = (transform.forward * currentDir.y + transform.right * 
currentDir.x) * speed + Vector3.up * velocityY;

    controller.Move(velocity * Time.deltaTime);

    if ( (inputDir.x != 0 || inputDir.y != 0) && OnSlope() ) {
        controller.Move(Vector3.down * controller.height / 2 * slopeForce * 
Time.deltaTime);
    }
    SetSpeed();
}

void IPunObservable.OnPhotonSerializeView (PhotonStream stream, 
PhotonMessageInfo info) {
    if ( stream.IsWriting ) {

        // We own this player: send the others our data
        stream.SendNext(transform.position); //position of the character
        stream.SendNext(transform.rotation); //rotation of the character

    } else {
        // Network player, receive data
        Vector3 syncPosition = (Vector3) stream.ReceiveNext();
        Quaternion syncRotation = (Quaternion) stream.ReceiveNext();
    }
}
}
现在,在多人游戏场景中,玩家应该看到彼此:

玩家拥有以下功能:

游戏设置中的脚本:

using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class GameSetupController : MonoBehaviour
{
 void Start()
 {
    CreatePlayer();
 }

private void CreatePlayer () {
     Debug.Log("Creating Player");
     PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "player"), Vector3.zero, 
 Quaternion.identity);
 }
 }
编辑:这样你就可以确切地看到它的样子。当我从编辑器窗口玩游戏时,即使我看不到它,我也可以四处移动,但对于客户端,我基本上不能在游戏中做任何事情。我认为这可能是游戏设置的问题,或者是将所有玩家与服务器同步的部分的问题

编辑2:发布了
playerController
脚本:

using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NetworkController : MonoBehaviourPunCallbacks
{
void Start()
{
    PhotonNetwork.ConnectUsingSettings();
}

public override void OnConnectedToMaster () {
    Debug.Log("coonnected to " + PhotonNetwork.CloudRegion + " server !");
}
void Update()
{
    
}
}
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RoomController : MonoBehaviourPunCallbacks
{
[SerializeField] int multiplayerSceneIndex;

public override void OnEnable () {
    PhotonNetwork.AddCallbackTarget(this);
}

public override void OnDisable () {
    PhotonNetwork.RemoveCallbackTarget(this);
}

public override void OnJoinedRoom () {
    Debug.Log("joined room");
    StartGame();
}

private void StartGame () {
    if ( PhotonNetwork.IsMasterClient ) {
        Debug.Log("Starting game");
        PhotonNetwork.LoadLevel(multiplayerSceneIndex);
    }
}
}
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour, IPunObservable {
[SerializeField] Transform camera;
[SerializeField] float sens = 3.5f;
[SerializeField] float walkSpeed = 6.0f;
[SerializeField] [Range(0.0f, 0.5f)] float smooth = 0.3f;
[SerializeField] [Range(0.0f, 0.5f)] float smoothMouse = 0.3f;
[SerializeField] float gravity = -13.0f;
[SerializeField] float slopeForce = 6;
[SerializeField] float slopeForceRayLenght = 1.5f;
[SerializeField] float runSpeed = 10;
[SerializeField] float runBuildUp = 4;
[SerializeField] float jumpForce = 10;
[SerializeField] KeyCode jumpk;
[SerializeField] KeyCode runk;

[SerializeField] bool cursorLock = true;

public Rigidbody rb;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask grndMask;

public bool isGrounded;

float cameraPitch = 0.0f;
float velocityY = 0.0f;
float speed;

Vector2 currentDir = Vector2.zero;
Vector2 currentDirVelocity = Vector2.zero;

Vector2 currentMouseDelta = Vector2.zero;
Vector2 currentMouseDeltaVelocity = Vector2.zero;

CharacterController controller;

PhotonView photonView;

private void Awake () {
    photonView = GetComponent<PhotonView>();
    photonView.ObservedComponents.Add(this);
    if ( !photonView.IsMine ) {
        enabled = false;
    }
}
void Start () {
    speed = walkSpeed;

    controller = GetComponent<CharacterController>();
    if ( cursorLock ) {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }
}

// Update is called once per frame
void Update () {
    UpdateMouseLook();
    UpdateMovement();
    Jump();
}

void UpdateMouseLook () {
    Vector2 mouseDelta = new Vector2(Input.GetAxis("Mouse 
X"),Input.GetAxis("Mouse Y"));

    currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, mouseDelta,ref 
currentMouseDeltaVelocity, smoothMouse);

    cameraPitch -= currentMouseDelta.y * sens;

    cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);
    camera.localEulerAngles = Vector3.right * cameraPitch;

    transform.Rotate(Vector3.up * currentMouseDelta.x * sens);
}

void SetSpeed () {
    if ( Input.GetKey(runk) ) {
        speed = Mathf.Lerp(speed, runSpeed, Time.deltaTime * runBuildUp);
    } else {
        speed = Mathf.Lerp(speed, walkSpeed, Time.deltaTime * runBuildUp);
    }
}

void Jump () {
    isGrounded = Physics.CheckSphere(groundCheck.position, 
groundDistance,grndMask);
    if ( isGrounded && Input.GetKeyDown(jumpk) ) {
        Debug.Log("jumping");
        velocityY = Mathf.Sqrt(jumpForce * -2f * gravity);
        controller.Move(Vector3.up * velocityY * Time.deltaTime);
    }
}

public bool OnSlope () {
    RaycastHit hit;

    if ( Physics.Raycast(transform.position, Vector3.down, out hit, 
controller.height / 2 * slopeForceRayLenght) ) {
        if ( hit.normal != Vector3.up ) {
            return true;
        }
    }
    return false;
}

void UpdateMovement () {
    Vector2 inputDir = new Vector2(Input.GetAxisRaw("Horizontal"),
    Input.GetAxisRaw("Vertical"));
    inputDir.Normalize();

    currentDir = Vector2.SmoothDamp(currentDir, inputDir, ref
    currentDirVelocity, smooth);

    if ( controller.isGrounded ) {
        velocityY = 0.0f;
    }
    velocityY += gravity * Time.deltaTime;

    Vector3 velocity = (transform.forward * currentDir.y + transform.right * 
currentDir.x) * speed + Vector3.up * velocityY;

    controller.Move(velocity * Time.deltaTime);

    if ( (inputDir.x != 0 || inputDir.y != 0) && OnSlope() ) {
        controller.Move(Vector3.down * controller.height / 2 * slopeForce * 
Time.deltaTime);
    }
    SetSpeed();
}

void IPunObservable.OnPhotonSerializeView (PhotonStream stream, 
PhotonMessageInfo info) {
    if ( stream.IsWriting ) {

        // We own this player: send the others our data
        stream.SendNext(transform.position); //position of the character
        stream.SendNext(transform.rotation); //rotation of the character

    } else {
        // Network player, receive data
        Vector3 syncPosition = (Vector3) stream.ReceiveNext();
        Quaternion syncRotation = (Quaternion) stream.ReceiveNext();
    }
}
}
使用Photon.Pun;
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类玩家控制器:单一行为,不可观察{
[字段]变换摄影机;
[SerializeField]浮点传感器=3.5f;
[SerializeField]浮动行走速度=6.0f;
[field][Range(0.0f,0.5f)]浮动平滑=0.3f;
[SerializeField][Range(0.0f,0.5f)]浮动平滑鼠标=0.3f;
[Field]浮动重力=-13.0f;
[SerializeField]浮点斜率力=6;
[SerializeField]浮动斜率ForceRayLength=1.5f;
[SerializeField]浮点运行速度=10;
[SerializeField]float runbuild=4;
[Field]浮动跳线力=10;
[SerializeField]键码跳线;
[SerializeField]KeyCode runk;
[SerializeField]bool cursorLock=true;
公共刚体;
公开审查;
公共浮地距离=0.4f;
公共层掩码grndMask;
公共学校停课;
浮动式摄像机=0.0f;
浮动速度y=0.0f;
浮动速度;
Vector2 currentDir=Vector2.0;
Vector2 currentDirVelocity=Vector2.0;
Vector2 currentMouseDelta=Vector2.0;
Vector2 currentMouseDeltaVelocity=Vector2.0;
字符控制器;
PhotonView PhotonView;
私人空间(){
photonView=GetComponent();
photonView.ObservedComponents.Add(本);
如果(!photonView.IsMine){
启用=错误;
}
}
无效开始(){
速度=行走速度;
控制器=GetComponent();
如果(游标锁定){
Cursor.lockState=CursorLockMode.Locked;
Cursor.visible=false;
}
}
//每帧调用一次更新
无效更新(){
UpdateMouseLook();
UpdateMovement();
跳跃();
}
void UpdateMouseLook(){
Vector2 mouseDelta=新的Vector2(Input.GetAxis(“鼠标
X”),Input.GetAxis(“鼠标Y”);
currentMouseDelta=Vector2.SmoothDamp(currentMouseDelta,mouseDelta,参考
currentMouseDeltaVelocity,smoothMouse);
cameraPitch-=currentMouseDelta.y*传感器;
cameraPitch=Mathf.夹具(cameraPitch,-90.0f,90.0f);
camera.localEulerAngles=Vector3.right*cameraPitch;
变换。旋转(矢量3.up*currentMouseDelta.x*传感器);
}
无效设定速度(){
if(Input.GetKey(runk)){
速度=Mathf.Lerp(速度、运行速度、时间增量*运行累积);
}否则{
速度=Mathf.Lerp(速度、行走速度、时间增量*运行累积);
}
}
空跳(){
isGrounded=物理.CheckSphere(groundCheck.position,
接地距离(grndMask);
if(isground&&Input.GetKeyDown(jumpk)){
Debug.Log(“跳跃”);
速度y=数学Sqrt(跳跃力*-2f*重力);
控制器移动(向量3.up*速度y*时间.deltaTime);
}
}
斜坡上的公共厕所(){
雷卡斯特击中;
如果(物理)光线投射(transform.position,Vector3.down,out-hit,
控制器高度/2*slopeForceRayLength){
if(hit.normal!=Vector3.up){
返回true;
}
}
返回false;
}
void UpdateMovement(){
Vector2 inputDir=新Vector2(Input.GetAxisRaw(“水平”),
GetAxisRaw(“垂直”);
inputDir.Normalize();
currentDir=Vector2.SmoothDamp(currentDir,inputDir,ref
速度,平滑);
如果(controller.isground){
速度y=0.0f;
}
速度y+=重力*时间增量;
矢量3速度=(transform.forward*currentDir.y+transform.right*
currentDir.x)*速度+矢量3.up*速度y;
控制器。移动(速度*时间。增量时间);
if((inputDir.x!=0 | | inputDir.y!=0)&&OnSlope(){
控制器。移动(矢量3.down*controller.height/2*slopeForce*
时间(deltaTime);
}
设置速度();
}
无效IPunObservable.OnPhotonSerializeView(PhotonStream流,
PhotonMessageInfo(信息){
if(stream.IsWriting){
//我们拥有这个玩家:向其他玩家发送我们的数据
stream.SendNext(transform.position);//字符的位置
stream.SendNext(transform.rotation);//字符的旋转
}否则{
//网络播放器,接收数据
Vector3 syncPosition=(Vector3)stream.ReceiveNext();
四元数同步旋转=(四元数)stream.ReceiveNext();
}
}
}
EDIT3:我发现了一些东西!当我从主玩家进入游戏时,我仍然需要在编辑器中将自己拉高一点,我将停止摔倒,我可以玩游戏,但是当第二个玩家连接时,我的相机设置为他的(在这段时间内,我可以移动我的角色,并通过第二个玩家的相机看到这一点),当他离开时,在主玩家上恢复正常。然而,对于第二个玩家,我无法移动