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 EtherealPineapple · Apr 29, 2016 at 01:02 AM · c#scripting problemtranslatevoid

C# | What void do I put in if I want to do something once each click?

I have this aiming script in which the gun moves to the left (and the sights align) when I click the right mouse button. But the problem is that it doesn't move by the number of units I want. Either I'm not using the translation correctly or I'm using the wrong void because when I right-click, the gun keeps moving until I let go. Here's the script:

 public bool isAiming = false;
  
          if(isAiming = false && Input.GetKey(KeyCode.Mouse1)){
              transform.Translate (Vector3.left * 1.24822076f);
              isAiming = true;
          }
          if(isAiming = true && Input.GetKey(KeyCode.Mouse1)){
                  transform.Translate (Vector3.right * 1.24822076f);
                  isAiming = 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

2 Replies

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

Answer by AlbinoStoic · Apr 29, 2016 at 01:22 AM

@Creator123

Hello!

There are numerous ways to approach this issue, the easiest might be using Update() and fetching the MouseButtonDown on (1) , then logging this to a Class variable (isAiming) as you had already started to do. The order of events matters if doing it this way.

 // public bool isAiming = false;
 // public bool wasAiming = false; // I test with double variable
 void Update() { // Called every frame update
   isAiming = Input.GetMouseButton(1); // Stays true if still down

   if (!wasAiming && isAiming) // If we only started aiming
     transform.Translate (Vector3.left * 1.24822076f);
   if (wasAiming && !isAiming) // If we only stopped aiming
     transform.Translate (Vector3.right * 1.24822076f);

   wasAiming = isAiming;
 }

Thus each is only called once, based on the state of wasAiming. (note that I have no clause of if both are true, or if both are false; therefore they're never called multiple times.)

Comment
Add comment · Show 4 · 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 EtherealPineapple · Apr 29, 2016 at 08:35 PM 0
Share

Alright, I did this and it worked, but it didn't move by the amount I wanted. It moved 0.4 more.

avatar image AlbinoStoic EtherealPineapple · Apr 30, 2016 at 01:11 AM 0
Share

You should generally not use the Translate to move a specific amount, you should ins$$anonymous$$d have a GunClose and a GunFar "Empty" attached to the player in the positions you want it to be.

Then you can move to the positions of these empties.

avatar image EtherealPineapple AlbinoStoic · Apr 30, 2016 at 02:13 PM 0
Share

Okay, and how would I do that? Would it be:

 GameObject.transform.position = otherGameobject?
Show more comments
avatar image
1

Answer by $$anonymous$$ · Apr 29, 2016 at 07:36 PM

The main mistake in the code is using = instead of == in the if-statements

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

154 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 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

How to instantiate (MonoBehaviour) script multiple times and call void? 1 Answer

Entire contents of public void skipped except for one line 1 Answer

Translate C# into Javascript 0 Answers

Can't reference script. 1 Answer

Mechanic that propels character in a certain direction 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