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 Ascend_Dark · Jun 19, 2015 at 07:58 AM · unity 4.6

FPS Weapon Switch Script

Hi i am currently working on a FPS and i was wondering how i would do a weapon switch animation where when i press "2" the gun will move down and the pistol will come out any ideas? i was testing this script but i cant see it slowly go down???

 var GunUp : Vector3;
 var GunDown : Vector3;
 var GunMoveSpeed = 0.5;
 
 function Start () 
 {
 transform.localPosition = GunUp;
 }
 
 function Update ()
 {
         if(Input.GetKey("2")){
             transform.localPosition = Vector3.Lerp(transform.localPosition,GunDown,GunMoveSpeed*Time.deltaTime);
     }
 
         if(!Input.GetKey("2")){
             transform.localPosition = Vector3.Lerp(transform.localPosition,GunUp,GunMoveSpeed*Time.deltaTime);
     }
 }
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 kieknai · Jun 19, 2015 at 02:07 PM 0
Share

Well in your code it goes in first if statemen only while you hold key 2. You can call coroutine and in it move the object, or just create a regular animation to move it and not through code

avatar image Hellium · Jun 19, 2015 at 02:23 PM 0
Share

The followinf statement doesn't make sense for what you are trying to achieve, please read the manual for user input.

 if(!Input.Get$$anonymous$$ey("2")){
          transform.localPosition = Vector3.Lerp(transform.localPosition,GunUp,Gun$$anonymous$$oveSpeed*Time.deltaTime);
 }


While you don't hold the key 2, this line of code will be called, I don't think you want this to happen

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by rhbrr5hrfgdfgw · Jun 19, 2015 at 02:38 PM

I don't understand do you want this to work with two guns? if you do:(assuming they you want both weapons to end up in the exact vector

Should work

  var GunUp : Vector3;
  var Gun : GameObject;
  var pistol : GameObject;
  var GunDown : Vector3;
  var GunMoveSpeed = 0.5;
  
  function Start () 
  {
  transform.localPosition = GunUp;
  }
  
 function switchGuns(var gun : GameObject, var pistol : GameObject){
 
  if(Input.GetKeyUp(KeyCode.Alpha1)){
                    pistol.transform.localPosition = Vector3.Lerp(pistol.transform.localPosition,GunDown,GunMoveSpeed*Time.deltaTime);
 
                    gun.transform.localPosition = Vector3.Lerp(gun.transform.localPosition,GunUp,GunMoveSpeed*Time.deltaTime);
 
    }
 
  if(Input.GetKeyUp(KeyCode.Alpha2)){
                    gun.transform.localPosition = Vector3.Lerp(gun.transform.localPosition,GunDown,GunMoveSpeed*Time.deltaTime);
 
                    pistol .transform.localPosition = Vector3.Lerp(pistol.transform.localPosition,GunUp,GunMoveSpeed*Time.deltaTime);
 
    }
 }
 
  function Update ()
  {
     switchGuns(Gun,pistol);//Put in editor the gameobjects
  }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Change from Editor utility to Game Object ,Change Editor Utility in to Game Object 0 Answers

UI 4.6: custom anchors breaking sizeDelta 0 Answers

Unity 4.6: Change Default GUI Skin? 1 Answer

Unity 4.6 Buttons not responding in second canvas? 7 Answers

FPS using mouse to move rather than keys 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