private void Move() { // Character speed moveDirectionEvent.TryReceive(out Vector3 moveDirection); if (character.IsGrounded) { character.SetVelocity(moveDirection * MaxRunSpeed); aerialVelocity = moveDirection * MaxRunSpeed; } else { character.SetVelocity( aerialVelocity ); // aerialVelocity *= 0.7f; } if (Input.IsKeyPressed(Keys.Space)) { var rb = this.Entity.Get <CharacterComponent>(); if (rb.IsGrounded) { rb.Jump(Vector3.UnitY * 10.0f); } } // Broadcast normalized speed RunSpeedEventKey.Broadcast(moveDirection.Length()); }
void HaltMovement() { IsRunning = false; moveDirection = Vector3.Zero; Character.SetVelocity(Vector3.Zero); MoveDestination = Entity.Transform.WorldMatrix.TranslationVector; }
public override void Update() { if (!Input.IsKeyDown(Keys.W) && !Input.IsKeyDown(Keys.A) && !Input.IsKeyDown(Keys.S) && !Input.IsKeyDown(Keys.D)) { character.SetVelocity(Vector3.Zero); } var velocity = Vector3.Zero; if (Input.IsKeyDown(Keys.W)) { velocity += -Vector3.UnitZ * Speed; } if (Input.IsKeyDown(Keys.A)) { velocity += -Vector3.UnitX * Speed; } if (Input.IsKeyDown(Keys.S)) { velocity += Vector3.UnitZ * Speed; } if (Input.IsKeyDown(Keys.D)) { velocity += Vector3.UnitX * Speed; } character.SetVelocity(velocity); }
private void HaltMovement() { isRunning = false; moveDirection = Vector3.Zero; character.SetVelocity(Vector3.Zero); moveDestination = modelChildEntity.Transform.WorldMatrix.TranslationVector; }
private void PlayerInput() { velocity = Vector3.Zero; speedDelta = Speed * (float)Game.UpdateTime.Elapsed.TotalSeconds; if (Input.Keyboard.IsKeyDown(Keys.W)) { velocity -= Vector3.UnitZ * speedDelta; } if (Input.Keyboard.IsKeyDown(Keys.S)) { velocity += Vector3.UnitZ * speedDelta; } if (Input.Keyboard.IsKeyDown(Keys.A)) { velocity -= Vector3.UnitX * speedDelta; } if (Input.Keyboard.IsKeyDown(Keys.D)) { velocity += Vector3.UnitX * speedDelta; } characterComponent.SetVelocity(velocity); }
public override void Update() { var velocity = new Vector3(); if (Input.IsKeyDown(Keys.W)) { velocity.Z++; } if (Input.IsKeyDown(Keys.S)) { velocity.Z--; } if (Input.IsKeyDown(Keys.A)) { velocity.X++; } if (Input.IsKeyDown(Keys.D)) { velocity.X--; } velocity.Normalize(); velocity *= MovementMultiplier; velocity = Vector3.Transform(velocity, Entity.Transform.Rotation); character.SetVelocity(velocity); }
private void MoveForwardBackward(float dt) { if (characterComponent.Entity.Transform.Position.Y < -10) { //characterComponent.Orientation HadMove = true; characterComponent.Teleport(spawnPoint); } else { var translationForUpdate = Vector3.Zero; var isRunning = Input.IsKeyDown(Keys.LeftShift) || Input.IsKeyDown(Keys.RightShift); var speedToUse = isRunning ? RunSpeed : WalkSpeed; if (ShouldMoveForwards()) { translationForUpdate.Z = speedToUse.Z; HadMove = true; } if (ShouldMoveBackwards()) { translationForUpdate.Z = -speedToUse.Z; HadMove = true; } translationForUpdate *= dt; characterComponent.SetVelocity(translationForUpdate.GetRotationVector(Entity.Transform)); MovementSpeedChangedEvent.Broadcast( translationForUpdate.Length() <= 0 ? MovementState.Idle : isRunning ? MovementState.Running : MovementState.Walking); } }
private void Move() { // Character speed Vector3 moveDirection = Vector3.Zero; moveDirectionEvent.TryReceive(out moveDirection); character.SetVelocity(moveDirection * MaxRunSpeed); // Broadcast normalized speed RunSpeedEventKey.Broadcast(moveDirection.Length()); }
private void Move(float speed) { // Character speed moveDirectionEvent.TryReceive(out Vector3 newMoveDirection); // Allow very simple inertia to the character to make animation transitions more fluid moveDirection = (moveDirection * 0.85f) + (newMoveDirection * 0.15f); character.SetVelocity(moveDirection * speed); // Broadcast speed as per cent of the max speed RunSpeedEventKey.Broadcast(moveDirection.Length()); // Character orientation if (moveDirection.Length() > 0.001) { yawOrientation = MathUtil.RadiansToDegrees((float)Math.Atan2(-moveDirection.Z, moveDirection.X) + MathUtil.PiOverTwo); } modelChildEntity.Transform.Rotation = Quaternion.RotationYawPitchRoll(MathUtil.DegreesToRadians(yawOrientation), 0, 0); }
private void Move(float speed) { // Use the delta time from physics character.SetVelocity(MoveDirection * speed); }
public override void Update() { var rotationDelta = Input.MouseDelta; rotationDelta.Y = -rotationDelta.Y; // 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) { Vector2 pitchRoll = edgeTilter?.CalculatePitchRoll() ?? Vector2.Zero; pitchRoll = Vector2.Transform(pitchRoll, Quaternion.RotationZ(yaw)); _smoothPitchRoll = Vector2.SmoothStep(_smoothPitchRoll, pitchRoll, EdgeTiltSmoothAmount); //we need to pitch only the camera node CameraEntity.Transform.Rotation = baseCameraRotation * Quaternion.RotationYawPitchRoll(0, pitch - _smoothPitchRoll.Y, _smoothPitchRoll.X); } 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; //animation.("Gun Walk"); } 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.SetVelocity(move); //character.Orientation //Well, animations.......me lazy if (Input.IsKeyPressed(Keys.Space) && character.IsGrounded) { character.Jump(); } if (Input.IsKeyReleased(Keys.Escape)) { var game = (Game)Game; game.Exit(); } }
public override async Task Execute() { sprite = Entity.Get <SpriteComponent>().SpriteProvider as SpriteFromSheet; CharPlayer = Entity.Get <CharacterComponent>() as CharacterComponent; while (Game.IsRunning) { await Script.NextFrame(); if (Input.KeyDown.Count < 1 && sprite.CurrentFrame != 1) { sprite.CurrentFrame = 1; } else if (Input.IsKeyDown(Keys.A)) { //ToDo- Find a way to read Velocity! Entity.Transform.Scale.X = -1; float Increment = -0.01f; while (Input.IsKeyDown(Keys.A)) { Walk(); await Script.NextFrame(); if (Increment > -1.9f) { CharPlayer.SetVelocity(new Vector3(Increment, 0, 0)); if (Increment > -0.9f) { Increment -= 0.005f; await Script.NextFrame(); } await Script.NextFrame(); Increment -= 0.015f; } if (Increment > -2.3f) { Increment -= 0.1f; } } CharPlayer.SetVelocity(new Vector3(0, 0, 0)); } else if (Input.IsKeyDown(Keys.D)) { Entity.Transform.Scale.X = 1; float Inc = 0.01f; while (Input.IsKeyDown(Keys.D)) { Walk(); await Script.NextFrame(); if (Inc < 1.9f) { CharPlayer.SetVelocity(new Vector3(Inc, 0, 0)); if (Inc < 0.9f) { Inc += 0.005f; await Script.NextFrame(); } await Script.NextFrame(); Inc += 0.015f; } if (Inc < 2.3f) { Inc += 0.1f; } } CharPlayer.SetVelocity(new Vector3(0, 0, 0)); } } }
private void UpdateMoveTowardsDestination(float speed) { if (!ReachedDestination) { var direction = CurrentWaypoint - Entity.Transform.WorldMatrix.TranslationVector; // Get distance towards next point and normalize the direction at the same time var length = direction.Length(); direction /= length; // Check when to advance to the next waypoint bool advance = false; // Check to see if an intermediate point was passed by projecting the position along the path if (_pathToDestination.Count > 0 && _waypointIndex > 0 && _waypointIndex != _pathToDestination.Count - 1) { Vector3 pointNormal = CurrentWaypoint - _pathToDestination[_waypointIndex - 1].Entity.Transform.Position; pointNormal.Normalize(); float current = Vector3.Dot(Entity.Transform.WorldMatrix.TranslationVector, pointNormal); float target = Vector3.Dot(CurrentWaypoint, pointNormal); if (current > target) { advance = true; } } else { if (length < DestinationThreshold) // Check distance to final point { advance = true; } } // Advance waypoint? if (advance) { _waypointIndex++; if (ReachedDestination) { // Final waypoint reached HaltMovement(); return; } } // Calculate speed based on distance from final destination float moveSpeed = (_moveDestination - Entity.Transform.WorldMatrix.TranslationVector).Length() * DestinationSlowdown; if (moveSpeed > 1.0f) { moveSpeed = 1.0f; } // Slow down around corners float cornerSpeedMultiply = Math.Max(0.0f, Vector3.Dot(direction, _moveDirection)) * CornerSlowdown + (1.0f - CornerSlowdown); // Allow a very simple inertia to the character to make animation transitions more fluid _moveDirection = _moveDirection * 0.85f + direction * moveSpeed * cornerSpeedMultiply * 0.15f; _character.SetVelocity(_moveDirection * speed); // Broadcast speed as per cent of the max speed RunSpeedEventKey.Broadcast(_moveDirection.Length()); // Character orientation if (_moveDirection.Length() > 0.001) { _yawOrientation = MathUtil.RadiansToDegrees((float)Math.Atan2(-_moveDirection.Z, _moveDirection.X) + MathUtil.PiOverTwo); } _modelEntity.Transform.Rotation = Quaternion.RotationYawPitchRoll(MathUtil.DegreesToRadians(_yawOrientation), 0, 0); } else { // No target HaltMovement(); } }
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)) { playerState |= PlayerState.Run; playerDirection = Vector3.UnitX * speed; } else if (Input.IsKeyDown(Keys.Left)) { playerState |= PlayerState.Run; playerDirection = -Vector3.UnitX * speed; } // -- Logic // 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.SetVelocity(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 void Enable() { _characterComponent.SetVelocity(Vector3.UnitX * Speed); }
private void UpdateMovement() { MoveDirection = Entity.Transform.WorldMatrix.Forward.XY(); var Movement = new Vector3(0, 0, 0); float deltaTime = (float)Game.UpdateTime.Elapsed.TotalSeconds; MouseDelta = Input.Mouse.Delta; var rotation = Rotation(MouseDelta.X, CurrentRotation); //Rotate player entity //Entity.Transform.Rotation = rotation; CurrentRotation = Entity.Transform.Rotation; //Sets forward velocity in rotation matrix Matrix tempMatrix; Matrix.RotationQuaternion(ref rotation, out tempMatrix); if (Input.IsKeyDown(Keys.S)) { Movement = -tempMatrix.Backward; MoveDirection += -Vector2.UnitY; } if (Input.IsKeyDown(Keys.W)) { Movement = -tempMatrix.Forward; MoveDirection += Vector2.UnitY; } if (Input.IsKeyDown(Keys.A)) { Movement = -tempMatrix.Left; MoveDirection += -Vector2.UnitX; } if (Input.IsKeyDown(Keys.D)) { Movement = -tempMatrix.Right; MoveDirection += Vector2.UnitX; } var xx = new Vector3(MoveDirection.X, 0, MoveDirection.Y); var l = MoveDirection.Length(); xx *= l; RB.SetVelocity(xx /** deltaTime*/); var yawOrientation = MathUtil.RadiansToDegrees((float)Math.Atan2(-xx.Z, xx.X) + MathUtil.PiOverTwo); Entity.Transform.Rotation = Quaternion.RotationYawPitchRoll(MathUtil.DegreesToRadians(yawOrientation), 0, 0); if (!Movement.Equals(Vector3.Zero)) { if (!animComponent.IsPlaying("walk")) { animComponent.Crossfade("walk", TimeSpan.FromSeconds(0.5)); } } else { if (!animComponent.IsPlaying("idle")) { animComponent.Crossfade("idle", TimeSpan.FromSeconds(0.5)); } } // We store the local and world position of our entity's tranform in a Vector3 variable Vector3 localPosition = Camera.Entity.Transform.Position; Vector3 worldPosition = Entity.Transform.WorldMatrix.Forward; // We disaply the entity's name and its local and world position on screen DebugText.Print(Camera.Entity.Name + " - local position: " + localPosition, new Int2(400, 450)); DebugText.Print(Entity.Name + " - world position: " + worldPosition, new Int2(400, 470)); DebugText.Print($"{Movement.Z}: Z - {Movement.X} : X {rotation} : Rotation", new Int2(20, 45)); }