MouseMoves: Aiming and Moving Independently

3
  • 35 favourites

Stats

6,129 visits, 8,789 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Sprites can move to follow the cursor directly or move towards the cursor, but sometimes a game requires a sprite to simply face the mouse without actually moving.

In this example we will try making a kind of turret where the barrel of the gun aims where the mouse is without the base moving anywhere.

Two sprites will be added to the layout. One will be called the "Base" and the other the "Gun". Once it is added it is important to change 2 things about the sprite before continuing. The first is the default angle of the Gun. The angle considered zero is facing the right side shown by the little square in the middle.

We want the angle to be the same as where the Gun will be aiming so we want to rotate the sprite by clicking rotate clockwise button in the editing window.

The Gun should look like this now, with the angle facing the same way the Gun will aim.

The second thing that needs to be changed is the position of the origin point. While still in the editing window, click the image points button.

This will open the image points window. Move the origin point by clicking on the base part of the Gun.

This will be the part of the Gun that sticks to the Base sprite.

Once these two things are set we can add some events.

This says that every tick, the Gun will be at the same position as the Base sprite AND the Gun will set its angle towards the mouse X and Y coordinates.

Run the layout to test things out. The Gun should always face the mouse.

One last thing we could do is make the Base move as well. A simple way of trying this out is to add the 8-Direction behavior to the Base sprite. Just click on the sprite and add/edit behavior on the left-hand menu.

The default controls will be the arrow keys on the keyboard. So, now you can move the Base sprite and keep the Gun aimed where the mouse is.

The movement might not be exactly what you want depending on what the Base is supposed to be. It might be a tank or a person or something entirely different. You can try to create different types of movements that suit the character better.

Various Ways to Use: Beginner

Tanks and turrets

Ready, aim, fire!

Moving limbs or tools

This could be a useful way of moving the arms of a character or maybe a tool that the character is holding. Put the key in at the right angle.

Various Ways to Use: Advanced

Restricted angles

Use variable and different conditions to constrain the angle to limit the direction of firing.

Combine with charging and/or gravity

With the addition of a charging element the shots would require more precision and timing. Add some gravity and things get even more complex.

Segmented sprites

This technique can also be applied in other ways other than aiming and shooting. For example, if we combined the mouse tracking movement with the 'turret' facing the opposite direction, away from the mouse, the 'turret' becomes more of a 'tail'.

With multiple segments it could possibly become a serpent or snake.

  • 0 Comments

  • Order by
Want to leave a comment? Login or Register an account!