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 to post
Share on other sites

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

 

  • Like 2
Link to post
Share on other sites

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

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

Loading...
×
×
  • Create New...