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 RudyPython · Dec 19, 2014 at 11:22 PM · mouselogicpressed

Code Logic Help??

Hello, I am having this problem with my code logic. When right click is pressed, I want the weapon the character is holding to aim; but the problem is when right click is pressed instead of aiming the current weapon another weapon appear.

Can anybody help me come up with a better code logic or with this problem?

Thanks,

     public GameObject primaryWeapon;
     public GameObject secondaryWeapon;
     
     private bool drawPistol = false;
     private bool drawRifle = false;
     private bool isAimPistol = false;
     private bool isAimRifle = false;
 
     private Animator animator;

 void Start () {
     animator = GetComponent<Animator>();


 }
 
 void Update () {
         DrawPistol ();
         DrawRifle ();
         AimingP ();
         AimingR ();
 
     }
     public void DrawPistol(){
         if(Input.GetKeyUp(KeyCode.Alpha1) && !drawPistol){
             PistolDraw();
         }
         else if(Input.GetKeyUp(KeyCode.Alpha1) && drawPistol){
             unDrawPistol();
         }
     }
     public void PistolDraw()
     {
         drawPistol = true;
         animator.SetBool ("DrawPistol", true);
         secondaryWeapon.SetActive(true);
         animator.SetBool ("DrawRifle", false);
         primaryWeapon.SetActive(false);
         }
     public void unDrawPistol()
     {
         drawPistol = false;
         animator.SetBool ("DrawPistol", false);
         secondaryWeapon.SetActive(false);
         }
     public void DrawRifle()
     {if(Input.GetKeyUp(KeyCode.Alpha2) && !drawRifle){
             RifleDraw();
         }
         else if(Input.GetKeyUp(KeyCode.Alpha1) && drawRifle){
             unDrawRifle();
         }
         }
     public void RifleDraw(){
         drawRifle = true;
         animator.SetBool ("DrawRifle", true);
         animator.SetBool ("DrawPistol", false);
         primaryWeapon.SetActive(true);
         }
     public void unDrawRifle()
     {
         drawRifle = false;
         animator.SetBool ("DrawRifle", false);
         primaryWeapon.SetActive(false);
         }
     public void AimingP()
     {
         if(Input.GetKey(KeyCode.Mouse1) && !isAimPistol){
             AimPistol();
         }
         else if(Input.GetKey(KeyCode.Mouse1) && isAimPistol){
             unAimPistol();
         }
     }
     public void AimPistol(){
         isAimPistol = true;
         animator.SetBool ("AimPistol", true);
         secondaryWeapon.SetActive (true);
 
     }
     public void unAimPistol()
     {
         isAimPistol = false;
         animator.SetBool ("AimPistol", false);
     }
     public void AimingR()
     {
         if(Input.GetKey(KeyCode.Mouse1) && !isAimRifle){
             AimRifle();
             isAimPistol = false;
         }
         else if(Input.GetKey(KeyCode.Mouse1) && isAimRifle){
             unAimRifle();
         }
     }
     public void AimRifle(){
                 isAimRifle = true;
                 animator.SetBool ("AimRifle", true);
                 primaryWeapon.SetActive (true);
         }
     public void unAimRifle()
     {
         isAimRifle = false;
         animator.SetBool ("AimRifle", false);
     }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by NoseKills · Dec 20, 2014 at 02:34 PM

In short i think the main problem is that you are doing AimPistol and AimRifle every update no matter which weapon you have drawn - so no matter the selected weapon, both models are enabled when you click. You should call only one of those methods depending on drawRifle and drawPistol.

As a general advice I'd suggest you aim toward a more generic logic. The way you are doing things now, you have to write all these methods again and rethink your if()s when ever you implement a third, fourth, etc. weapon. This is because you have one class handling all the input, logic and animation.

You should aim toward a solution where each weapon has a script attached to it that knows the specifics of drawing or aiming that weapon. Then you'd have a script like this one that just changes the weapon based on number keys and calls the Aim and Draw methods with info from the weapon script.

To do this properly you would need to use inheritance. Might sound intimidating if you haven't done it before but this would be a very nice situation to use and learn it :)

