Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by ThatBroFromFinland · May 01, 2015 at 07:41 PM · shootingprojectiledisableweaponenable

how to only be able to shoot if an object is active

hi, ive been trying to have system in wich i could only shoot a projectile if my bow is active. I want the projectile to shoot from my main camera so i cant put the script under my bow. Any ideas how i could get it to work? Here is the script im using.

 #pragma strict
 
 var arrow : Rigidbody;
 var arrowSpeed = 25;
 
 function Start () {
 
 }
 
 function Update () {
 
      if (Input.GetButtonDown("Fire1"))
     {
         var clone = Instantiate(arrow, transform.position, transform.rotation);
         clone.velocity = transform.TransformDirection(Vector3(0, 0, arrowSpeed));
     }
 }

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Eno-Khaon · May 01, 2015 at 07:55 PM

I certainly don't see why you can't shoot from your camera using the bow as the trigger.

To give an example of a way of doing it:

 // Camera script

 var currentWeapon: GameObject; // Set this variable to be your bow or any other weapon you may equip. The currently-equipped weapon would use this slot.

 function Update()
 {
      if(currentWeapon != null)
      {
           if(Input.GetButtonDown("Fire1"))
           {
                currentWeapon.GetComponent(WeaponScript).Fire(transform.position);
           }
      }
 }

And...

 // Weapon script
 
 var projectile: GameObject; // renamed from arrow to be generic, in case more than just a bow is usable
 var projectileSpeed: float = 25;
 
 function Fire(camera: Transform)
 {
      var clone = Instantiate(projectile, camera.position, camera.rotation);
      clone.GetComponent(Rigidbody).velocity = camera.transform.TransformDirection(Vector3(0, 0, projectileSpeed));
 }


Edit: Made a small change for safety.

Comment
Add comment · 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
0

Answer by Addyarb · May 01, 2015 at 07:58 PM

Just do a simple check to see if your bow is activeInHierarchy.

Assuming you don't have a way (yet) to enable/disable your bow...

 GameObject bow; //drag your bow into the inspector to here.
 bool isActive; //A bool to control whether or not our bow is active.
 
 
 void Update(){
   if(Input.GetKeyDown(KeyCode.B)){  //if we press the B key..
      isActive = !isActive; //Set the bool to the opposite of what is was.
      SetWeaponActive(isActive); //Call our SetWeaponActive method with said bool.
    }
 
 //If we click AND our bow is active...
    if(Input.GetMouseButton(0) && isActive){
      //Fire your bow projectile here..
    }
 }
 
 //Function to set the bow active or inactive based on our bool in 
 //the above update function.
 void SetWeaponActive(bool myBool){
      bow.SetActive(myBool);
 }
Comment
Add comment · Show 1 · 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 ThatBroFromFinland · May 01, 2015 at 08:41 PM 0
Share

Hey man! Thanks this worked perfectly!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Alternatives to GameObject.SetActive(...) 2 Answers

Toggling the light with lightmapping(baked and dynamic shadows) 1 Answer

How to Enable and Disable Prefabs -1 Answers

Why does Unity disable the Collider when I Destroy the Rigidbody? (2D) 1 Answer

How to change settings to a rigidbody when adding it to a gamobject? 2 Answers


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