• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by DK86 · Aug 28, 2016 at 07:47 PM · vrshootinggunsteameyes

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);
 }

}

Comment

People who like this

0 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mj321 · Aug 28, 2016 at 08:28 PM 0
Share

When placing the bullet instance and adding force, try using Camera.main.transform instead of bullet.transform.

1 Reply

  • Sort: 
avatar image

Answer by DK86 · Aug 29, 2016 at 10:05 PM

I appreciate your support!! Thank you!

Comment

People who like this

0 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DK86 · Aug 30, 2016 at 02:30 AM 0
Share

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

avatar image mj321 DK86 · Aug 30, 2016 at 05:32 AM 0
Share

Sorry, i just read that your camera must be tagged "Main Camera" for this to work.

http://docs.unity3d.com//ScriptReference/Camera-main.html

avatar image DK86 mj321 · Aug 30, 2016 at 11:39 PM 0
Share

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. VRYK_ObjectAutoGrab

namespace VRTK { using UnityEngine; using System.Collections;

 public class VRTK_ObjectAutoGrab : MonoBehaviour
 {
     public VRTK_InteractableObject objectToGrab;
     public bool cloneGrabbedObject;

     private VRTK_InteractGrab controller;

     private IEnumerator Start()
     {
         controller = GetComponent<VRTK_InteractGrab>();
         if (!controller)
         {
             Debug.LogError("The VRTK_InteractGrab script is required to be attached to the controller along with this script.");
             yield break;
         }


         

         VRTK_InteractableObject grabbableObject = objectToGrab;
         if (cloneGrabbedObject)
         {
             grabbableObject = Instantiate(objectToGrab);
         }
         controller.GetComponent<VRTK_InteractTouch>().ForceStopTouching();
         controller.GetComponent<VRTK_InteractTouch>().ForceTouch(grabbableObject.gameObject);
         controller.AttemptGrab();
     }
 }

}


2. Gun

using UnityEngine; using VRTK;

public class Gun : VRTK_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);
 }

}

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

79 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Please help me solve the problem with shoot a gun(HTC VIVE) 1 Answer

Changing eye height with steamVR Camera Rig 2 Answers

realtime modification of VIVE tracking 1 Answer

How to remove this ghost image? double imaging? on Unity 2017.3.1 + SteamVR 1.1.3 + HTC Vive / Vive Pro 1 Answer

Unity Project SteamVR bindings not saving... 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges