Code: Select all
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
public class FireBulletOnActivate : MonoBehaviour
{
public GameObject bullet;
public Transform spawnPoint;
public float fireSpeed = 20;
// Start is called before the first frame update
void Start()
{
XRGrabInteractable grabbable = GetComponent();
grabbable.activated.AddListener(FireBullet);
}
// Update is called once per frame
void Update()
{
}
public void FireBullet(ActivateEventArgs arg)
{
GameObject spawnedBullet = Instantiate(bullet);
spawnedBullet.transform.position = spawnPoint.position;
spawnedBullet.GetComponent().velocity =
spawnPoint.forward * fireSpeed;
Destroy(spawnedBullet, 5);
}
}