Unreal Development Kit Game Programming with UnrealScript:Beginner's Guide
上QQ阅读APP看书,第一时间看更新

Time for action – Using rotators

Before we use rotators on our AwesomeActor, we need to add some visual clue to let us know that it's actually rotating. To do that we're going to add another bit to our default properties like the sprite, but this time it will be an arrow we'll be able to see in the editor.

  1. Below the sprite in the default properties, add this:
    Begin Object Class=ArrowComponent Name=Arrow
    End Object
    Components.Add(Arrow)
  2. We're going to log our actor's current rotation, so inside our PostBeginPlay add this:
    'log("Rotation:" @ Rotation);

    Our class should now look like this:

    class AwesomeActor extends Actor
        placeable;
    
    function PostBeginPlay()
    {
        'log("Rotation:" @ Rotation);
    }
    
    defaultproperties
    {
        Begin Object Class=SpriteComponent Name=Sprite
            Sprite=Texture2D'EditorResources.S_NavP'
        End Object
        Components.Add(Sprite)
    
       Begin Object Class=ArrowComponent Name=Arrow
       End Object
       Components.Add(Arrow)
    }
  3. Compile and take a look in the editor. Our actor now has an arrow to indicate its current rotation.
    Time for action – Using rotators
  4. By default we know its current rotation is going to be (0,0,0), so let's rotate it in the editor. Click on the AwesomeActor to select it, and then press the Space bar once to change to the rotation tool.
    Time for action – Using rotators
  5. Now click and hold anywhere on the blue part of the rotation tool. We're going to change our actor's Yaw, so move it as close to 90 degrees as you can get. Don't worry if it's not perfect.
    Time for action – Using rotators
  6. Save our map, and then run it in the game. Exit and check the log for our rotation value.
    [0008.33] ScriptLog: Rotation: 0,16160,0

It's logging, but why is the value so high? That's definitely not 90. Internally, the ints that make up rotators are 32 bits and have a range of 0 to 65535. Therefore, in Unreal's unit system, 360 degrees = 65536. That would make 90 degrees = 16384, which is close to what was logged.

Rotators can be created the same as we did with vectors. You can make a var like this:

var rotator MyRot;

And then change the individual components in the PostBeginPlay function.

MyRot.Yaw = 2000;

You could also create a rotator as it's being used, like we did with vectors. The names of the functions we use for this are specific to rotators though:

SetRotation(Rotation + rot(0,0,4000));

What just happened?

Rotators, like vectors, are important structs to be examined on their own. Every actor in the game has a rotation, and the most noticeable use of rotators is in the player's camera, to tell the game where we're looking.

We've talked a lot about variables in this chapter. Next we're going to discuss ways we can change how they work.