• 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
Question by rhbrr5hrfgdfgw · Jun 19, 2013 at 12:20 PM · machinegun

Machine gun

I made a simple mac$$anonymous$$ne gun, when i enter the trigger its setting camera to true and and when i exit the trigger the camera goes back to false, all of that work for now,

i want to add:

when i enter i want to make my current weapon to off (so i wont see $$anonymous$$m)

but its not working

and when i exit the trigger set it back to true

Some codes:

 var Cam : GameObject;
 
 
 function OnTriggerEnter(c : Collider){
     
     if(c.gameObject.tag == "Player"){
         Cam.SetActiveRecursively(true);
         Pickup.curWeapon.SetActiveRecursively(false);
     }
 }
 
 function OnTriggerExit(c : Collider){
     
     if(c.gameObject.tag == "Player"){
         Cam.SetActiveRecursively(false);
         Pickup.curWeapon.SetActiveRecursively(true);
     }
 }

T$$anonymous$$s line is from the script i switch weapons Pickup.curWeapon.SetActiveRecursively(true);

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 KiraSensei · Jun 19, 2013 at 01:05 PM 0
Share

First, don't use SetActiveRecursively to hide / show the weapon, you only need to use SetActive to curWeapon. If a parent is hidden, the children are not visible, even if they are active.

I don't know what is in your cam object, it depends on what you really need to do, it may work as well, so try it.

SetActiveRecursively is of course heavier than SetActive.

Second, what is Pickup ?

What really is your problem ? A compilation problem ? An "algorithm" problem ?

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by DricoJD · Jun 19, 2013 at 01:09 PM

I ain't going to write any lines of code as I simply can not say First-person or even gun games are my strong point, I script games that do not use guns, very sorry, I will say one t$$anonymous$$ng though.

When I can not seem to make triggers make say a object go invisible and not be accessed I use render and collider switchers.

References for t$$anonymous$$s is:

 gameObject.Find("YourGameObjectName").renderer.enabled = false;
 gameObject.Find("YourGameObjectName").collider.enabled = false;

Then I would use two scripts toggle a script off and on, therefore t$$anonymous$$s stops the weapon or in your case gun from actually working when it should not be.

References for t$$anonymous$$s code is:

 <Yourgameobject>.GetComponent(yourscriptname).enabled = false;


Make a separate script to actually turn the weapon back to its normal functionality, you do t$$anonymous$$s by copying the script into a new script and changing all false's to true.

Does t$$anonymous$$s work for you?

Comment

People who like this

0 Show 4 · 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 rhbrr5hrfgdfgw · Jun 19, 2013 at 02:09 PM 0
Share

I tried this on the function OnTriggerEnter: gameObject.Find("ak").SetActiveRecursively(false);

this work but on trigger exit function i putted this line gameObject.Find("ak").SetActiveRecursively(true);

and thats giving me an error; OnTriggerExit(UnityEngine.Collider c) NullReferenceException

And also one of you asked me what Pickup is? thats the script i use to switch between weapons (this script also include the variable curWeapon)

Now i got one more bug (i have no idea why) when ever i go out of the trigger its pausing the game. i've only deleted the camera behind the gun.

avatar image KiraSensei · Jun 19, 2013 at 02:18 PM 1
Share

It is normal, the "Find" method can find a game object only if it is active. If you want to set it back to active, you need to "stock" it in a variable:

 var akGameObject = GameObject.Find("ak");
 
 akGameObject.SetActiveRecursively(false);
 ...
 akGameObject.SetActiveRecursively(true);
avatar image DricoJD · Jun 19, 2013 at 02:21 PM 0
Share

Kira, I will put that in my answer if that is okay with you?

avatar image KiraSensei · Jun 19, 2013 at 03:00 PM 0
Share

Sure, go ahead.

avatar image

Answer by rhbrr5hrfgdfgw · Jun 19, 2013 at 02:33 PM

Thank you, if i only knew i cant call the function Find to an game object w$$anonymous$$ch is not active, anyway i got the code working.

Now i got one more question;

i want it to shoot just again some bug that need to be fixed:

 if(Input.GetButton("Fire1")){
 var bulletT : Rigidbody = Instantiate(bullet, spawn.position, spawn.rotation);
 bulletT.AddForce(transform.forward * 1000);
 }

spawn variable (that in the code) is a transform and its the spawn bullet position. bullet variable is a GameObject.

The problem is its shooting the bullet to the right instead of forward.

Comment

People who like this

0 Show 8 · 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 rhbrr5hrfgdfgw · Jun 20, 2013 at 10:57 AM 0
Share

The other problem still not fixed.

avatar image KiraSensei · Jun 20, 2013 at 11:31 AM 0
Share

You wanted to know how to enable and disable the weapon, we told you how to.

What is your question ?

avatar image rhbrr5hrfgdfgw · Jun 20, 2013 at 02:26 PM 0
Share

I said;

Thank you, if i only knew i cant call the function Find to an game object which is not active, anyway i got the code working.

Now i got one more question;

i want it to shoot just again some bug that need to be fixed:

 if(Input.GetButton("Fire1")){
 var bulletT : Rigidbody = Instantiate(bullet, spawn.position, spawn.rotation);
 bulletT.AddForce(transform.forward * 1000);
 }



spawn variable (that in the code) is a transform and its the spawn bullet position. bullet variable is a GameObject.

The problem is its shooting the bullet to the right instead of forward.

avatar image KiraSensei · Jun 20, 2013 at 03:01 PM 0
Share

You're asking a question that is not related to the first one. You should have opened another question, not put it in the same ...

Did you check the spawn.rotation value ? there may be a 90° somewhere in it that you don't want.

avatar image DricoJD · Jun 20, 2013 at 03:20 PM 0
Share

Like Kira said check the rotation of the spawn.rotation it shouldn't have these values 90 or - 90. If that does not work check the position of the bullet from which it fires

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Help make Machine Gun damage enemies and not player 2 Answers

Modified Machine Gun Script Error 0 Answers

parented/constrained object transform lags when moving character 0 Answers

Adding sound to MachineGun 4 Answers

FPS Game Machine Gun controls 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