UnityのPhoton2でゲームを再生するとプレイヤー1がルームを作り、プレイヤー2以降がそのルームに参加するプログラムをこの動画を参考に作ったのですが、どちらも相手プレイヤーが表示はされるのですが、スポーンしたらすぐ床をそれぞれの画面の相手プレイヤーが床をすり抜けて落下してしまいます。
なにが、原因でしょうか?
PlayerMove
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using Photon.Pun; 5 6public class PlayerMove : MonoBehaviourPunCallbacks 7{ 8 private CharacterController controller; 9 private Animator animator; 10 private Vector3 velocity; 11 [SerializeField] 12 private float walkSpeed = 5f; 13 [SerializeField] 14 private float runSpeed = 8f; 15 private bool runFlag = false; 16 public float mouseSensitivity = 100f; 17 public Transform playerBody; 18 float xRotation = 0f; 19 [SerializeField] 20 private Transform spine; 21 public GameObject cameraParent; 22 23 void Start() 24 { 25 controller = GetComponent<CharacterController>(); 26 animator = GetComponent<Animator>(); 27 Cursor.lockState = CursorLockMode.Locked; 28 if (photonView.IsMine) 29 { 30 cameraParent.SetActive(true); 31 controller.enabled = true; 32 } 33 } 34 35 void Update() 36 { 37 if (!photonView.IsMine) return; 38 if (controller.isGrounded) 39 { 40 velocity = Vector3.zero; 41 velocity = (transform.forward * Input.GetAxis("Vertical") + transform.right * Input.GetAxis("Horizontal")).normalized; 42 float speed = 0f; 43 if ((Input.GetButton("Run")&& Input.GetButton("Fire2") == false)) 44 { 45 runFlag = true; 46 speed = runSpeed; 47 } 48 else 49 { 50 runFlag = false; 51 speed = walkSpeed; 52 } 53 velocity *= speed; 54 55 if (velocity.magnitude > 0f) 56 { 57 if (runFlag) 58 { 59 animator.SetFloat("Speed", 2.1f); 60 } 61 else 62 { 63 animator.SetFloat("Speed", 1f); 64 } 65 } 66 else 67 { 68 animator.SetFloat("Speed", 0f); 69 } 70 } 71 velocity.y += Physics.gravity.y * Time.deltaTime; 72 controller.Move(velocity * Time.deltaTime); 73 if (Input.GetKey(KeyCode.Escape)) 74 { 75 Application.Quit(); 76 } 77 } 78 void LateUpdate() 79 { 80 if (!photonView.IsMine) return; 81 float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime; 82 float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime; 83 xRotation -= mouseY; 84 xRotation = Mathf.Clamp(xRotation, -90, 90); 85 playerBody.Rotate(Vector3.up * mouseX); 86 spine.rotation = Quaternion.Euler(spine.eulerAngles.x + xRotation, spine.eulerAngles.y, spine.eulerAngles.z); 87 } 88} 89
Launcher (MenuScene)
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using Photon.Pun; 5 6namespace TEST 7{ 8 public class Launcher : MonoBehaviourPunCallbacks 9 { 10 public void Awake() 11 { 12 PhotonNetwork.AutomaticallySyncScene = true; 13 Connect(); 14 } 15 public override void OnConnectedToMaster() 16 { 17 Debug.Log("CONNECTED"); 18 Join(); 19 base.OnConnectedToMaster(); 20 } 21 public override void OnJoinedRoom() 22 { 23 StartGame(); 24 base.OnJoinedRoom(); 25 } 26 public override void OnJoinRandomFailed(short returnCode, string message) 27 { 28 Create(); 29 base.OnJoinRandomFailed(returnCode, message); 30 } 31 public void Connect() 32 { 33 Debug.Log("Trying to Connect"); 34 PhotonNetwork.GameVersion = "0.0.0"; 35 PhotonNetwork.ConnectUsingSettings(); 36 } 37 public void Join() 38 { 39 PhotonNetwork.JoinRandomRoom(); 40 } 41 public void Create() 42 { 43 PhotonNetwork.CreateRoom(""); 44 } 45 public void StartGame() 46 { 47 if(PhotonNetwork.CurrentRoom.PlayerCount == 1) 48 { 49 PhotonNetwork.LoadLevel(1); 50 } 51 } 52 } 53}
Manager (MapScene)
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using Photon.Pun; 5 6namespace TEST 7{ 8 public class Manager : MonoBehaviour 9 { 10 public Transform spawn_point; 11 private void Start() 12 { 13 Spawn(); 14 } 15 public void Spawn() 16 { 17 GameObject Player = PhotonNetwork.Instantiate("Pose",spawn_point.position,spawn_point.rotation); 18 } 19 } 20}
単に後から生成されるObjectの位置が床の位置よりもすこししたになっているのではないでしょうか.
![guest](/img/icon/icnUserSample.jpg)
あなたの回答
tips
プレビュー