001 Game Creator Guide

Facing the Cursor for 001 Game Creator

Facing the Cursor

Overview

How to make an Actor look to where the cursor is pointing.

Sprite Setup

Before we begin the scripting, you’ll have to set up your actor sprite correctly.

3D and Top Down Perspective

The first thing you want to do is check the Rotate Graphics with Direction checkbox (blue arrow). This will make the sprite or model able to freely rotate around its own axis. If you look down at the purple box, you can see that it does not look like a clock any more and only has a single arrow instead. This signals that the sprite can rotate freely.

There is one more checkbox that you should keep in mind, Rotate Collision with Direction (orange arrow). I would recommend keeping this unchecked, as it can lead to the actor becoming stuck if they’re too near another collider like a tile or another actor. It is better that you make your collision box around the sprite or model fit the bounds.

This is just a generic top down sprite, with its collision bounds set to fit it perfectly. Notice how it’s facing to the right. This is important. Otherwise the rotation angle will look a bit off.

If you’re using a 3D model, make sure it’s facing to the right as well. You can use the dials to rotate it if you need to.

45 degree Perspective

This does not require any additional steps apart from adding in the graphics for each of the directions. This mode should also have the Rotate Graphics with Direction checkbox unchecked, unless you know what you’re doing.

Scripting

Now onto the fun part, the scripting! The script is relatively straightforward.

First, go to the Actor editor of the actor you want to face the cursor (most likely your main player) and add a Timer to it. In the While Condition field, type

Actor(“this”).HP > 0

This will make sure the Timer only runs while the actor is alive. If you don’t want that, you don’t have to type anything in there.

Leave the Interval blank, unless you want to only rotate at a certain interval. Leaving it blank will execute the code every frame.

Inside the Timer

The first thing we want to do is add a Comparison Branch. In the Expression, either type

Cursor.TargetMap

or click the Use Value button and search for “Target Map” and select the option with the red box around it.

After you’ve done this, set the Operator (the drop down menu) to Not Equal and the Map to “None”.

Cursor.TargetMap returns the map that the cursor is currently over. If your cursor goes outside the map bounds, it will return “None”. If the cursor is within the bounds of the map, it’ll return the Scripting ID of that map. This is an important step, because if the player puts the cursor outside the map without this check, you’ll get errors. We don’t want that.

After you’re done, the Comparison Branch should look like this:

We’re done with the Comparison Branch for now. Now we need to add one final event, Face Direction of Location.

For the Actor, pick the actor you want to rotate.
Then, for the X parameter click the Use Value button and search for “target”. Look at the screenshot on the right, where two values are marked with red; “Targeted X(Z) and Targeted Y(Z).

For our X parameter choose Targeted X(Z). You’ll be presented with another window. In here, type

Actor(“this”).Z

This will return the current Z positon (height) of our actor.

For the Y parameter, do the same but select Targeted Y(Z) instead. Type out the same value for the Z parameter in this window.

The Z parameter should also be

Actor(“this”).Z

Only one step left, and that’s the Axis. Leave this at it is, Regular. When you’re done, it should look like this:

Now you can start your game and watch the actor look towards your cursor!

Making it Smoother

You may notice that the actor turns really fast towards the cursor. This is because it’s setting the rotation every frame. But what if you want the rotating to be smoother? Well, there’s a way to do that as well.

Instead of using the Face Direction of Location event, we’ll instead use the Change Rotation Z event, as this has a Time parameter, which will smoothly lerp the rotation in the time specified.

Next to the big dial, click the Use Value button and search for Direction You’ll want to select Direction Between Points.

You’re now presented with a new window. Inside Source X and Y, type

Actor(“this”).X

and

Actor(“this”).Y

respectively.

For Destination X and Y, we’ll write

Cursor.TargetXofZ(Actor(“this”).Z)

and

Cursor.TargetYofZ(Actor(“this”).Z)

respectively. Remember those? Once again we have to find the cursor position on the map to return the point it’s hovering.

For the Time parameter, you’ll have to experiment on your own to find what feels good. Lower values makes the rotation faster.

Just for clarity’s sake, the Direction parameter should say

Geometry.Direction(Actor(“this”).X,Actor(“this”).Y,Cursor.TargetXofZ(Actor(“this”).Z),Cursor.TargetYofZ(Actor(“this”).Z))

And there you go, your character will now face towards the cursor.
Thanks for reading, and please give a rating or leave a comment if you found this useful.

SteamSolo.com