Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Can someone please explain to me what this particular code means?

Member

So i was planning to make a clone of this very famous android game called Colour Bump and came across a tutorial which explained how to do it. The script attached to this question is for controlling the player by using the mouse or a touch. I havent understood this script entirely so i'd appreciate if someone would explain this particular script to me. :D

• Member

In the FixedUpdate (which is called in sync of Physics) , you defined a Vector2 deltaPos which is by default (0, 0) (Vector2.zero, which is 0 on X and 0 on Y)

Now, if there is Input.GetMouseDown(0) (meaning the primary button, usually left button of the mouse, or on touch devices, the touch), we define currentMousePos = Input.mousePosition (or the touchPos of finger)

And then if lastMousePos is zero, then lastMousePos = currentMousePos

Now, in line 22, deltaPos = currentMousePos - lastMousePos

And then lastMousePos = currentMousePos

The all above code is only for reading and preserving current and last Mouse or touch position. Last is required because so we can calculate the movement relative to last touch and current touch.

Now, in line 25, movement is a Vector3 (in 3 axis, x, y, z) which is the X axis of the deltaPos (the relative position to last and current, as I stated above) , Zero on Y axis, (I think it's a 2D Game, so that there are only two movements, X and Z, not the upward Y) and the Y of deltaPos on Z Axis, which is multiplied by the speed we want.

And then , we add the force,or movement we obtained to the Rigidbody.

And then , in line 31, if there is not any mouse or touch input (as opposed to line 15, we set lastMousePos to Zero.

Thanks, this is all that I understood and written you.