facebook twitter hatena line email

「Unity/photon/pun1/位置同期」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
 
(同じ利用者による、間の7版が非表示)
行1: 行1:
==サンプル手順==
+
[[unity/photon/pun1/位置同期/シンプル]]
#ヒエラルキーにCubeを作成
+
#CubeにAddComponentをしてPhotonViewを追加
+
#Cubeに以下CubeScript.csも割り当てる。
+
#CubeのInspectorのPhotonViewのObserverdOptionにCubeを指定する
+
#ヒエラルキーのCubeをプロジェクトのResourcesにプレハブとして追記する(ドラッグ)
+
#部屋入室時に以下コードを実行
+
void OnJoinedRoom() {
+
    GameObject cube = PhotonNetwork.Instantiate ("Cube", new Vector3 (0.0f, 0.0f, 0.0f), Quaternion.Euler(Vector3.zero),0);
+
}
+
  
CubeScript.cs
+
[[unity/photon/pun1/位置同期/アバター風]]
using System.Collections;
+
using System.Collections.Generic;
+
using UnityEngine;
+
public class CubeScript : MonoBehaviour {
+
void Start () {
+
if (GetComponent<PhotonView> ().isMine) {
+
GetComponent<Renderer> ().material.color = Color.red;
+
}
+
}
+
void OnMouseDrag()
+
{
+
Debug.Log ("ownerId=" + GetComponent<PhotonView> ().ownerId);
+
Debug.Log ("isMine=" + GetComponent<PhotonView> ().isMine);
+
if (GetComponent<PhotonView>().isMine) {
+
GetComponent<Renderer> ().material.color = Color.red;
+
Vector3 objectPointInScreen = Camera.main.WorldToScreenPoint (this.transform.position);
+
  Vector3 mousePointInScreen = new Vector3 (Input.mousePosition.x,
+
Input.mousePosition.y,
+
objectPointInScreen.z);
+
Vector3 mousePointInWorld = Camera.main.ScreenToWorldPoint (mousePointInScreen);
+
mousePointInWorld.z = this.transform.position.z;
+
this.transform.position = mousePointInWorld;
+
}
+
}
+
}
+
 
+
==注意==
+
以下コードがfalseになっていると離脱後もインスタンスが残るので注意。
+
PhotonNetwork.autoCleanUpPlayerObjects = true;
+
 
+
==移動をスムーズにするには==
+
#移動させるオブジェクトのInspectorを開き
+
#PhotonTransformViewをAddComponentから追加する
+
#SyncronizePositionにチェックを入れる
+
 
+
==参考==
+
http://sleepnel.hatenablog.com/entry/2016/06/09/120200
+
 
+
http://www.urablog.xyz/entry/2016/09/19/232345
+

2021年8月11日 (水) 17:46時点における最新版

unity/photon/pun1/位置同期/シンプル

unity/photon/pun1/位置同期/アバター風