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 UnityUnited · Dec 31, 2011 at 03:44 PM · weaponhowattachtoa

Add Weapon

I need help attaching a weapon to a firstPersonController.. How do you attach a weapon to a FirstPersonController?

Comment
Add comment · Show 2
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 ByteSheep · Dec 31, 2011 at 04:01 PM 1
Share

You can make it a child object of the controller so it will always follow the controller around, happy new year!

avatar image Lo0NuhtiK · Dec 31, 2011 at 04:19 PM 1
Share

Child it to the controller's camera though, NOT the controller itself ; otherwise you'll be back asking how to get your weapon to quit moving goofy when you make your player look around.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by aldonaletto · Dec 31, 2011 at 06:02 PM

The best way is to child all weapons to the camera, like @Lo0NuhtiK said - this way every weapon will point to the direction you're looking at. Set each weapon's position/rotation in the Editor, and attach this script to the camera to select the weapon and fire:

function Start(){ SelectWeapon(0); // select weapon 0 at Start; // if you want to have no weapons initially, use SelectWeapon(-1) }

function SelectWeapon(num: int){ for (var i=0; i < transform.childCount; i++){ // activate the weapon index "num", and deactivates all the others: transform.GetChild(i).gameObject.SetActiveRecursively(i == num); } }

function Update(){ // repeat the code below for each weapon: if (Input.GetKeyDown("1")) SelectWeapon(0); if (Input.GetKeyDown("2")) SelectWeapon(1); if (Input.GetKeyDown("3")) SelectWeapon(2); // this is the fire code: if (Input.GetButtonDown("Fire1")){ transform.BroadcastMessage("Fire"); } } HOW IT WORKS: when you press keys 1, 2, 3 etc. the function SelectWeapon activates only the selected weapon and deactivates all the others. If you pass a non-existent index to SelectWeapon, all weapons are deactivated. When you press the button Fire1 (left mouse button by default), the function Fire will be called in the active weapon, thus you must write a Fire function in each weapon to effectively shoot - something like this (script suitable for fast bullet weapons like rifles, guns etc. - must be attached to the weapon):

var shotSound: AudioClip; // drag the shot sound here at the Inspector

function Fire(){ var hit: RaycastHit; if (Physics.Raycast(transform.position, transform.direction, hit)){ if (shotSound) audio.PlayOneShot(shotSound);
hit.transform.SendMessage("ApplyDamage", 5, SendMessageOptions.DontRequireReceiver) } } This approach is used in the First Person Tutorial - download the Completed version and see how they set up the 2 weapons.

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

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

7 People are following this question.

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

Related Questions

Making a Game like 'Kitty Cannon'? 1 Answer

Textfield in javascript, make unity read it on pressing enter 1 Answer

How to update unity web player 0 Answers

How to check my Game System Requirements? 2 Answers

Question about network items 0 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