Calculating the angle
Now you have everything you need to figure out the angle to aim the monkey at the target.
The code you wrote in the previous steps will calculate the horizontal and vertical distances between the monkey and the target.
Now you need to write some code to calculate the angle. To do this you are going to use a maths operation called atan
.
atan
takes the lengths dx
and dy
and turns them into an angle we can use in our game.
angle = atan( dy / dx )
Now let's do it!
> Open Transform and drag the set rotation of myself
block above the send every game object a message of "shoot"
block. We will set this in a moment.
> Open Operators and drag out the sin
block and click on it to change it to atan
.
> Open Operators again and drag out the numeric __+__
block. Change it to divide (÷
). Then connect it to the end of the atan
block.
Like so:
> Click the Variables button and drag out the number dy
and number dx
blocks.
> And now drag them into the divide block to make atan dy ÷ dx
.
> Then drag these blocks into the set rotation of myself
block.
> Click the Play button to test. Press around on the screen to see dx
, dy
and angle
being calculated by your code.
Take note of what works, what doesn't and what we will need to fix.