Jump to content
Unity Insider Forum

Instantiate GameObject an Cursor Position (Multiplayer / Photon)


Recommended Posts

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:

upload_2021-6-8_9-40-43-png.867658

 

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 

Link zu diesem Kommentar
Auf anderen Seiten teilen

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);
    }
}

 

Link zu diesem Kommentar
Auf anderen Seiten teilen

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gast
Auf dieses Thema antworten...

×   Du hast formatierten Text eingefügt.   Formatierung jetzt entfernen

  Only 75 emoji are allowed.

×   Dein Link wurde automatisch eingebettet.   Einbetten rückgängig machen und als Link darstellen

×   Dein vorheriger Inhalt wurde wiederhergestellt.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Lädt...
×
×
  • Neu erstellen...