In "pseudo code"... you'd do something like this.

 public class YourClass : MonoBehaviour {
 
     // keep all keycodes that are used for drawing weapons here
     private KeyCode[] weaponButtons = new KeyCode[]{KeyCode.Alpha1, KeyCode.Alpha2}; 
 
     // drag the weapon GameObjects here in an order that matches keys in the above array
      public WeaponScript[] weapons;
 
     // variable pointing to the currently draw weapon (script)
     private WeaponScript currentWeapon;
 
     // aiming or not
     private bool aiming = false;
     private Animator animator;

     void Start () {
         animator = GetComponent<Animator>();
      }

     void Update () 
     {
         if (Input.anyKeyDown()) // if any key was pressed
         {
             int i = 0;
             // check each alpha key
             while (i < weaponButtons.Length && i < weapons.Length) 
             {
                 // if that alpha key was pressed
                 if (Input.GetKeyDown(weaponButtons[i]))
                 {
                     // and if the weapon corresponding to that key is not already drawn
                     if (weapons[i] != currentWeapon)
                     {
                         DrawNewWeapon(weapons[i]); // draw that weapon
                         break; // exit the loop because we are already drawing a weapon  
                     }
                 }
                 i++;
             }
         }
 
         if (Input.GetKeyDown(KeyCode.Mouse1) && currentWeapon != null)
         {
             // weapon drawn and clicking mouse
             // aim or unaim
             ToggleAiming();
         }
     }
 
     public void DrawNewWeapon(WeaponScript newWeapon)
     {
         if (currentWeapon != null)
         {
             animator.SetBool(currentWeapon.GetDrawBoolName(), false);
             currentWeapon.gameObject.SetActive(false);
         }
 
         // set aiming and drawn state to animator
         animator.SetBool(newWeapon.GetAimBoolName(), aiming);
         animator.SetBool(newWeapon.GetDrawBoolName(), true);
         newWeapon.gameObject.SetActive(false);
     }
 
     public void ToggleAiming()
     {
         aiming = !aiming;
         animator.SetBool(newWeapon.GetAimBoolName(), aiming);
     }
 }
 
 // Make a script that's common to all weapons
 // all weapons must know what are their aim and draw animations' names
 public abstract class WeaponScript : MonoBehaviour 
 {
     // abstract methods are methods that child classes must implment
     public abstract string GetDrawBoolName();
     public abstract string GetAimBoolName();
 }
 
 // the actual weapon script you attach to your Pistol
 // extends WeaponScript so compiler knows it has the 2 needed methods
 public class PistolScript : WeaponScript {
     public override string GetDrawBoolName(){
         return "DrawPistol";
     }
     public override string GetAimBoolName(){
         return "AimPistol";
     }
 }
 
 // the actual weapon script you attach to your Rifle
 // extends WeaponScript so compiler knows it has the 2 needed methods
 public class RifleScript : WeaponScript {
     public override string GetDrawBoolName(){
         return "DrawRifle";
     }
     public override string GetAimBoolName(){
         return "AimRifle";
     }
 }

If you manage to do something like this, your code works as well with 100 weapons as it does with 2. All you have to do is make another WeaponScript. Also the drawing and aiming logic is now the same for all weapons so if there are bugs in them, you have only a few lines of code to debug and search mistakes in.

Comment
Add comment · Show 5 · 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 RudyPython · Dec 20, 2014 at 08:31 PM 0
Share

Thanks for the code logic, it helps. But the problem now is that the weapon object is showing when drawn. Am I suppose to set it up in the WeaponScript?

Thanks,

avatar image meat5000 ♦ · Dec 20, 2014 at 08:42 PM 0
Share

Accept his answer if its the solution you used!

Follow up questions should be asked separately. We do not encourage walkthrough Do-It-For-mes.

avatar image RudyPython · Dec 20, 2014 at 08:49 PM 1
Share

I understand

avatar image meat5000 ♦ · Dec 20, 2014 at 08:50 PM 0
Share

Click the tick ;)

avatar image NoseKills · Dec 20, 2014 at 11:05 PM 0
Share

I hastily wrote the code so I spotted at least one mistake there

 newWeapon.gameObject.SetActive(false);
 
 // should of course be 
 
 newWeapon.gameObject.SetActive(true);
 

probably other problems too...

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Mouse flying 0 Answers

How to make a 2Dgun shoot at the position of mouse? 3 Answers

Character Not Rotating 0 Answers

mouse dissapear 1 Answer

Get Mouse Position GUI 3 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