Online multiplayer AR

ARCamPos

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ARCamPos : MonoBehaviour
{
    internal GameObject Player;
    internal Vector3 pos; //current position
    internal Vector3 Rot; //current rotation
    public float MovementSpeed;
    internal bool StartPos = false;
    void FixedUpdate()
    {
        if (StartPos == true)
        {
            pos = new Vector3(transform.position.x, -0.3f, transform.position.z);
            Rot = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z);
            Player.transform.eulerAngles = new Vector3(Player.transform.eulerAngles.x, Rot.y, Player.transform.eulerAngles.z);
            Player.transform.position = Vector3.Lerp(Player.transform.position, pos, MovementSpeed);
        }
    }
}


ARClicktoPlace

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
public class ARClicktoPlace : MonoBehaviour
{
    public GameObject Model;
    public GameObject objectToPlace;
    public GameObject placementIndicator;
    private ARSessionOrigin arOrigin;
    private ARRaycastManager arRaycast;
    private Pose placementPose;
    private bool placementPoseIsValid = false;
    private bool onetime = true;
    public Camera ARCam;
    void Start()
    {
        arOrigin = FindObjectOfType();
        arRaycast = FindObjectOfType();
    }
    void Update()
    {
        if( onetime )
{
            UpdatePlacementPose();
            UpdatePlacementIndicator();
            if (placementPoseIsValid && Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
            {
                PlaceObject();
            }
        }
    }
    private void PlaceObject()
    {
        if (onetime == true)
        {
            GameObject obj = Instantiate(objectToPlace, placementPose.position, placementPose.rotation);
            Model.transform.localPosition = obj.transform.localPosition;
            Model.transform.localRotation = obj.transform.localRotation;
            onetime = false;
            placementIndicator.SetActive(false);
            GetComponent().enabled = false;
        }
    }
    private void UpdatePlacementIndicator()
    {
        if( onetime )
{
            if (placementPoseIsValid)
            {
                placementIndicator.SetActive(true);
                placementIndicator.transform.SetPositionAndRotation(placementPose.position, placementPose.rotation);
            }
            else
            {
                placementIndicator.SetActive(false);
            }
        }
    }
    private void UpdatePlacementPose()
    {       
        var screenCenter = ARCam.ViewportToScreenPoint(new Vector3(0.5f, 0.5f));
        var hits = new List();
       
        arRaycast.Raycast(screenCenter, hits, TrackableType.Planes);
        placementPoseIsValid = hits.Count > 0;
        if (placementPoseIsValid)
        {
            placementPose = hits[0].pose;
            var cameraForward = Camera.main.transform.forward;
            var cameraBearing = new Vector3(cameraForward.x, 0, cameraForward.z).normalized;
            placementPose.rotation = Quaternion.LookRotation(cameraBearing);           
        }
    }
}


CharacterManager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class CharacterManager : MonoBehaviour
{
    public GameObject Model; //That mesh we want to show as our character
    public PhotonView photonView;
    internal ARCamPos ARCam;
    private void Start()
    {
        Invoke("Process",3);
    }
    void Process()
    {
        if (photonView.IsMine == true)
        {
            ARCam = GameObject.Find("AR Camera").GetComponent();
            ARCam.Player = gameObject;
            Model.SetActive(false);
            ARCam.StartPos = true;
        }
        if (photonView.IsMine == false)
        {
            Model.SetActive(true);
        }
            gameObject.name = photonView.Owner.NickName;
    }
}



PosTriggerSystem

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class PosTriggerSystem : MonoBehaviour
{
    internal int ID; // The ID of this trigger
    public SetDataSystem setdate;
    private void OnTriggerEnter(Collider obj)
    {
        print(gameObject.name + " Says that " + obj.transform.name + " is here !");
        if (obj.GetComponent().IsMine == true)
        {
            setdate.CheckCurrentPos(ID, obj.name);
        }
    }
}



SetDataSystem

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using UnityEngine.UI;
public class SetDataSystem : MonoBehaviour
{
    private PhotonView m_photonview;
    public GameObject[] TriggerPos; // The list of triggers for movement and add ID automatically
    public GameObject Player;
    public float PlayerMovementTime; // When player wants to move by this script, It will be speed of lerp (smooth movement)
    public int ID;
    public float dist; // The distance between player and trigger object (For manage the lerp process)
    internal bool PosActivator = false;
    private void Awake()
    {
        m_photonview = GetComponent(); //Make sure that your object needs to have Photonview component
        for (int i = 0; i < TriggerPos.Length; i++)
        {
            TriggerPos[i].GetComponent().ID = i;
            TriggerPos[i].name = "";
            TriggerPos[i].name = "Trigger" + i;
        }
    }
    private void Start()
    {
        Invoke("PosActivator",4.2f);
    }
    void StartPosSet()
    {
        PosActivator = true;
    }
    public void CheckCurrentPos(int TargetID, string objname)
    {
        m_photonview.RPC("RPC_Pos", RpcTarget.All, TargetID, objname);
    }
    [PunRPC]
    private void RPC_Pos(int TargetID, string objname)
    {
        ID = TargetID;
        GameObject obj = GameObject.Find(objname);
        Player = obj;
        print(TargetID + objname);
    }
    private void Update()
    {
        if (PosActivator == true)
        {
            if (TriggerPos[ID] != null && Player != null)
            {
                dist = Vector3.Distance(Player.transform.position,TriggerPos[ID].transform.position);
            }
            if (dist > 0.1f)
            {
                Player.transform.position = Vector3.Lerp(Player.transform.position,TriggerPos[ID].transform.position,PlayerMovementTime*Time.deltaTime);
                print("Lerping !!!");
            }
        }
    }
}



Content ©2023 Abolfazl Tanha All Rights Reserved​​​​​​​