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 /
  • Help Room /
avatar image
0
Question by skinchlea · Mar 02, 2018 at 03:38 AM · inputcontrollerspawning

Press button to perform Action 1. Same button for Action 2. Code skips to Action 2.

OK, sorry for the weird title but I've never been able to figure this one out as i'm still learning C# and Unity in general.

I have a script for my inputs. In this script i have:

 public static bool Spawn()
 {
     return Input.GetKeyDown ("e");
 }

In another script i wish to spawn a preview of an enemy by pressing 'E'. Then i want to press 'E' again and have it spawn the real thing. This is the code:

 public void SpawnMinion()
 {
 MPreview = Instantiate (MInionPreview, PlayerAiming.CroshairHit + (Vector3.up * 0.5f), Quaternion.identity);
 PreviewSpawned = true;
 }
         
 if (PreviewSpawned == true) {
         
 MPreview.transform.position = PlayerAiming.CroshairHit + (Vector3.up * 0.5f);
         
 if (PlayerInput.Spawn()) {
 Destroy (MPreview);
 PreviewSpawned = false;
 MActual = Instantiate (MinionActual, PlayerAiming.CroshairHit + (Vector3.up * 0.5f), Quaternion.identity);}

However, because i am already pressing 'E' to spawn the preview, the game just skips the preview and takes that press and spawns the real thing. What's the best way of not having this happen. Maybe cancelling the Input for a split second?

Thank you

Comment
Add comment · 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 Sabre-Runner · Mar 02, 2018 at 04:39 AM 0
Share

It's hard to understand your code. It looks like there's a method and then some statements outside of it. Can you give a more complete picture?

2 Replies

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

Answer by Laurentino · Mar 02, 2018 at 08:51 AM

I would suggest using another bool to detect when the key has been released, in addition to the one you have to check that the preview is active.

Check the button release with something like this in your Update:

 if(Input.GetKeyUp("e"))
 {
     ActionKeyReleased = true;
 }

Then setup your spawn condition as something like this:

 if (PlayerInput.Spawn() && ActionKeyReleased)
 {
     Destroy (MPreview);
     PreviewSpawned = false;
     MActual = Instantiate (MinionActual, PlayerAiming.CroshairHit + (Vector3.up * 0.5f), Quaternion.identity);
 
     ActionKeyReleased = false;
 }
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 upasnavig90 · Mar 02, 2018 at 06:50 AM

As per my understanding with your code i suggest, u can take the count of pressing button, let us say count=0, 1. if on pressing button, count is 0-> perform action 1 and increment the count. 2. then on pressing again the same button, if count=1, perform action 2, and reset the count to '0'.

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

94 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

Related Questions

I have an issue with the code for Submenu. Someone to help me ? 1 Answer

Contollers are unusable when already plugged in when starting build/editor 0 Answers

Crouch Script Issue 0 Answers

Fix Controller Input? 1 Answer

Xbox One Controller: Left stick axis snaps to cardinal directions? 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