Cursor Aiming

Written by Luke Harrison 2007

In this tutorial you will learn how to make your character shoot bullets towards the mouse cursor.


Summary

  1. Optional: Create the Parent Bullet Object
  2. Create the Mouse Bullet Object
  3. Make the Mouse Bullet Object move towards the Mouse Point
  4. Make the Character Object fire the bullets

Download Example


Before you Start

You will need the following:

  • Character Object
  • Cursor Sprite with the origin set as the Center (Sprite Properties, click the Center button)
  • Bullet Sprite with the origin set as the Center

Create the Parent Bullet Object (opitional)

Note: This is optional, suggested if you have more than one type of bullet.

It’s a good idea to have a Parent Object of a Bullet, in this Parent object, you set all the collisions with things like walls and crates ect, not characters. You can then create other bullet objects that are fired by the player and enemies, with these bullets you can then add extra collision events while using the ones set in the parent, saving you time if you have say, 20 different types of bullets, the parent will store the default collision events.

  • Create a New Object called o_bullet_parent
    • This object does not need a sprite as it is a parent and won't be in the level
    • This object is only used to set common events for all bullets such as wall collisions.

  • Add a Collision Event with the Wall
    • Add the Destroy Instance Action
    • Set Applies To as, Self

Now you have set a default event on the parent, you can set other types of bullet as a child to this object. Doing this will give it the Collision event with the wall.

Why is this worth doing? Say you have 50 types of bullet and they all destroy when they collide with the wall, using a parent object to set all common events, you only need to code them once.


Create the Mouse Bullet Object

This will be the object that is fired when the mouse is clicked. The bullet will move towards the point the mouse was clicked.

  • Click the Add Object button.
    • Name the Object, o_bullet_mouse
    • Set the Sprite as the Bullet Sprite
    • This sprite must have it's origin set as the center.

If you created a Parent Bullet Object

  • Set the Parent as, o_bullet_parent
    • This is done via the drop down box labelled Parent in the Object Properties
    • This feature is only avalible in Advanced mode

Make the Mouse Bullet Object move towards the Mouse Point

When the bullet is created, it should be told to move towards the mouse point.

  • Open the o_bullet_mouse Object Properties

  • Add a Create Event
  • Add the, Move in Direction of Point Action from the Movement Tab.
    • Set X as, mouse_x
    • Set Y as, mouse_y
    • Set Speed as, 15 (you can change this if you want)

Setting the X and Y values as mouse_x and mouse_y tells the Bullet to move towards the current position of the Mouse.


Make the Character Object fire the bullets

Now we will make your character fire bullets when the mouse is clicked.

  • Open your Character Object

  • Add a New Event, Global Left Released
    • Mouse / Global Mouse / Global Left Released

This event is triggered when the Left Button on the Mouse is released anywhere in the screen.

The difference between the Normal Mouse Events and Global Mouse Events are, a Normal Mouse Event is triggered when the Mouse is over the Object, a Global Mouse Event is triggered wherever the Mouse is in the Screen.

  • Open the Global Left Release Event
  • Add the, Create Instance Action from the Main 1 Tab.
    • Set the Object as, o_bullet_mouse
    • Leave the X and Y values as they are unless you want to change where the Bullet shoots from.
    • Make sure you Tick Relative so the Bullet is created where the Character is.

This code will create an instance of the Mouse Bullet Object when ever the user clicks the Left Mouse Button.
The create event on the Mouse Bullet Object will make the Bullet move towards the Mouse Cursor.