Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 lsugzdinis · Dec 28, 2020 at 04:24 PM · button3dgunaimgun script

when float is less then zero, do action once

I'm making a game where time slows down when you aim. There's a timer so that you can't aim forever. When the timer goes off, I want the gun to move to the right once, but I can't seem to figure it out.

I'm fairly new to Unity, so any help would be appreciated.

Here's the code:

 public float currentAim;
 public float aimWait;
 public bool gunAiming;
 public bool gunTimeout;
 public bool gunMoved;
 public bool gunMovedTrue;

  void Start()
 {
     currentAim = maxAim;
     gunAiming = false;

     aimWait = 1f;

     gunMoved = false;
 }

 // Update is called once per frame
 void Update()
 {
     if(Input.GetKeyDown(KeyCode.Mouse0))
     {
         Shoot();
     }

     if(currentAim <= 0 && gunMovedTrue)
     {
         gunMoved = true;
         gunMovedTrue = false;
     }
     else
     {
         gunMoved = false;
     }

     if(currentAim <= 0)
     {
         //make guntimeout always true
         gunTimeout = true;

         //make gunmoved true once and then false
       //  gunMoved = true;
     }
     else
     {
         gunTimeout = false;
         gunMoved = false;
     }

     if(gunTimeout)
     {
         gunMovedTrue = true;
     }

     if(Input.GetKeyDown(KeyCode.Mouse1))
     {
         gun.transform.Translate(Vector3.right);
         gunAiming = true;
     }

     if (Input.GetKeyUp(KeyCode.Mouse1))
     {
         gun.transform.Translate(Vector3.left);
         gunAiming = false;
     }

     Aim();

     if (Input.GetKey(KeyCode.Mouse1))
     {
         gunAiming = true;
     }
     else
     {
         gunAiming = false;
     }

     
 }

 void Aim()
 {
     if (gunAiming)
     {
         if (gunTimeout)
         {
             if (gunMoved)
             {
                 gun.transform.Translate(Vector3.left);
                 gunMoved = false;
             }
             else
             {
                 Time.timeScale = 1f;
                 camera.fieldOfView = 60;
             }
         }
         else
         {
                 Time.timeScale = 0.1f;
                 camera.fieldOfView = 45;
                 currentAim -= Time.deltaTime * 10;
         }



     }

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
0

Answer by sacredgeometry · Dec 28, 2020 at 06:57 PM

You could do it a few ways. I would use c#s events/ delegates personally but the easiest way I can think of is just to add it to the property setter.

i.e.

 public bool GunTimeout;
 
 private float _currentAim = MaxAim;
 public float CurrentAim 
 {
     get 
     {
         return currentAim;
     }
     set
     {
         GunTimeout = value <= 0.0f;
         _currentAim = value;
     }
 }



This way every time you set the CurrentAim to another value it updates the GunTimout bool with the correct value.

Comment
Add comment · Show 7 · 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 lsugzdinis · Dec 28, 2020 at 07:10 PM 0
Share

Which function is that in?

avatar image sacredgeometry lsugzdinis · Dec 28, 2020 at 07:15 PM 0
Share

I am not sure what you mean. I haven't written a function. I have just written a field and a field backed property.

avatar image lsugzdinis sacredgeometry · Dec 28, 2020 at 07:34 PM 0
Share

now unity just crashes when I hit play

Show more comments

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

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

Related Questions

How to move gun from middle of screen to right of screen? 1 Answer

WaitForSecond Corountine Sleep Time Varies 2 Answers

Gun script not working 3 Answers

Using a model preview as a image for a button 1 Answer

Gun not smoothly following player,How to make gun smoothly follow player 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