Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Bibrosko · Apr 19, 2019 at 10:10 PM · shootinggrabkeys

Keys conflicts when shooting or grabbing objects

I have 2 scripts using both the same keys "Fire1" and "E" to perform different actions (shooting and grabbing an item). // SHOOT SCRIPT

     void Update()
     {
         timer += Time.deltaTime;
 
         if (Input.GetButtonDown("Fire1") && timer >= timeBetweenBullets )
         {
             ShootPlus();
         }
     }

// GRAB SCRIPT

     void Update()
     {
         playerDistance = Vector3.Distance(item.transform.position, grabber.transform.position);
 
         if (playerDistance >= grabDistance)
         {
             isGrabbed = false;
         }
 
         if (isGrabbed == true)
         {
             item.GetComponent<Rigidbody>().velocity = Vector3.zero;
             item.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
             item.transform.SetParent(grabber.transform);
             item.GetComponent<Rigidbody>().useGravity = false;
             item.GetComponent<Rigidbody>().detectCollisions = true;
 
             if (Input.GetButton("E"))
             {
                 isGrabbed = false;
             }
          
             if (Input.GetButton("Fire1"))
             {
                 transform.Rotate(new Vector3 (0,100,0) * Time.deltaTime);
             }            
         }
         
         
 
     void OnMouseOver()
     {
 
         if (playerDistance <= grabDistance && structureHealth.DOF >= 3)
         {
             canGrab = true;
 
         }
 
         if (canGrab == true && Input.GetButtonDown("E"))
         {
             canGrab = false;
             isGrabbed = true;
         }
     }


Desired behaviour:


  • if the item is not grabbed and player use "Fire1"=> player can shoot

  • if item is grabbable and "E" is pressed => "isGrabbed = true" and the item is grabbed

  • if "isGrabbed = true" => the player cannot shoot. Instead, if "Fire1" is pressed, the grabbed item rotates

  • while the item is grabbed, if "E" is pressed the item is released and the player can shoot again

I guess it's only a matter of logic or using the correct "flags" but I couldn't be able to solve the problem. Anyone can help please?

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 blubbr · Apr 19, 2019 at 11:01 PM

I would put multiple if statements checking multiple conditions so the if statement goes down the list checking if the player is grabbing an item, if the player is looking at an item, etc. Sorry but I'm too lazy to type everything out so ill put an outline so you can get the concept im thinking of


 //Fire1
         if (Input.GetButtonDown("Fire1"))
         {
             if (isGrabbed == true)
             {
                 //rotate item
             } else
             {
                 //shoot
             }         
         }
 
         //E
         if (Input.GetButtonDown("E"))
         { 
 
             if (isGrabbed == true)
             {
                 //release item
                 isGrabbed = false;
             }
 
             if (canGrab == true)
             {
                 //grab item
                 isGrabbed = true;
             }
 
         }

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 Bibrosko · Apr 19, 2019 at 11:25 PM

I tried to implement the change for the "E" button but it doesn't work well. I'm not sure where to change the code. as you can see i'm using either "OnMouseOver" and "Update" methods to set the bool "isGrabbed" true or false. I guess those 2 are in conflict because on mouse over, if E is pressed, isGrabbed becomes true, but in the mean time it becomes false in "update". where should I set the bools true/false?

Same thing for the fire function. they belong to 2 different scripts (which i referenced already) but i don't know where i should set the conditions

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

106 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 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 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 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 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 avatar image

Related Questions

Shooting a projectile the longer a button is held c# 1 Answer

Finding point of mesh visible by another game object 1 Answer

Shoot when Xbox 1 trigger pressed. 0 Answers

Coding A Megaman-like Charge Shot? 1 Answer

,How do I make projectile weapons stick to enemies or targets? 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