Search Unity

Instantiate Object at Cursor Position

Discussion in 'Multiplayer' started by BeatenByBacon, Jun 8, 2021.

  1. BeatenByBacon

    BeatenByBacon

    Joined:
    Sep 8, 2017
    Posts:
    13
    Hey developers.
    I am pretty new on developing a multiplayer game, but I wanted to give it a try.
    I try to create a multiplayer game (based on Photon), where a mechanic is that players can spawn walls at the position, their cursor is pointing. For some reason it doesn't work completely. The host's instantiated objects are at the correct cursor position, while the client's instantiated objects are not at the right spot of the world space. Here's a picture to visualize my problem better:

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

    You can see in the console that the positions of the client are not in the actual game area, as well of course the walls as well.

    Here's the code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5.  
    6. public class WallPlacing : MonoBehaviourPunCallbacks
    7. {
    8.     public GameObject wallGameObject;
    9.     bool isInstantiated = false;
    10.  
    11.     PhotonView view;
    12.     void Start()
    13.     {   //get the photon view component
    14.         view = GetComponent<PhotonView>();
    15.     }
    16.  
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if (view.IsMine)
    22.         {
    23.             if (Input.GetKeyDown(KeyCode.LeftControl))
    24.             {
    25.                 view.RPC(nameof(PlaceWall), RpcTarget.All);
    26.             }
    27.         }
    28.     }
    29.  
    30.     //place a wall at cursos pos
    31.     [PunRPC]
    32.     void PlaceWall()
    33.     {
    34.  
    35.         Vector2 cursorPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    36.  
    37.      
    38.         PhotonNetwork.InstantiateRoomObject(wallGameObject.name, new Vector3(cursorPos.x, cursorPos.y, 1), Quaternion.identity);
    39.         //GameObject wall = Instantiate(wallGameObject, new Vector3(cursorPos.x, cursorPos.y, 1), Quaternion.identity);
    40.         Debug.Log(cursorPos);
    41.      
    42.     }
    43. }
    44.  
    Does someone know how to fix this Problem?
    Greetings
     
    Last edited: Jun 8, 2021
    usama0islam likes this.
  2. usama0islam

    usama0islam

    Joined:
    Mar 13, 2021
    Posts:
    1
    bro du u get any answer please let me know i have same problem