WeaponLoader.cs 1.21 KB
using System.Collections;
using System.Collections.Generic;
using UnityEngine.XR.ARKit;
using UnityEngine.XR.ARFoundation;
using UnityEngine;

public class WeaponLoader : MonoBehaviour
{
    public GameObject _objectToBeInstantiated; // object that is going to be instantiated = prefabs 

    public Vector3 position;
    Vector3 cam_pos;
    GameObject cam;

    GameObject weapon;

    // Start is called before the first frame update
    void Start()
    {
        cam = GameObject.Find("AR Camera");
        cam_pos = cam.transform.position;

        if(cam != null)
        {
            Debug.Log("cam attatched ");
        }
        Debug.Log(cam_pos.ToString());
    }

    // Update is called once per frame
    void Update()
    {
        if (!weapon)
        {
            weapon = Instantiate<GameObject>(_objectToBeInstantiated, cam_pos - position, new Quaternion(0, 0, 0, 0));
        }
        else
        {
            cam_pos = cam.transform.position;
            
            if (weapon.transform.position != cam_pos)
            {
                weapon.transform.position = cam.transform.position - position;
                weapon.transform.rotation = cam.transform.rotation;
            }
        }    
    }
}