How to attach shooting gun to my eyes???(HTC vive) When I press the button on the controller, it should fire from my eyes. But I can't find the way!! please help me what to do!! Best regards,
using UnityEngine; using VRTK;
public class Gun : VRTK_InteractableObject { private GameObject bullet; private float bulletSpeed = 1000f; private float bulletLife = 5f;
public override void StartUsing(GameObject usingObject)
{
base.StartUsing(usingObject);
FireBullet();
}
protected override void Start()
{
base.Start();
bullet = transform.Find("Bullet").gameObject;
bullet.SetActive(false);
}
private void FireBullet()
{
GameObject bulletClone = Instantiate(bullet, bullet.transform.position, bullet.transform.rotation) as GameObject;
bulletClone.SetActive(true);
Rigidbody rb = bulletClone.GetComponent<Rigidbody>();
rb.AddForce(-bullet.transform.forward * bulletSpeed);
Destroy(bulletClone, bulletLife);
}
}
When placing the bullet instance and adding force, try using Camera.main.transform ins$$anonymous$$d of bullet.transform.
Answer by DK86 · Aug 29, 2016 at 10:05 PM
I appreciate your support!! Thank you!
Unfortunately,,, it doesn't still work ....
Can you show me the correct code?????
When I put the code"Camera.main", some error happened (Camera.main doesn't contain other definitions)
Please help me fix the issue
Sorry, i just read that your camera must be tagged "$$anonymous$$ain Camera" for this to work.
There are two scrips to attach the gun to front view of camera Please help me fix the problem Tell me how to solve this...... I really need your help Please save my life
1. VRY$$anonymous$$_ObjectAutoGrab
namespace VRT$$anonymous$$ { using UnityEngine; using System.Collections;
public class VRT$$anonymous$$_ObjectAutoGrab : $$anonymous$$onoBehaviour
{
public VRT$$anonymous$$_InteractableObject objectToGrab;
public bool cloneGrabbedObject;
private VRT$$anonymous$$_InteractGrab controller;
private IEnumerator Start()
{
controller = GetComponent<VRT$$anonymous$$_InteractGrab>();
if (!controller)
{
Debug.LogError("The VRT$$anonymous$$_InteractGrab script is required to be attached to the controller along with this script.");
yield break;
}
VRT$$anonymous$$_InteractableObject grabbableObject = objectToGrab;
if (cloneGrabbedObject)
{
grabbableObject = Instantiate(objectToGrab);
}
controller.GetComponent<VRT$$anonymous$$_InteractTouch>().ForceStopTouching();
controller.GetComponent<VRT$$anonymous$$_InteractTouch>().ForceTouch(grabbableObject.gameObject);
controller.AttemptGrab();
}
}
}
2. Gun
using UnityEngine; using VRT$$anonymous$$;
public class Gun : VRT$$anonymous$$_InteractableObject { public static Camera main; private GameObject bullet; private float bulletSpeed = 2000f; private float bulletLife = 1f; private AudioSource gunAudio;
public override void StartUsing(GameObject usingObject)
{
main = GetComponentInParent<Camera>();
base.StartUsing(usingObject);
FireBullet();
}
protected override void Start()
{
gunAudio = GetComponent<AudioSource>();
base.Start();
bullet = transform.Find("Bullet").gameObject;
bullet.SetActive(false);
}
private void FireBullet()
{
GameObject bulletClone = Instantiate(bullet, bullet.transform.position, bullet.transform.rotation) as GameObject;
bulletClone.SetActive(true);
Rigidbody rb = bulletClone.GetComponent<Rigidbody>();
rb.AddForce(-bullet.transform.forward * bulletSpeed);
Destroy(bulletClone, bulletLife);
}
}