BeatenByBacon Geschrieben 8. Juni 2021 Melden Share Geschrieben 8. Juni 2021 Hey. Ich bin ziemlich neu in der Entwicklung eines Multiplayer-Spiels, aber ich wollte mal einen Versuch wagen. Ich versuche, ein Multiplayer-Spiel zu programmieren (basierend auf Photon), eine Mechanik davon ist, dass Spieler Wände an die Position des Cursors spawnen können. Aus irgendeinem Grund funktioniert das nicht richtig. Die instanziierten Objekte des Hosts befinden sich an der korrekten Cursorposition, während die instanziierten Objekte des Clients nicht an der richtigen Stelle sind. Hier ist ein Bild, um mein Problem besser zu visualisieren: using System.Collections; using System.Collections.Generic; using UnityEngine; using Photon.Pun; public class WallPlacing : MonoBehaviourPunCallbacks { public GameObject wallGameObject; bool isInstantiated = false; PhotonView view; void Start() { //get the photon view component view = GetComponent<PhotonView>(); } // Update is called once per frame void Update() { if (view.IsMine) { if (Input.GetKeyDown(KeyCode.LeftControl)) { view.RPC(nameof(PlaceWall), RpcTarget.All); } } } //place a wall at cursos pos [PunRPC] void PlaceWall() { Vector2 cursorPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); PhotonNetwork.InstantiateRoomObject(wallGameObject.name, new Vector3(cursorPos.x, cursorPos.y, 1), Quaternion.identity); //GameObject wall = Instantiate(wallGameObject, new Vector3(cursorPos.x, cursorPos.y, 1), Quaternion.identity); Debug.Log(cursorPos); } } Hat jemand eine Idee, wo das Problem liegt? Viele Grüße Zitieren Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
BeatenByBacon Geschrieben 12. Juni 2021 Autor Melden Share Geschrieben 12. Juni 2021 Hab es gelöst bekommen. Das die Lösung ist, dass die Position als Parameter übergeben werden musste. Hier meine lösung: using System.Collections; using System.Collections.Generic; using UnityEngine; using Photon.Pun; public class WallPlacing : MonoBehaviourPunCallbacks { public GameObject wallPrefab; bool isInstantiated = false; PhotonView view; void Start() { //get the photon view component view = GetComponent<PhotonView>(); } // Update is called once per frame void Update() { if (view.IsMine) { if (Input.GetKeyDown(KeyCode.LeftControl)) { view.RPC(nameof(PlaceWall), RpcTarget.All, Camera.main.ScreenToWorldPoint(Input.mousePosition)); //view.RPC(nameof(RotateWall), RpcTarget.All ); } } } //place a wall at cursos pos [PunRPC] void PlaceWall(Vector3 position) { GameObject wall = PhotonNetwork.InstantiateRoomObject(wallPrefab.name, new Vector3(position.x, position.y, 0), Quaternion.identity); } [PunRPC] void RotateWall(GameObject wall) { var dir = Input.mousePosition - Camera.main.WorldToScreenPoint(wall.transform.position); var angle = Mathf.Atan2(dir.x, dir.y) * Mathf.Rad2Deg; wall.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward); } } 2 Zitieren Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.