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 thomas-bird · Apr 11, 2019 at 11:14 AM · 2d game2d-gameplayupdate functionupdate problem

how to let user only have one go at moving?

so I'm creating a 2d game where there is a ball on top of a box, the idea is to drop the box and ball down as low as possible without it dropping below the bottom of the screen or the closest to a point or something. The box and ball will spawn mid screen, and you hold click (mouse button) to move the box downwards with the ball falling on top of it. So far i have got the movement of the box working with this code:

   public Rigidbody2D rb;
     public Vector2 moveVelocity;
     public float speed;
 
 
     private void Start()
     {
         rb = GetComponent<Rigidbody2D>();
 
     }
 
 
 
     public void Update()
     {
        
         Vector2 moveInput = new Vector2(0, -Input.GetAxisRaw("Fire1"));
         moveVelocity = moveInput.normalized * speed;
        
     }
 
     void FixedUpdate()
     {
         rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime);
    
     }

however this allows you to move the box whenever you would like. What i want it to do is that you will start the game have one go at moving the box down and then you cannot move the box anymore after that first go. i believe that the problem is in the update function and the fact that it moves the box down by -1 every frame rather than once for the duration of your click. if there were some way of making it move the box down for the duration of your click, then setting a variable such as HasTried = true, then i could stop them from being able to move again. but the fact it moves the box frame by frame i cannot "instance" the click/hold in any way. Hope you understand what i am trying to do!

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

Answer by MrRightclick · Apr 11, 2019 at 12:45 PM

Just make a boolean check for when the box should move, and use Input.GetMouseButtonUp event to check when the player releases the button for the first time.

 public bool hasTried = false;
 
 void Update () {
 
         if (!hasTried) {
             // Your input & movement code here
         }
 
         if (Input.GetMouseButtonUp(0)) hasTried = true;
 }


Also if you're using velocity to move the box, you should probably set the rigidbody's velocity to zero once you want the box to stop moving.

Comment
Add comment · Show 2 · 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 thomas-bird · Apr 11, 2019 at 05:07 PM 0
Share

It worked thank you so much!

avatar image MrRightclick thomas-bird · Apr 12, 2019 at 07:47 AM 0
Share

Hey, glad to be of help. Please remember to set the working answer as Best Answer so the question can be closed. Happy coding!

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

122 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

Related Questions

,Spawning snow or changing tilesets to snow as the player walks past them 0 Answers

how to do an hot update of android and ios build 0 Answers

Adding knockback to a 2D game (in C#) 2 Answers

How can I stop hurt animation and bounce the player when hit by an enemy? 0 Answers

2d Iteam respawn in Spawn point 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