Hello, i’m new in stride, and learning. I create protozoan fps character controller, and i want to know, are there any gross mistakes?
Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Stride.Core.Mathematics;
using Stride.Input;
using Stride.Engine;
using Stride.Physics;
namespace MyGame2
{
public class MouseLook : SyncScript
{
public Entity PlayerCharacter { set; get; }
private int mouseSentitivity;
public int MouseSensitivity
{
get
{
return mouseSentitivity / 100;
}
set
{
mouseSentitivity = value * 100;
}
}
public int MoveSpeed { get; set; }
private float deltaTime;
private float mouseX, mouseY, xRotation;
private CharacterComponent character;
private Vector3 moveDirection = Vector3.Zero;
public override void Start()
{
GraphicsDevice.Presenter.PresentInterval = Stride.Graphics.PresentInterval.Immediate;
character = PlayerCharacter.Get<CharacterComponent>();
}
public override void Update()
{
moveDirection = Vector3.Zero;
DebugText.Print("FPS: " + Game.UpdateTime.FramePerSecond, new Int2(20, 20));
deltaTime = (float)Game.UpdateTime.Elapsed.TotalSeconds;
mouseX = -Input.MouseDelta.X * mouseSentitivity;
mouseY = -Input.MouseDelta.Y * mouseSentitivity;
xRotation += mouseY;
xRotation = MathUtil.Clamp(xRotation, -90f, 90f);
bool Right = Input.IsKeyDown(Keys.D);
bool Left = Input.IsKeyDown(Keys.A);
bool Forward = Input.IsKeyDown(Keys.W);
bool Back = Input.IsKeyDown(Keys.S);
bool Jump = Input.IsKeyDown(Keys.Space);
if (Input.IsMouseButtonPressed(MouseButton.Left)) Input.LockMousePosition();
if (Input.IsMouseButtonPressed(MouseButton.Right)) Input.UnlockMousePosition();
if (Right) moveDirection.X += 1;
if (Left) moveDirection.X -= 1;
if (Forward) moveDirection.Z -= 1;
if (Back) moveDirection.Z += 1;
if (character != null)
{
character.Orientation *= Quaternion.RotationY(MathUtil.DegreesToRadians(mouseX));
character.Orientation.Rotate(ref moveDirection);
character.SetVelocity(moveDirection * MoveSpeed);
Entity.Transform.Rotation = Quaternion.RotationX(MathUtil.DegreesToRadians(xRotation));
if (Jump && character.IsGrounded)
{
character.Jump();
}
}
}
}
}