class CP1PlayerController : ZilchComponent { [Property] var Speed : Real = 1.0; // How fast you move forward [Property] var RotationSpeed : Real = 1.0; // How fast you rotate function Initialize(init : CogInitializer) { //Connect to logic update Zero.Connect(this.Space, Events.LogicUpdate, this.OnLogicUpdate); } function OnLogicUpdate(updateEvent : UpdateEvent) { // The angle we are going to rotate by var rotationAngles = local Real3(0.0, 0.0, 0.0); // If the Left Arrow key is held down if (Zero.Keyboard.KeyIsDown(Keys.Left)) { rotationAngles.Z += this.RotationSpeed; } // If the Right Arrow key is held down if (Zero.Keyboard.KeyIsDown(Keys.Right)) { rotationAngles.Z -= this.RotationSpeed; } // Apply the calculated rotation first so movement is not base of the rotation from the previous frame this.Owner.Transform.RotateAnglesWorld(rotationAngles * updateEvent.Dt); // The direction we want to move in var movement = local Real3(0.0, 0.0, 0.0); // Get our facing direction from the Orientation component var forwardDirection = this.Owner.Orientation.WorldForward; // If the Up Arrow key is held down if (Zero.Keyboard.KeyIsDown(Keys.Up)) { // Set the movement direction to the direction the player is facing movement += forwardDirection; } // If the Down Arrow key is held down else if(Zero.Keyboard.KeyIsDown(Keys.Down)) { // Set the movement direction to the opposite of the direction the player is facing movement -= forwardDirection; } // Make sure the we only get the direction of the vector: movement = Math.Normalize(movement); // Apply the recorded movement using the new rotation this.Owner.Transform.Translation += movement * this.Speed * updateEvent.Dt; } }