Time for action – creating a vector from cursor movement
Our cannonballs are propelled by an initial force that we create using the X
and Y
elements of the AddForce
vector. Up until this point, the two values were set at a constant value of 12.5
, creating the same velocity and angle each time.
In this section, we'll use the change in the X
and Y
values from our cursor to affect the dynamic X
and Y
values in our firing function so that the velocity and angle are affected by the way the cursor is moved. Perform the following steps to create the vector:
Declare two new
float
variables calledmouseX
andmouseY
next to the other variables in yourCannonScript.cs
file as shown:private int buttonWidth; private int buttonHeight; private string buttonText; public GameObject cannonballPrefab; private float mouseX; private float mouseY;
Create a new function called
CaptureTouch
inCannonScript.cs
using the following code:void CaptureTouch() { }
As swipe gestures happen over multiple frames of our game...