Sync Tutorial - Further Programming Player Movement
We are going to add even more code to your ShipMovement.cs file.
Go back to ShipMovement.cs and add this code:

function ShipMovementBehavior::onUpdate(%
this)
{
%
this.findTargetAngle();
%
this.updateMovement();
//this code is updated every frame.
//This callback can slow down your game so use only when absolutly necessary.
}

function ShipMovementBehavior::findTargetAngle(%
this)
{
if(%this.left + %this.right + %this.up + %this.down) // checks for input
{
if(%this.left)
{
%TargetAngle = 270;
}
if(%this.right)
{
%TargetAngle = 90;
}
if(%this.down)
{
%TargetAngle = 180;
}
if(%this.up)
{
%TargetAngle = 0;
}
if(%this.left && %this.up)
{
%TargetAngle = 315;
}
if(%this.left && %this.down)
{
%TargetAngle = 225;
}
if(%this.right && %this.up)
{
%TargetAngle = 45;
}
if(%this.right && %this.down)
{
%TargetAngle = 135;
}
%this.TargetAngle = %TargetAngle;
}
//This code sets a target angle to what buttons are pressed on the keyboard
}
This code determines what buttons the player is pressing and creates the variable %TargetAngle which we are going to use to determine the rotation of the player’s ship.
Next Page
Previous Page