• 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
Question by YTGameDevDave · Mar 12, 2017 at 07:48 PM · booleanaddfalseonesubtract

store increment info on true 'without losing' it when bool returns false.

I've just started learning programming and managed to script pong. This is the ricochet script for the ball. I want to add +1 to speed when the ball hits a paddle (Hitpad) but I lose the +1 when the bool returns to false. I thought this would be easy after everything but it's doing my head in. Please help.

I've tried ++ operator and even calling a method inside update to increment. But no matter what i put under bool, it won't count anymore when it turns false. So the information isn't stored seperately/independently from the bool. Is there maybe a way to increment the variable speed and then cut the connection it has from the bool? Or any other solution? Adding +1 to speed doesn't work in the collision method since it only gets called once and won't get stored.

I want to add 1 to speed every time the collider hits. Really trying to get this one...

Here's the script:

C#

 public class DotScript : MonoBehaviour { 
 
     public bool Hitwall;
     public bool Hitpad;
     public float speed;
 
     void FixedUpdate ()
 
     {
         speed = 4;
 
         speed = speed * Time.deltaTime;
 
         transform.Translate (speed, speed, 0); 
 
         //if hit is true this statement enables, if hit is false it disables.
 
         if (Hitwall) 
             transform.Translate (0, -speed * 2, 0);
         
         if (Hitpad) 
         {
             transform.Translate (-speed * 2, 0, 0);
             speed = speed + 1; //problem: when bool is false the increment is gone with it.
         }
     }
     
 
     void OnCollisionEnter (Collision hit)
 
     {
 
         if (hit.gameObject.tag == "wall") 
             Hitwall = !Hitwall;    
 
         if (hit.gameObject.tag == "paddle") 
             Hitpad = !Hitpad;
     }
 }      


Any critique or tips are much appreciated btw.

Comment

People who like this

0 Show 3
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 YTGameDevDave · Mar 12, 2017 at 03:44 PM 0
Share

So I just figured that it was because I declared the speed variable inside of my update function...

But now the ball speeds up and DIRECTLY speeds down to 0... And I have no clue why...

 public class DotScript : MonoBehaviour { 
 
     public bool Hitwall;
     public bool Hitpad;
     public float speed = 4;
 
     void Update ()
 
     {
         speed = speed * Time.deltaTime;
 
         transform.Translate (speed, speed, 0); 
 
         //if hit is true this statement enables, if hit is false it disables.
 
         if (Hitwall) 
             transform.Translate (0, -speed * 2, 0);
         
         if (Hitpad) 
         {
             transform.Translate (-speed * 2, 0, 0);
             speed = speed + 1;
         }
     }
     
 
     void OnCollisionEnter (Collision hit)
 
     {
 
         if (hit.gameObject.tag == "wall") 
             Hitwall = !Hitwall;    
 
         if (hit.gameObject.tag == "paddle") 
             Hitpad = !Hitpad;
     }
 }      
avatar image YTGameDevDave · Mar 12, 2017 at 05:36 PM 0
Share

Okay I got it to work by doing it this way...

 public class DotScript : MonoBehaviour { 
 
     public bool Hitwall;
     public bool Hitpad;
     public float speed = 4;
 
     void Update ()
 
     {
 
         transform.Translate (speed * Time.deltaTime, speed * Time.deltaTime, 0); 
 
         //if hit is true this statement enables, if hit is false it disables.
 
         if (Hitwall) 
             transform.Translate (0, -speed * Time.deltaTime * 2, 0);
         
         if (Hitpad) 
             transform.Translate (-speed * Time.deltaTime * 2, 0, 0);
         
     }
     
 
     void OnCollisionEnter (Collision hit)
 
     {
 
         if (hit.gameObject.tag == "wall") {
             Hitwall = !Hitwall;
             speed = speed + 1;
         }
         if (hit.gameObject.tag == "paddle") 
             Hitpad = !Hitpad;
     }
 }      
avatar image YTGameDevDave · Mar 12, 2017 at 05:37 PM 0
Share

But now I need to know what the difference is between this:

          speed = speed * Time.deltaTime;
  
          transform.Translate (speed, speed, 0); 

and this:

        transform.Translate (speed * Time.deltaTime, speed * Time.deltaTime, 0); 

can someone help me please? I've grown wiser already, just need to make sense out of this.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by YTGameDevDave · Mar 12, 2017 at 10:05 PM

I just figured it out...

speed = speed * Time.deltaTime;

Doesn't work cause:

deltatime with 60fps is 0.016 * 4 (speed) = 0.064 = speed

causing speed to attain a new value. Being in the update function; the calculation runs again:

(speed) 0.064 * (speed x deltatime) 0.016 = 0.001024 = speed

0.001024 * 0.016 = 0.000016384 = speed

This exponentially goes on as a quadratic function until there are too many zeroes for a float, resulting in a value of 0 for the float speed.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Mouse Methods (OnMouseDown, OnMouseOver etc) stop working after certain circumstances 0 Answers

Why is Unity registering my false boolean as true? 1 Answer

Problem: Adding and Subtracting score 1 Answer

Is "if (!bool)" the same as "if (bool == false)" ? 2 Answers

Boolean problem. 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