diff --git a/Assets/prefabs/player.prefab b/Assets/prefabs/player.prefab index dc02c43..8f65d15 100644 --- a/Assets/prefabs/player.prefab +++ b/Assets/prefabs/player.prefab @@ -44,6 +44,10 @@ { "__type": "Kal", "__guid": "8467b849-9449-492b-b862-76c3c37aee1e", + "Body": { + "_type": "gameobject", + "go": "b99ceffc-a173-4a6e-a239-6d3c1612c09f" + }, "CamOffsetX": 200, "CamOffsetZ": 61, "DuckSpeed": 50, @@ -128,7 +132,6 @@ "Flags": 0, "Name": "spine_2", "Position": "5.610497,0.00001525879,-0.000007629395", - "Rotation": "-0.0003193617,0.001911119,-0.0103026,-0.9999417", "Enabled": true, "Children": [ { @@ -143,8 +146,8 @@ "__guid": "a660f880-88bb-4972-bb01-5a6a2e2d2a84", "Flags": 0, "Name": "head", - "Position": "4.511508,-0.0006238669,-0.0008400269", - "Rotation": "-0.000007912517,-0.0000001043081,0.01384526,-0.9998921", + "Position": "4.511506,-0.0004174262,-0.001046275", + "Rotation": "0.4999392,-0.006922714,0.01199041,-0.8659359", "Enabled": true, "Children": [ { diff --git a/Assets/scenes/minimal.scene b/Assets/scenes/minimal.scene index 88e9f84..e27d0ae 100644 --- a/Assets/scenes/minimal.scene +++ b/Assets/scenes/minimal.scene @@ -17,13 +17,13 @@ }, { "__type": "Sandbox.KPTLConnect", - "__guid": "895b0f93-287e-4607-808e-6ce5bdbb5c05", + "__guid": "79db0a43-3cee-44bf-86db-fa70ca72f62c", "PlayerPrefab": { "_type": "gameobject", "prefab": "prefabs/player.prefab" }, "SpawnPoints": [], - "StartServer": true + "StartServer": false } ] }, diff --git a/Code/Kal.cs b/Code/Kal.cs index 415b255..3e43bf4 100644 --- a/Code/Kal.cs +++ b/Code/Kal.cs @@ -4,41 +4,23 @@ using ShrimpleCharacterController; public sealed class Kal : Component { - [RequireComponent] - public ShrimpleCharacterController.ShrimpleCharacterController Controller { get; set; } - - [RequireComponent] - public CitizenAnimationHelper AnimationHelper { get; set; } + [RequireComponent] public ShrimpleCharacterController.ShrimpleCharacterController Controller { get; set; } + [RequireComponent] public CitizenAnimationHelper AnimationHelper { get; set; } public SkinnedModelRenderer Renderer { get; set; } + [Property] public GameObject Body { get; set; } public GameObject Camera { get; set; } - [Property] - [Range(1f, 200f, 1f)] - public float CamOffsetX { get; set; } - - [Property] - [Range(1f, 200f, 1f)] - public float CamOffsetZ { get; set; } - - [Property] - [Range(50f, 200f, 10f)] - public float WalkSpeed { get; set; } = 100f; + [Property] [Range(1f, 200f, 1f)] public float CamOffsetX { get; set; } + [Property] [Range(1f, 200f, 1f)] public float CamOffsetZ { get; set; } + [Property] [Range(50f, 200f, 10f)] public float WalkSpeed { get; set; } = 100f; + [Property] [Range(100f, 500f, 20f)] public float RunSpeed { get; set; } = 300f; + [Property] [Range(25f, 100f, 5f)] public float DuckSpeed { get; set; } = 50f; + [Property] [Range(200f, 500f, 20f)] public float JumpStrength { get; set; } = 350f; + [Sync] public Angles EyeAngles { get; set; } + [Sync] public bool isDucking { get; set; } = false; + [Sync] public bool isRunning { get; set; } = false; - [Property] - [Range(100f, 500f, 20f)] - public float RunSpeed { get; set; } = 300f; - - [Property] - [Range(25f, 100f, 5f)] - public float DuckSpeed { get; set; } = 50f; - - [Property] - [Range(200f, 500f, 20f)] - public float JumpStrength { get; set; } = 350f; - - public Angles EyeAngles { get; set; } - - protected override void OnStart() + protected override void OnStart() { base.OnStart(); @@ -49,52 +31,75 @@ public sealed class Kal : Component Camera.SetParent(GameObject); var cameraComponent = Camera.Components.Create(); cameraComponent.ZFar = 32768f; - - // Sandbox.Game. } protected override void OnFixedUpdate() { base.OnFixedUpdate(); - if ( !Network.IsOwner ) return; - - var wishDirection = Input.AnalogMove.Normal * Rotation.FromYaw(EyeAngles.yaw); - var isDucking = Input.Down("Duck"); - var isRunning = Input.Down("Run"); - var wishSpeed = isDucking ? DuckSpeed : - isRunning ? RunSpeed : WalkSpeed; - - Controller.WishVelocity = wishDirection * wishSpeed; - - Controller.Move(); - - if (Input.Pressed("Jump") && Controller.IsOnGround) + if ( Network.IsOwner ) { - Controller.Punch(Vector3.Up * JumpStrength); - AnimationHelper?.TriggerJump(); + var wishDirection = Input.AnalogMove.Normal * Rotation.FromYaw( EyeAngles.yaw ); + isDucking = Input.Down( "Duck" ); + isRunning = Input.Down( "Run" ); + var wishSpeed = isDucking ? DuckSpeed : + isRunning ? RunSpeed : WalkSpeed; + + Controller.WishVelocity = wishDirection * wishSpeed; + + Controller.Move(); + + if ( Input.Pressed( "Jump" ) && Controller.IsOnGround ) + { + Controller.Punch( Vector3.Up * JumpStrength * 2f ); + AnimationHelper?.TriggerJump(); + } + + if ( !AnimationHelper.IsValid() ) return; + } + + UpdateAnimations(); + + } - if (!AnimationHelper.IsValid()) return; - - AnimationHelper.WithWishVelocity(Controller.WishVelocity); - AnimationHelper.WithVelocity(Controller.Velocity); - AnimationHelper.DuckLevel = isDucking ? 1f : 0f; - AnimationHelper.IsGrounded = Controller.IsOnGround; + void UpdateAnimations() + { + AnimationHelper.WithWishVelocity(Controller.WishVelocity); + AnimationHelper.WithVelocity(Controller.Velocity); + AnimationHelper.IsGrounded = Controller.IsOnGround; + AnimationHelper.DuckLevel = isDucking ? 1f : 0f; } protected override void OnUpdate() { base.OnUpdate(); + + if ( Network.IsOwner ) + { + EyeAngles += Input.AnalogLook; + EyeAngles = EyeAngles.WithPitch(MathX.Clamp(EyeAngles.pitch, -10f, 40f)); + // Renderer.WorldRotation = Rotation.Slerp(Renderer.WorldRotation, Rotation.FromYaw(EyeAngles.yaw), Time.Delta * 5f); + var cameraOffset = Vector3.Up * CamOffsetZ + Vector3.Backward * CamOffsetX; + + Camera.WorldRotation = EyeAngles.ToRotation(); + Camera.LocalPosition = cameraOffset * Camera.WorldRotation; + } - if ( !Network.IsOwner ) return; + RotateBody(); + } + + void RotateBody() + { + if(Body is null) return; - EyeAngles += Input.AnalogLook; - EyeAngles = EyeAngles.WithPitch(MathX.Clamp(EyeAngles.pitch, -10f, 40f)); - Renderer.WorldRotation = Rotation.Slerp(Renderer.WorldRotation, Rotation.FromYaw(EyeAngles.yaw), Time.Delta * 5f); + var targetAngle = new Angles(0, EyeAngles.yaw, 0).ToRotation(); + + float rotateDifference = Body.WorldRotation.Distance(targetAngle); - var cameraOffset = Vector3.Up * CamOffsetZ + Vector3.Backward * CamOffsetX; - Camera.WorldRotation = EyeAngles.ToRotation(); - Camera.LocalPosition = cameraOffset * Camera.WorldRotation; + if(rotateDifference > 30f || Controller.Velocity.Length > 10f) + { + Body.WorldRotation = Rotation.Lerp(Body.WorldRotation, targetAngle, Time.Delta * 2f); + } } }