Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party Doubt about mirror get object

Discussion in 'Multiplayer' started by Danisanca, Jun 12, 2021.

  1. Danisanca

    Danisanca

    Joined:
    Nov 14, 2020
    Posts:
    3
    I would like to know how I make this script get the object in scene via mouse click.
    I put identity on the canvas and on the enemy, but I can't get the object in the target variable.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using Mirror;
    6.  
    7. public class Target : NetworkBehaviour
    8. {
    9.     public CanvasTarget CvTarget;
    10.  
    11.     public GameObject prefplayer;
    12.     public Personagem player;
    13.     Vector3 touchPosWorld;
    14.     TouchPhase touchPhase = TouchPhase.Ended;
    15.  
    16.     [SyncVar]
    17.     public Vector3 mousePos;
    18.     [SyncVar]
    19.     public Vector2 mousePos2D;
    20.     [SyncVar]
    21.     public RaycastHit2D hit;
    22.  
    23.     public Camera cam;
    24.  
    25.  
    26.     // Start is called before the first frame update
    27.     void Start()
    28.     {
    29.        
    30.         CvTarget = GetComponent<CanvasTarget>();
    31.  
    32.     }
    33.  
    34.     // Update is called once per frame
    35.     void Update()
    36.     {
    37.  
    38.         if (prefplayer == null)
    39.         {
    40.             cam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
    41.             prefplayer = GameObject.FindGameObjectWithTag("DataBase").GetComponent<GamesObjectsIns>().PrefPerso;
    42.             player = prefplayer.GetComponentInParent<Personagem>();
    43.         }
    44.  
    45.  
    46.         //toutchscreen();
    47.         // touchMouse();
    48.  
    49.         mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
    50.         mousePos2D = new Vector2(mousePos.x, mousePos.y);
    51.         hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
    52.  
    53.  
    54.         if (Input.GetMouseButtonDown(0))
    55.         {
    56.          
    57.             ClientRpcTarget(mousePos, mousePos2D, hit);
    58.         }
    59.        
    60.            
    61.     }
    62.  
    63.     [ClientRpc]
    64.    public void ClientRpcTarget(Vector3 _myPos, Vector2 _myDir2d, RaycastHit2D hit)
    65.     {
    66.  
    67.         Debug.Log(hit.collider.name);
    68.         if (hit.collider != null)
    69.         {
    70.             if (hit.collider.GetComponent<Personagem>())
    71.             {
    72.                 if (hit.collider.GetComponent<Personagem>().ID != player.ID)
    73.                 {
    74.                     CvTarget.GetComponent<CanvasTarget>().setTarget(hit.collider.gameObject);
    75.  
    76.                 }
    77.  
    78.  
    79.             }
    80.             if (hit.collider.GetComponent<Monstro>())
    81.             {
    82.                 CvTarget.GetComponent<CanvasTarget>().setTarget(hit.collider.GetComponent<Monstro>().gameObject);
    83.             }
    84.  
    85.         }
    86.  
    87.  
    88.     }
    89.  
    90.     public void toutchscreen()
    91.     {
    92.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == touchPhase)
    93.         {
    94.             touchPosWorld = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
    95.  
    96.             Vector2 touchPosWorld2D = new Vector2(touchPosWorld.x, touchPosWorld.y);
    97.  
    98.             RaycastHit2D hitInformation = Physics2D.Raycast(touchPosWorld2D, Camera.main.transform.forward);
    99.  
    100.             if (hitInformation.collider != null)
    101.             {
    102.                 GameObject touchedObject = hitInformation.transform.gameObject;
    103.                 if (touchedObject.GetComponent<Monstro>())
    104.                 {
    105.                     CvTarget.GetComponent<CanvasTarget>().setTarget(touchedObject.transform.gameObject);
    106.                 }
    107.                 else if (touchedObject.GetComponent<Personagem>().ID != player.ID)
    108.                 {
    109.                     CvTarget.GetComponent<CanvasTarget>().setTarget(touchedObject.transform.gameObject);
    110.                 }
    111.             }
    112.  
    113.  
    114.         }
    115.     }
    116.  
    117.  
    118.  
    119. }
    120.  
    121.