- Home /
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.
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 : $$anonymous$$onoBehaviour {
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;
}
}
Okay I got it to work by doing it this way...
public class DotScript : $$anonymous$$onoBehaviour {
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;
}
}
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.
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.
Your answer
Follow this Question
Related Questions
The public bool will not change unless i set it 1 Answer
Why won't my if result to true? 1 Answer
Adding a life on collision, otherwise subtract 1 Answer
Why is Unity registering my false boolean as true? 1 Answer
Boolean problem. 1 Answer