restored from pc, added .editorconfig
This commit is contained in:
18
Assets/jelycho/Code/Player/PlayerActor.cs
Normal file → Executable file
18
Assets/jelycho/Code/Player/PlayerActor.cs
Normal file → Executable file
@@ -42,15 +42,10 @@ namespace RealityReboot.jelycho.Player {
|
||||
public void Look(Vector2 input) {
|
||||
m_Camera.Rotate(input.x, input.y);
|
||||
}
|
||||
|
||||
public void MoveRight(float input) {
|
||||
float3 direction = Quaternion.AngleAxis(m_Camera.Yaw, Vector3.up) * Vector3.right;
|
||||
m_Locomotion.AddMovementInput(direction, input);
|
||||
}
|
||||
|
||||
public void MoveForward(float input) {
|
||||
float3 direction = Quaternion.AngleAxis(m_Camera.Yaw, Vector3.up) * Vector3.forward;
|
||||
m_Locomotion.AddMovementInput(direction, input);
|
||||
|
||||
public void SetMoveInput(Vector2 input) {
|
||||
float3 direction = Quaternion.AngleAxis(m_Camera.Yaw, Vector3.up) * new float3(input.x, 0.0f, input.y);
|
||||
m_Locomotion.SetWishDirection(direction);
|
||||
}
|
||||
|
||||
public void StartDrag() {
|
||||
@@ -71,11 +66,12 @@ namespace RealityReboot.jelycho.Player {
|
||||
}
|
||||
|
||||
void UpdateAnimator() {
|
||||
float forwardNormalized = m_Locomotion.LocalVelocity.z / m_Locomotion.maxMovementSpeed;
|
||||
float forwardNormalized = m_Locomotion.LocalVelocity.z / m_Locomotion.runSpeed;
|
||||
|
||||
m_Animator.SetFloat(AnimatorParamHashes.s_VelocityForwardNormalized, forwardNormalized);
|
||||
m_Animator.SetFloat(AnimatorParamHashes.s_VelocityRightNormalized, 0.0f);
|
||||
m_Animator.SetBool(AnimatorParamHashes.s_IsGrounded, true);
|
||||
|
||||
m_Animator.SetBool(AnimatorParamHashes.s_IsGrounded, m_Locomotion.IsGrounded);
|
||||
}
|
||||
}
|
||||
}
|
||||
0
Assets/jelycho/Code/Player/PlayerActor.cs.meta
Normal file → Executable file
0
Assets/jelycho/Code/Player/PlayerActor.cs.meta
Normal file → Executable file
13
Assets/jelycho/Code/Player/PlayerController.cs
Normal file → Executable file
13
Assets/jelycho/Code/Player/PlayerController.cs
Normal file → Executable file
@@ -17,8 +17,6 @@ namespace RealityReboot.jelycho.Player {
|
||||
readonly Config m_Config;
|
||||
PlayerActor m_FPPActor;
|
||||
|
||||
[Inject] InputService m_InputService;
|
||||
|
||||
public PlayerController(Config config) {
|
||||
m_Config = config;
|
||||
}
|
||||
@@ -27,8 +25,8 @@ namespace RealityReboot.jelycho.Player {
|
||||
}
|
||||
|
||||
public async UniTask OnStart(CancellationToken cancellationToken) {
|
||||
m_InputService.LockCursor();
|
||||
m_InputService.EnableControls();
|
||||
RR.Input.LockCursor();
|
||||
RR.Input.EnableControls();
|
||||
|
||||
m_FPPActor = await RR.World.SpawnActor<PlayerActor>(m_Config.playerActorPrefab, cancellationToken);
|
||||
m_FPPActor.Simulate = true;
|
||||
@@ -37,8 +35,8 @@ namespace RealityReboot.jelycho.Player {
|
||||
}
|
||||
|
||||
public void OnStop() {
|
||||
m_InputService.DisableControls();
|
||||
m_InputService.UnlockCursor();
|
||||
RR.Input.DisableControls();
|
||||
RR.Input.UnlockCursor();
|
||||
Object.Destroy(m_FPPActor);
|
||||
|
||||
RR.World.KillActor(m_FPPActor);
|
||||
@@ -57,8 +55,7 @@ namespace RealityReboot.jelycho.Player {
|
||||
m_FPPActor.Look(lookInput * FPPConfig.s_MouseSensitivity.FloatValue);
|
||||
|
||||
Vector2 moveInput = m_Config.moveActionReference.action.ReadValue<Vector2>();
|
||||
m_FPPActor.MoveRight(moveInput.x);
|
||||
m_FPPActor.MoveForward(moveInput.y);
|
||||
m_FPPActor.SetMoveInput(moveInput);
|
||||
|
||||
if (m_Config.jumpActionReference.action.WasPerformedThisFrame()) {
|
||||
m_FPPActor.Jump();
|
||||
|
||||
0
Assets/jelycho/Code/Player/PlayerController.cs.meta
Normal file → Executable file
0
Assets/jelycho/Code/Player/PlayerController.cs.meta
Normal file → Executable file
4
Assets/jelycho/Code/Player/PlayerControllerAsset.cs
Normal file → Executable file
4
Assets/jelycho/Code/Player/PlayerControllerAsset.cs
Normal file → Executable file
@@ -6,8 +6,8 @@ namespace RealityReboot.jelycho.Player {
|
||||
public class PlayerControllerAsset : ControllerAsset {
|
||||
[SerializeField] PlayerController.Config m_Config;
|
||||
|
||||
public override IController Create(DIContext context) {
|
||||
PlayerController controller = context.Create<PlayerController>(m_Config);
|
||||
public override IController Create() {
|
||||
PlayerController controller = new(m_Config);
|
||||
return controller;
|
||||
}
|
||||
}
|
||||
|
||||
0
Assets/jelycho/Code/Player/PlayerControllerAsset.cs.meta
Normal file → Executable file
0
Assets/jelycho/Code/Player/PlayerControllerAsset.cs.meta
Normal file → Executable file
230
Assets/jelycho/Code/Player/PlayerFPPLocomotion.cs
Normal file → Executable file
230
Assets/jelycho/Code/Player/PlayerFPPLocomotion.cs
Normal file → Executable file
@@ -1,68 +1,164 @@
|
||||
using System;
|
||||
using RebootKit.Engine.Extensions;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RealityReboot.jelycho.Player {
|
||||
[RequireComponent(typeof(Rigidbody), typeof(CapsuleCollider))]
|
||||
public class PlayerFPPLocomotion : MonoBehaviour {
|
||||
[SerializeField] Rigidbody m_Rigidbody;
|
||||
|
||||
public float acceleration = 10.0f;
|
||||
public float accelerationBackwards = 2.0f;
|
||||
public float accelerationStrafe = 2.0f;
|
||||
public float maxMovementSpeed = 8.0f;
|
||||
public float maxMovementSpeedBackwards = 2.0f;
|
||||
public float maxStrafeSpeed = 2.0f;
|
||||
|
||||
public float3 Velocity => m_Rigidbody.linearVelocity;
|
||||
public float3 LocalVelocity => m_Rigidbody.transform.InverseTransformDirection(m_Rigidbody.linearVelocity);
|
||||
|
||||
float3 m_PendingInputValue;
|
||||
bool m_IsJumpRequested;
|
||||
|
||||
void Awake() {
|
||||
}
|
||||
|
||||
void FixedUpdate() {
|
||||
if (m_PendingInputValue.IsZero()) {
|
||||
return;
|
||||
}
|
||||
|
||||
float3 localVelocity = LocalVelocity;
|
||||
|
||||
m_PendingInputValue = math.normalize(m_PendingInputValue);
|
||||
float3 localInput = m_Rigidbody.transform.InverseTransformDirection(m_PendingInputValue);
|
||||
|
||||
float3 velocityToAdd = float3.zero;
|
||||
if (localInput.z > 0.0f) {
|
||||
if (localVelocity.z < maxMovementSpeed) {
|
||||
velocityToAdd.z += localInput.z * acceleration * Time.fixedDeltaTime;
|
||||
}
|
||||
} else {
|
||||
velocityToAdd.z += localInput.z * accelerationBackwards * Time.fixedDeltaTime;
|
||||
}
|
||||
|
||||
velocityToAdd.x += localInput.x * accelerationStrafe * Time.fixedDeltaTime;
|
||||
|
||||
float3 velocityToAddWorldSpace = m_Rigidbody.transform.TransformDirection(velocityToAdd);
|
||||
m_Rigidbody.AddForce(velocityToAddWorldSpace, ForceMode.VelocityChange);
|
||||
|
||||
m_PendingInputValue = float3.zero;
|
||||
}
|
||||
|
||||
void OnValidate() {
|
||||
if (m_Rigidbody == null) {
|
||||
m_Rigidbody = GetComponent<Rigidbody>();
|
||||
}
|
||||
}
|
||||
|
||||
public void AddMovementInput(float3 input, float scale) {
|
||||
m_PendingInputValue += input * scale;
|
||||
}
|
||||
|
||||
public void Jump() {
|
||||
m_IsJumpRequested = true;
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using RebootKit.Engine.Extensions;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RealityReboot.jelycho.Player {
|
||||
[RequireComponent(typeof(Rigidbody), typeof(CapsuleCollider))]
|
||||
public class PlayerFPPLocomotion : MonoBehaviour {
|
||||
static readonly RaycastHit[] s_HitBuffer = new RaycastHit[32];
|
||||
|
||||
[SerializeField] Rigidbody m_Rigidbody;
|
||||
[SerializeField] CapsuleCollider m_CapsuleCollider;
|
||||
[SerializeField] LayerMask m_GroundLayerMask;
|
||||
|
||||
public float runSpeed = 10.0f;
|
||||
public float sprintSpeed = 20.0f;
|
||||
|
||||
public float groundAcceleration = 10.0f;
|
||||
public float groundFriction = 10.0f;
|
||||
|
||||
public float jumpHeight = 2.0f;
|
||||
|
||||
public float inAirAcceleration = 10.0f;
|
||||
public float gravity = 9.8f;
|
||||
|
||||
public float3 Velocity => m_Rigidbody.linearVelocity;
|
||||
public float3 LocalVelocity => m_Rigidbody.transform.InverseTransformDirection(m_Rigidbody.linearVelocity);
|
||||
|
||||
float3 m_WishDir;
|
||||
bool m_IsSprinting;
|
||||
|
||||
public bool IsGrounded { get; private set; }
|
||||
|
||||
void Awake() {
|
||||
}
|
||||
|
||||
void FixedUpdate() {
|
||||
IsGrounded = CheckGrounded();
|
||||
|
||||
float3 vel = m_Rigidbody.linearVelocity;
|
||||
|
||||
if (IsGrounded) {
|
||||
UpdateGroundVelocity(ref vel, Time.fixedDeltaTime);
|
||||
} else {
|
||||
UpdateAirVelocity(ref vel, Time.fixedDeltaTime);
|
||||
}
|
||||
|
||||
m_Rigidbody.linearVelocity = vel;
|
||||
}
|
||||
|
||||
void ApplyGroundFriction(ref float3 vel, float dt) {
|
||||
float speed = math.length(vel);
|
||||
if (speed < 0.1f) {
|
||||
vel = float3.zero;
|
||||
return;
|
||||
}
|
||||
|
||||
float drop = speed * groundFriction * dt;
|
||||
float newSpeed = speed - drop;
|
||||
if (newSpeed < 0.0f) {
|
||||
newSpeed = 0.0f;
|
||||
}
|
||||
|
||||
newSpeed /= speed;
|
||||
vel *= newSpeed;
|
||||
}
|
||||
|
||||
void ApplyGravity(ref float3 vel, float dt) {
|
||||
vel.y -= gravity * dt;
|
||||
}
|
||||
|
||||
void Accelerate(ref float3 vel, float3 wishDir, float wishSpeed, float acceleration, float dt) {
|
||||
float currentSpeed = math.dot(vel, wishDir);
|
||||
float addSpeed = wishSpeed - currentSpeed;
|
||||
if (addSpeed <= 0.0f) {
|
||||
return;
|
||||
}
|
||||
|
||||
float accelSpeed = acceleration * dt * wishSpeed;
|
||||
if (accelSpeed > addSpeed) {
|
||||
accelSpeed = addSpeed;
|
||||
}
|
||||
|
||||
vel += accelSpeed * wishDir;
|
||||
}
|
||||
|
||||
void UpdateGroundVelocity(ref float3 vel, float dt) {
|
||||
ApplyGroundFriction(ref vel, dt);
|
||||
|
||||
float wishSpeed;
|
||||
if (m_IsSprinting) {
|
||||
wishSpeed = sprintSpeed;
|
||||
} else {
|
||||
wishSpeed = runSpeed;
|
||||
}
|
||||
|
||||
Accelerate(ref vel, m_WishDir, wishSpeed, groundAcceleration, dt);
|
||||
ApplyGravity(ref vel, dt);
|
||||
}
|
||||
|
||||
void UpdateAirVelocity(ref float3 vel, float dt) {
|
||||
ApplyGravity(ref vel, dt);
|
||||
|
||||
Accelerate(ref vel, m_WishDir, runSpeed, inAirAcceleration, dt);
|
||||
}
|
||||
|
||||
void OnValidate() {
|
||||
if (m_Rigidbody == null) {
|
||||
m_Rigidbody = GetComponent<Rigidbody>();
|
||||
}
|
||||
}
|
||||
|
||||
void OnDrawGizmosSelected() {
|
||||
float3 origin = transform.position;
|
||||
|
||||
Gizmos.color = Color.blue;
|
||||
Gizmos.DrawLine(origin, origin + m_WishDir);
|
||||
|
||||
Gizmos.color = Color.green;
|
||||
Gizmos.DrawLine(origin, origin + Velocity);
|
||||
}
|
||||
|
||||
bool CheckGrounded() {
|
||||
float3 origin = transform.position + Vector3.up * 0.1f;
|
||||
float3 direction = Vector3.down;
|
||||
float distance = 0.2f;
|
||||
|
||||
int hitCount = Physics.SphereCastNonAlloc(origin,
|
||||
m_CapsuleCollider.radius,
|
||||
direction,
|
||||
s_HitBuffer,
|
||||
distance,
|
||||
m_GroundLayerMask,
|
||||
QueryTriggerInteraction.Ignore);
|
||||
|
||||
for (int i = 0; i < hitCount; i++) {
|
||||
RaycastHit hit = s_HitBuffer[i];
|
||||
if (hit.collider is not null && hit.collider.gameObject != gameObject) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetWishDirection(float3 dir) {
|
||||
m_WishDir = dir;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Jump() {
|
||||
if (IsGrounded) {
|
||||
m_Rigidbody.AddForce(Vector3.up * CalculateJumpVelocity(jumpHeight, gravity), ForceMode.VelocityChange);
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
static float CalculateJumpVelocity(float jumpHeight, float gravity) {
|
||||
return math.sqrt(2.0f * gravity * jumpHeight);
|
||||
}
|
||||
}
|
||||
}
|
||||
0
Assets/jelycho/Code/Player/PlayerFPPLocomotion.cs.meta
Normal file → Executable file
0
Assets/jelycho/Code/Player/PlayerFPPLocomotion.cs.meta
Normal file → Executable file
Reference in New Issue
Block a user