I didn't quite understand how this works

Since we are using the line of code:

        myRigidBody2D.velocity += velocityTweak;

Doesn’t that mean we are always ADDING the value of velocityTweak to our current velocity? So why is it that when we crank up the random factor to 10 sometimes the ball goes super fast, then super slow? Shouldn’t the velocity always be increasing??

Hi,

The problem here is that we add some random value which does not have anything to do with myRigidBody2D.velocity. In fact, we change the magnitude of the vector, and therefore also the speed. With low tweak values, you usually will not notice any difference because we constantly increase and decrease the speed slightly. The higher your tweak values are, the more you notice what is actually going on.

Actually, you would have to do proper vector maths to avoid changing the speed, which is beyond the scope of this course, though.

I didn’t really understand your response.

Blockquote
we add some random value which does not have anything to do with myRigidBody2D.velocity . In fact, we change the magnitude of the vector, and therefore also the speed.

Yeah we add the value of velocityTweak to myRigidBody2D. As far as I understand, myRigidBody2D.velocity corresponds to the velocity (magnitude of vector) of the ball right? If you do nothing but ADD to this value, wouldn’t the ball always speed up? Why does it slow down at all if we are ONLY adding positive values to it? There is something here that I am not understanding.

Do you know how vector maths works? For instance, what is the definition of speed?

Calculate the speed for these unmodified velocity values of the/a ball in Unity:
(2, 4, 0)
(-9, 0, 0)

What speed does the ball have?

Now add a random tweak vector and calculate the speed again.
(2, 4, 0) + (0.5, 0.2, 0)
(-9, 0, 0) + (0.5, 0.2, 0)

And the next time, we have a different tweak vector.
(2, 4, 0) + (-0.3, -0.1, 0)
(-9, 0, 0) + (-0.3, -0.1, 0)

Can you spot the difference?

Okay I think I have a better understanding now. I was under the impression that we were only modifying the magnitude, but it looks like we are changing the direction too. Thanks for the example.

You are welcome.

I thought it was obvious that we change the direction because the idea was to prevent an endless loop. By changing the magnitude only, our ball would just move slower/faster inside its loop. There is no reason to do that.

What most students do not know is that we also change the magnitude with our tweak, which is actually an undesired side effect.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms