public override void Update() { var move = new Vector3(); if (Input.IsKeyDown(Keys.A) || Input.IsKeyDown(Keys.Left)) { move = -Vector3.UnitX; } if (Input.IsKeyDown(Keys.D) || Input.IsKeyDown(Keys.Right)) { move = Vector3.UnitX; } if (Input.PointerEvents.Any()) { var last = Input.PointerEvents.Last(); if (last != null) { switch (last.State) { case PointerState.Down: if (last.Position.X < 0.5) { pointerVector = -Vector3.UnitX; } else { pointerVector = Vector3.UnitX; } break; case PointerState.Up: case PointerState.Out: case PointerState.Cancel: pointerVector = Vector3.Zero; break; } } } if (pointerVector != Vector3.Zero) { move = pointerVector; } move *= speed; character.Move(move); }
public override void Update() { var move = new Vector3(); if (Input.IsKeyDown(Keys.A) || Input.IsKeyDown(Keys.Left)) { move = -Vector3.UnitX; } if (Input.IsKeyDown(Keys.D) || Input.IsKeyDown(Keys.Right)) { move = Vector3.UnitX; } foreach (var evt in Input.PointerEvents) { switch (evt.EventType) { case PointerEventType.Pressed: if (evt.Position.X < 0.5) { pointerVector = -Vector3.UnitX; } else { pointerVector = Vector3.UnitX; } break; case PointerEventType.Released: case PointerEventType.Canceled: pointerVector = Vector3.Zero; break; } } if (pointerVector != Vector3.Zero) { move = pointerVector; } move *= speed; character.Move(move); }
public override void Update() { var playerState = PlayerState.Idle; var playerDirection = Vector3.Zero; // -- Keyboard Inputs // Space bar = jump if (Input.IsKeyDown(Keys.Space)) { playerState |= PlayerState.Jump; } // Left - right = run if (Input.IsKeyDown(Keys.Right)) { movingToTarget = false; playerState |= PlayerState.Run; playerDirection = Vector3.UnitX * speed; } else if (Input.IsKeyDown(Keys.Left)) { movingToTarget = false; playerState |= PlayerState.Run; playerDirection = -Vector3.UnitX * speed; } // -- Pointer (mouse/touch) foreach (var pointerEvent in Input.PointerEvents.Where(pointerEvent => pointerEvent.EventType == PointerEventType.Pressed)) { if (!movingToTarget) { var screenX = (pointerEvent.Position.X - 0.5f) * 2.0f; screenX *= 8.75f; autoPilotTarget = new Vector3(screenX, 0, 0); movingToTarget = true; } else { playerState |= PlayerState.Jump; } } // -- Logic // are we autopiloting? if (movingToTarget) { var direction = autoPilotTarget - Entity.Transform.Position; direction.Y = 0; //should we stop? var length = direction.Length(); if (length < speed) { movingToTarget = false; playerDirection = Vector3.Zero; playerState = PlayerState.Idle; } else { direction.Normalize(); playerDirection = (direction.X > 0 ? Vector3.UnitX : -Vector3.UnitX) * speed; playerState |= PlayerState.Run; } } // did we start jumping? if (playerState.HasFlag(PlayerState.Jump) && !oldState.HasFlag(PlayerState.Jump)) { playerController.Jump(); } // did we just land? if (oldState.HasFlag(PlayerState.Jump)) { if (!playerController.IsGrounded) { //force set jump flag if (!playerState.HasFlag(PlayerState.Jump)) { playerState |= PlayerState.Jump; // Mantain motion playerDirection = oldDirection; } } else if (playerController.IsGrounded) { //force clear jump flag if (playerState.HasFlag(PlayerState.Jump)) { playerState ^= PlayerState.Jump; } } } // did we start running? if (playerState.HasFlag(PlayerState.Run) && !oldState.HasFlag(PlayerState.Run)) { PlayRun(); } // did we stop running? else if (!playerState.HasFlag(PlayerState.Run) && oldState.HasFlag(PlayerState.Run)) { PlayIdle(); } // movement logic if (oldDirection != playerDirection) { playerController.Move(playerDirection); if (playerState.HasFlag(PlayerState.Run)) { if ((playerDirection.X > 0 && Entity.Transform.Scale.X < 0) || (playerDirection.X < 0 && Entity.Transform.Scale.X > 0)) { Entity.Transform.Scale.X *= -1.0f; } } } // Store current state for next frame oldState = playerState; oldDirection = playerDirection; }
public override void Update() { var rotationDelta = Input.MouseDelta; // Compute translation speed according to framerate and modifiers var translationSpeed = Speed * (float)Game.UpdateTime.Elapsed.TotalSeconds; // Take shortest path var deltaPitch = desiredPitch - pitch; var deltaYaw = (desiredYaw - yaw) % MathUtil.TwoPi; if (deltaYaw < 0) { deltaYaw += MathUtil.TwoPi; } if (deltaYaw > MathUtil.Pi) { deltaYaw -= MathUtil.TwoPi; } desiredYaw = yaw + deltaYaw; // Perform orientation transition var rotationAdaptation = (float)Game.UpdateTime.Elapsed.TotalSeconds * RotationAdaptationSpeed; yaw = Math.Abs(deltaYaw) < rotationAdaptation ? desiredYaw : yaw + rotationAdaptation * Math.Sign(deltaYaw); pitch = Math.Abs(deltaPitch) < rotationAdaptation ? desiredPitch : pitch + rotationAdaptation * Math.Sign(deltaPitch); desiredYaw = yaw -= 1.333f * rotationDelta.X * RotationSpeed; // we want to rotate faster Horizontally and Vertically desiredPitch = pitch = MathUtil.Clamp(pitch - rotationDelta.Y * RotationSpeed, -MathUtil.PiOverTwo, MathUtil.PiOverTwo); if (CameraEntity != null) { //we need to pitch only the camera node CameraEntity.Transform.Rotation = baseCameraRotation * Quaternion.RotationYawPitchRoll(0, pitch, 0); } Entity.Transform.Rotation = Quaternion.RotationYawPitchRoll(yaw, 0, 0); //do not apply pitch to our controller var move = new Vector3(); var forward = Vector3.Transform(ForwardVector, Entity.Transform.Rotation); var projectedForward = Vector3.Normalize(new Vector3(forward.X, 0, forward.Z)); var up = Vector3.TransformNormal(UpVector, Matrix.RotationQuaternion(Entity.Transform.Rotation)); var right = Vector3.Cross(forward, up); if (Input.IsKeyDown(Keys.A) || Input.IsKeyDown(Keys.Left)) { move += -right; } if (Input.IsKeyDown(Keys.D) || Input.IsKeyDown(Keys.Right)) { move += right; } if (Input.IsKeyDown(Keys.W) || Input.IsKeyDown(Keys.Up)) { move += projectedForward; } if (Input.IsKeyDown(Keys.S) || Input.IsKeyDown(Keys.Down)) { move += -projectedForward; } move.Normalize(); move *= translationSpeed; //please note that the default character controller ignores rotation, in the case of complex collisions you would have more kinematic elements within your model anyway. character.Move(move); if (Input.IsKeyPressed(Keys.Space)) { character.Jump(); } if (Input.IsKeyReleased(Keys.Escape)) { var game = (Game)Game; game.Exit(); } }