2020-08-02 02:52:38 +02:00
|
|
|
|
using UnityEngine;
|
2020-08-07 20:20:13 +02:00
|
|
|
|
using NeonTea.Quakeball.Networking.Packets;
|
2020-08-08 03:17:54 +02:00
|
|
|
|
using NeonTea.Quakeball.Util;
|
2020-08-08 06:00:17 +02:00
|
|
|
|
using NeonTea.Quakeball.Networking;
|
|
|
|
|
using NeonTea.Quakeball.Networking.Instances;
|
2020-08-08 07:23:23 +02:00
|
|
|
|
using NeonTea.Quakeball.Combat;
|
2020-08-08 14:37:55 +02:00
|
|
|
|
using NeonTea.Quakeball.Interface;
|
2020-08-10 17:41:07 +02:00
|
|
|
|
using NeonTea.Quakeball.Animation;
|
2020-08-10 19:35:54 +02:00
|
|
|
|
using NeonTea.Quakeball.Audio;
|
2020-08-14 16:47:14 +02:00
|
|
|
|
using NeonTea.Quakeball.Items;
|
2020-08-02 02:52:38 +02:00
|
|
|
|
|
2020-08-07 20:23:21 +02:00
|
|
|
|
namespace NeonTea.Quakeball.Players {
|
2020-08-02 02:52:38 +02:00
|
|
|
|
/// <summary>The central glue class for players (both local and remote).</summary>
|
|
|
|
|
/// <remarks>Other classes will handle netcode/inputs, and then speak to this class in order to make the little people run around.</remarks>
|
|
|
|
|
[RequireComponent(typeof(CharacterController))]
|
|
|
|
|
public class Player : MonoBehaviour {
|
|
|
|
|
|
2020-08-08 01:57:12 +02:00
|
|
|
|
/// <summary>The duration after running off a cliff, during which the player should still be considered grounded.</summary>
|
2020-08-02 02:52:38 +02:00
|
|
|
|
public float CoyoteTime;
|
2020-08-08 03:44:45 +02:00
|
|
|
|
public float PingBias;
|
2020-08-02 02:52:38 +02:00
|
|
|
|
|
2020-08-06 19:58:10 +02:00
|
|
|
|
public MoveStyle[] MoveStyles;
|
2020-08-02 02:52:38 +02:00
|
|
|
|
|
2020-08-10 17:41:07 +02:00
|
|
|
|
public SoldierProceduralAnimator SoldierProceduralAnimator;
|
|
|
|
|
|
2020-08-14 19:48:27 +02:00
|
|
|
|
[Header("Item related")]
|
|
|
|
|
public Transform ItemRoot;
|
|
|
|
|
public GameObject[] PossibleItems;
|
|
|
|
|
public int CurrentItemIdx;
|
|
|
|
|
public Item CurrentItem;
|
|
|
|
|
|
2020-08-08 15:40:26 +02:00
|
|
|
|
[Tooltip("GameObjects that are disabled on death and re-enabled on respawn.")]
|
|
|
|
|
public GameObject[] DisabledOnDeath;
|
|
|
|
|
|
2020-08-08 05:20:23 +02:00
|
|
|
|
[Header("Shooting")]
|
|
|
|
|
[Tooltip("For raycasting the shoot target.")]
|
|
|
|
|
public Transform CameraRoot;
|
|
|
|
|
|
2020-08-08 03:17:54 +02:00
|
|
|
|
[Header("Visuals")]
|
2020-08-11 00:43:24 +02:00
|
|
|
|
public float LerpedDesyncDistance;
|
2020-08-08 03:17:54 +02:00
|
|
|
|
public DesyncLerper[] Lerpables;
|
2020-08-08 05:40:01 +02:00
|
|
|
|
public float Lean;
|
2020-08-08 03:17:54 +02:00
|
|
|
|
|
2020-08-08 07:19:46 +02:00
|
|
|
|
public ParticleSystem Splatter;
|
|
|
|
|
|
2020-08-08 07:59:44 +02:00
|
|
|
|
[Header("Audio")]
|
2020-08-08 08:50:55 +02:00
|
|
|
|
public AudioSource HitAudioSource;
|
|
|
|
|
public AudioSource LocalHitAudioSource;
|
2020-08-08 07:59:44 +02:00
|
|
|
|
|
2020-08-02 03:45:54 +02:00
|
|
|
|
[Header("Player rotation status")]
|
|
|
|
|
/// <summary>The pitch of the player's head.</summary>
|
|
|
|
|
public float Pitch;
|
|
|
|
|
/// <summary>The total yaw of the player. Head yaw is Yaw - BodyYaw.</summary>
|
|
|
|
|
public float Yaw;
|
2020-08-02 02:52:38 +02:00
|
|
|
|
|
|
|
|
|
[Header("Player movement status")]
|
|
|
|
|
/// <summary>The direction the player is going.</summary>
|
|
|
|
|
public Vector3 MoveDirection;
|
|
|
|
|
|
|
|
|
|
/// <summary>The amount of movement the player wants to happen.</summary>
|
|
|
|
|
/// <remarks>Without analog controls, always 0 or 1.</remarks>
|
|
|
|
|
public float InputSpeed;
|
|
|
|
|
|
|
|
|
|
/// <summary>The way the player is moving.</summary>
|
2020-08-06 19:58:10 +02:00
|
|
|
|
public byte CurrentMoveStyle = 0;
|
|
|
|
|
public MoveStyle MoveStyle => MoveStyles[CurrentMoveStyle];
|
2020-08-02 02:52:38 +02:00
|
|
|
|
|
|
|
|
|
[Header("Runtime computed values")]
|
|
|
|
|
/// <summary>The speed at which the player is currently moving across the ground.</summary>
|
|
|
|
|
public Vector3 GroundVelocity;
|
|
|
|
|
|
|
|
|
|
/// <summary>The speed at which the player is rising or falling.</summary>
|
|
|
|
|
public Vector3 GravitationalVelocity;
|
|
|
|
|
|
|
|
|
|
/// <summary>The timestamp of when the player was last on the ground.</summary>
|
|
|
|
|
public float GroundedTime;
|
|
|
|
|
|
2020-08-08 06:00:17 +02:00
|
|
|
|
/// <summary> The possible networked Id of this Player instance </summary>
|
|
|
|
|
public ulong NetId;
|
|
|
|
|
|
2020-08-08 14:37:55 +02:00
|
|
|
|
/// <summary> Is the player dead</summary>
|
|
|
|
|
public bool IsDead;
|
|
|
|
|
|
2020-08-08 01:57:12 +02:00
|
|
|
|
public float LatestGroundedY;
|
|
|
|
|
|
2020-08-02 02:52:38 +02:00
|
|
|
|
[Header("Misc. technical knobs")]
|
|
|
|
|
public float GroundCastLength = 0.2f;
|
|
|
|
|
public LayerMask GroundLayer;
|
|
|
|
|
|
|
|
|
|
[Header("Debug settings")]
|
|
|
|
|
public bool ShowGroundCast;
|
2020-08-05 23:15:28 +02:00
|
|
|
|
public bool ShowMoveVector;
|
2020-08-02 02:52:38 +02:00
|
|
|
|
|
|
|
|
|
private CharacterController CharacterController;
|
|
|
|
|
private Vector3 FeetPosition;
|
|
|
|
|
|
2020-08-14 16:47:14 +02:00
|
|
|
|
private float NextAllowedShot;
|
2020-08-08 14:37:55 +02:00
|
|
|
|
private float TimeofDeath;
|
2020-08-08 08:09:34 +02:00
|
|
|
|
|
2020-08-07 20:12:53 +02:00
|
|
|
|
/// <summary>Creates a PlayerUpdatePckt representing this Player's current status, for sending to other peers.</summary>
|
2020-08-07 23:59:51 +02:00
|
|
|
|
public PlayerUpdatePckt CreateUpdatePacket(ulong id = 0) {
|
2020-08-07 22:09:31 +02:00
|
|
|
|
return new PlayerUpdatePckt(MoveDirection, CurrentMoveStyle, Pitch, Yaw, id);
|
2020-08-07 20:12:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 00:15:33 +02:00
|
|
|
|
/// <summary>Updates this Player with the given packet.</summary>
|
|
|
|
|
public void ProcessUpdatePacket(PlayerUpdatePckt packet) {
|
2020-08-10 17:41:07 +02:00
|
|
|
|
if (!IsDead) {
|
|
|
|
|
Pitch = packet.Pitch;
|
|
|
|
|
Yaw = packet.Yaw;
|
|
|
|
|
MoveDirection = packet.MoveDirection;
|
|
|
|
|
CurrentMoveStyle = packet.MoveStyle;
|
|
|
|
|
}
|
2020-08-07 20:12:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 00:15:33 +02:00
|
|
|
|
/// <summary>Creates a PlayerSyncPacket representing this Player's position and velocity, for sending to the server.</summary>
|
2020-08-08 00:21:02 +02:00
|
|
|
|
public PlayerSyncPacket CreateSyncPacket(ulong id = 0, bool unsynced = false) {
|
|
|
|
|
return new PlayerSyncPacket(id, unsynced, transform.position, GroundVelocity);
|
2020-08-07 23:59:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 00:15:33 +02:00
|
|
|
|
/// <summary>Applies the sync packet, checking it for cheatiness if shouldApply is false.</summary>
|
2020-08-08 00:21:02 +02:00
|
|
|
|
public bool ProcessSyncPacket(PlayerSyncPacket syncPckt, bool shouldApplyWithoutInspection = true) {
|
2021-09-16 01:00:54 +02:00
|
|
|
|
bool ShouldApply = true;
|
2020-08-08 00:15:33 +02:00
|
|
|
|
if (!shouldApplyWithoutInspection) {
|
|
|
|
|
// TODO: Gaze into the crystal ball to determine the nefariousness level of the packet and update ShouldApply accordingly.
|
|
|
|
|
}
|
|
|
|
|
if (ShouldApply) {
|
2020-08-08 03:17:54 +02:00
|
|
|
|
Vector3 Delta = transform.position - syncPckt.Location;
|
2020-08-11 00:43:24 +02:00
|
|
|
|
if (Delta.magnitude < LerpedDesyncDistance) {
|
2020-08-08 03:17:54 +02:00
|
|
|
|
// Player is close enough to the sync packet, lerp them over.
|
|
|
|
|
foreach (DesyncLerper Lerper in Lerpables) {
|
|
|
|
|
Lerper.Offset(Delta);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-08 00:15:33 +02:00
|
|
|
|
transform.position = syncPckt.Location;
|
2020-08-08 00:26:28 +02:00
|
|
|
|
GroundVelocity = syncPckt.GroundVelocity;
|
2020-08-08 00:15:33 +02:00
|
|
|
|
}
|
2020-08-08 00:21:02 +02:00
|
|
|
|
return ShouldApply;
|
2020-08-07 23:59:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-07 22:25:56 +02:00
|
|
|
|
public bool Jump() {
|
2020-08-08 03:44:45 +02:00
|
|
|
|
bool IsCoyoteTime = Time.time - GroundedTime <= CoyoteTime + PingBias;
|
2020-08-08 01:57:12 +02:00
|
|
|
|
if (IsCoyoteTime || IsGrounded()) {
|
2020-08-07 22:25:56 +02:00
|
|
|
|
GravitationalVelocity = Vector3.up * MoveStyle.JumpVelocity;
|
2020-08-08 01:57:12 +02:00
|
|
|
|
Vector3 Pos = transform.position;
|
|
|
|
|
Pos.y = LatestGroundedY;
|
|
|
|
|
transform.position = Pos;
|
2020-08-07 22:25:56 +02:00
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 04:26:17 +02:00
|
|
|
|
public void Shoot() {
|
2020-08-14 16:47:14 +02:00
|
|
|
|
float next = NextAllowedShot;
|
2020-08-08 08:09:34 +02:00
|
|
|
|
if (Net.Singleton.Instance is Server) {
|
|
|
|
|
float ping = ((Server)Net.Singleton.Instance).Players[NetId].Ping;
|
2020-08-14 16:47:14 +02:00
|
|
|
|
next -= ping;
|
2020-08-08 08:09:34 +02:00
|
|
|
|
}
|
2020-08-14 16:47:14 +02:00
|
|
|
|
if (Time.time < next) {
|
2020-08-08 08:09:34 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-14 19:48:27 +02:00
|
|
|
|
NextAllowedShot = Time.time + CurrentItem.Cooldown;
|
|
|
|
|
CurrentItem.Shoot(this);
|
2020-08-08 04:26:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 09:08:10 +02:00
|
|
|
|
public void Hit(ulong sourceUid) {
|
2020-08-10 17:41:07 +02:00
|
|
|
|
Terminal.Singleton.Println($"{NetId} hit sourceIid: {sourceUid}");
|
2020-08-08 08:50:37 +02:00
|
|
|
|
if (Net.Singleton.Instance is Server) {
|
2020-08-08 14:37:55 +02:00
|
|
|
|
((Server)Net.Singleton.Instance).HandlePlayerDeath(NetId, sourceUid);
|
2020-08-08 08:50:37 +02:00
|
|
|
|
}
|
2020-08-08 09:08:10 +02:00
|
|
|
|
bool IsLocal = true;
|
|
|
|
|
if (Net.Singleton.Instance != null) {
|
|
|
|
|
IsLocal = Net.Singleton.Instance.LocalPlayer.Id == sourceUid;
|
|
|
|
|
}
|
2020-08-08 07:19:46 +02:00
|
|
|
|
Splatter.Play();
|
2020-08-08 08:50:55 +02:00
|
|
|
|
HitAudioSource.Play();
|
2020-08-08 09:34:09 +02:00
|
|
|
|
if (IsLocal) {
|
|
|
|
|
Net.Singleton.Instance.LocalPlayer.Controlled.LocalHitAudioSource.Play();
|
|
|
|
|
}
|
2020-08-08 06:00:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 14:37:55 +02:00
|
|
|
|
/// <summary>Called when this Player is dead</summary>
|
|
|
|
|
public void Dead(ulong killer) {
|
2020-08-10 17:41:07 +02:00
|
|
|
|
Terminal.Singleton.Println($"{killer} killed me: {NetId}, deadness currently: {IsDead}");
|
2020-08-08 14:37:55 +02:00
|
|
|
|
if (IsDead) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (Net.Singleton.Instance != null && Net.Singleton.Instance.LocalPlayer.Id == NetId) {
|
|
|
|
|
string name = $"Connection {killer}";
|
|
|
|
|
GameObject.FindGameObjectWithTag("DeadScreen").GetComponent<DeadScreen>().StartCountdown(name);
|
2020-08-08 15:22:46 +02:00
|
|
|
|
Net.Singleton.Instance.LocalPlayer.Controlled.GetComponent<LocalPlayer>().DisableInput += 1;
|
2020-08-08 14:37:55 +02:00
|
|
|
|
}
|
2020-08-10 17:41:07 +02:00
|
|
|
|
SoldierProceduralAnimator.StartRagdoll();
|
2020-08-08 14:37:55 +02:00
|
|
|
|
TimeofDeath = Time.time;
|
2020-08-08 15:22:46 +02:00
|
|
|
|
MoveDirection = Vector3.zero;
|
2020-08-08 15:40:26 +02:00
|
|
|
|
foreach (GameObject obj in DisabledOnDeath) {
|
|
|
|
|
obj.SetActive(false);
|
|
|
|
|
}
|
2020-08-08 14:37:55 +02:00
|
|
|
|
IsDead = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Called when this Player is respawned, after dying.</summary>
|
|
|
|
|
public void Respawn(Vector3 location) {
|
2020-08-10 17:41:07 +02:00
|
|
|
|
Terminal.Singleton.Println($"Respawned (I am {NetId}), deadness before proper resurrection: {IsDead}");
|
2020-08-10 02:19:21 +02:00
|
|
|
|
if (Net.Singleton.Instance != null && Net.Singleton.Instance.LocalPlayer.Id == NetId && IsDead) {
|
2020-08-08 14:37:55 +02:00
|
|
|
|
GameObject.FindGameObjectWithTag("DeadScreen").GetComponent<DeadScreen>().Open = false;
|
2020-08-08 15:22:46 +02:00
|
|
|
|
Net.Singleton.Instance.LocalPlayer.Controlled.GetComponent<LocalPlayer>().DisableInput -= 1;
|
2020-08-08 14:37:55 +02:00
|
|
|
|
}
|
|
|
|
|
transform.position = location;
|
2020-08-10 17:41:07 +02:00
|
|
|
|
SoldierProceduralAnimator.StopRagdoll();
|
2020-08-08 15:40:26 +02:00
|
|
|
|
foreach (GameObject obj in DisabledOnDeath) {
|
|
|
|
|
obj.SetActive(true);
|
|
|
|
|
}
|
2020-08-08 14:37:55 +02:00
|
|
|
|
IsDead = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-02 02:52:38 +02:00
|
|
|
|
public bool IsGrounded() {
|
2020-08-08 01:57:12 +02:00
|
|
|
|
return CharacterController.isGrounded && Vector3.Dot(GravitationalVelocity, Vector3.down) >= 0;
|
2020-08-02 02:52:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-14 19:48:27 +02:00
|
|
|
|
public void SwitchItem(int index) {
|
|
|
|
|
if (CurrentItem != null) {
|
|
|
|
|
CurrentItem.SwitchedOut();
|
|
|
|
|
}
|
|
|
|
|
GameObject obj = GameObject.Instantiate(PossibleItems[index]);
|
2020-08-20 20:55:11 +02:00
|
|
|
|
|
2020-08-14 19:48:27 +02:00
|
|
|
|
Vector3 localPos = obj.transform.localPosition;
|
2020-08-20 20:55:11 +02:00
|
|
|
|
Quaternion localRot = obj.transform.localRotation;
|
2020-08-14 19:48:27 +02:00
|
|
|
|
obj.transform.parent = ItemRoot.transform;
|
|
|
|
|
obj.transform.localPosition = localPos;
|
2020-08-20 20:55:11 +02:00
|
|
|
|
obj.transform.localRotation = localRot;
|
|
|
|
|
|
2020-08-14 19:48:27 +02:00
|
|
|
|
CurrentItem = obj.GetComponent<Item>();
|
|
|
|
|
CurrentItem.Holder = this;
|
|
|
|
|
CurrentItem.Switched();
|
|
|
|
|
SoldierProceduralAnimator.OnItemSwitched();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-10 01:27:48 +02:00
|
|
|
|
/// <summary>The normal of the ground below the player. If there is no ground, it's <c>Vector3.up</c> by default.</summary>
|
|
|
|
|
private Vector3 GroundCast() {
|
|
|
|
|
RaycastHit hit;
|
|
|
|
|
Vector3 feetPosition = FeetPosition + transform.position + CharacterController.center + Vector3.down * CharacterController.height / 2;
|
|
|
|
|
if (ShowGroundCast) {
|
|
|
|
|
Debug.DrawLine(feetPosition, feetPosition - Vector3.up, Color.red, 1f);
|
|
|
|
|
}
|
|
|
|
|
if (Physics.Raycast(feetPosition, -Vector3.up, out hit, GroundCastLength, GroundLayer)) {
|
|
|
|
|
return hit.normal;
|
|
|
|
|
} else {
|
|
|
|
|
return Vector3.up;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-14 19:48:27 +02:00
|
|
|
|
private void Start() {
|
|
|
|
|
SwitchItem(CurrentItemIdx);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-02 02:52:38 +02:00
|
|
|
|
private void Awake() {
|
|
|
|
|
CharacterController = GetComponent<CharacterController>();
|
|
|
|
|
FeetPosition = transform.position + CharacterController.center - Vector3.up * (CharacterController.height / 2 + CharacterController.skinWidth / 2);
|
2020-08-08 01:26:56 +02:00
|
|
|
|
if (GameObject.FindObjectOfType<Util.PhysicsSyncer>() == null) {
|
|
|
|
|
Debug.LogWarning("Player.Awake: There is no PhysicsSyncer in this scene! Some code will not work as expected.");
|
|
|
|
|
}
|
2020-08-02 02:52:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update() {
|
2020-08-08 14:37:55 +02:00
|
|
|
|
if (Net.Singleton.Instance is Server) {
|
|
|
|
|
if (IsDead && (Time.time - TimeofDeath > 3)) {
|
|
|
|
|
((Server)Net.Singleton.Instance).HandlePlayerRespawn(NetId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 05:40:01 +02:00
|
|
|
|
float TargetLean = -Vector3.Dot(GroundVelocity / MoveStyle.TargetVelocity, CameraRoot.right) * MoveStyle.LeanDegrees;
|
|
|
|
|
Lean = Mathf.Lerp(Lean, TargetLean, 30f * Time.deltaTime);
|
2020-08-10 19:49:24 +02:00
|
|
|
|
CameraRoot.localEulerAngles = new Vector3(Pitch - CameraRoot.parent.eulerAngles.x, Yaw - CameraRoot.parent.eulerAngles.y, Lean);
|
2020-08-08 14:37:55 +02:00
|
|
|
|
}
|
2020-08-08 05:40:01 +02:00
|
|
|
|
|
2020-08-08 14:37:55 +02:00
|
|
|
|
private void LateUpdate() {
|
2020-08-02 03:45:54 +02:00
|
|
|
|
UpdateMovement();
|
2020-08-02 02:52:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-02 03:45:54 +02:00
|
|
|
|
private void UpdateMovement() {
|
2020-08-02 02:52:38 +02:00
|
|
|
|
bool Grounded = IsGrounded();
|
|
|
|
|
|
2020-08-07 22:25:56 +02:00
|
|
|
|
bool FallingDown = Vector3.Dot(Vector3.down, GravitationalVelocity) > 0;
|
|
|
|
|
if (Grounded && FallingDown) {
|
2020-08-10 01:27:48 +02:00
|
|
|
|
GravitationalVelocity = GroundCastLength * Vector3.down;
|
2020-08-07 22:25:56 +02:00
|
|
|
|
} else if (!Grounded) {
|
2020-08-02 03:45:54 +02:00
|
|
|
|
GravitationalVelocity += Physics.gravity * Time.deltaTime;
|
2020-08-02 02:52:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-10 18:42:06 +02:00
|
|
|
|
Vector3 GroundNormal = GroundCast();
|
|
|
|
|
|
2020-08-02 02:52:38 +02:00
|
|
|
|
float FrictionVelocityFactor = Mathf.Max(GroundVelocity.magnitude, MoveStyle.StopVelocity);
|
2020-08-02 03:45:54 +02:00
|
|
|
|
float Deccel = FrictionVelocityFactor * Time.deltaTime;
|
2020-08-10 18:42:06 +02:00
|
|
|
|
if (Grounded || GroundNormal != Vector3.up) {
|
2020-08-02 02:52:38 +02:00
|
|
|
|
Deccel *= MoveStyle.Friction;
|
|
|
|
|
} else {
|
|
|
|
|
Deccel *= MoveStyle.AirFriction;
|
|
|
|
|
}
|
|
|
|
|
float FrictionedVelocity = Mathf.Max(0, GroundVelocity.magnitude - Deccel);
|
|
|
|
|
GroundVelocity = GroundVelocity.normalized * FrictionedVelocity;
|
|
|
|
|
|
|
|
|
|
Vector3 FixedHeading = Vector3.ProjectOnPlane(MoveDirection, GroundNormal).normalized;
|
|
|
|
|
float CurrentSpeed = Vector3.Dot(GroundVelocity, FixedHeading);
|
2020-08-02 03:45:54 +02:00
|
|
|
|
float Acceleration = MoveStyle.TargetVelocity * Time.deltaTime;
|
2020-08-10 18:42:06 +02:00
|
|
|
|
if (Grounded || GroundNormal != Vector3.up) {
|
2020-08-02 02:52:38 +02:00
|
|
|
|
Acceleration *= MoveStyle.Acceleration;
|
|
|
|
|
} else {
|
|
|
|
|
Acceleration *= MoveStyle.AirAcceleration;
|
|
|
|
|
}
|
|
|
|
|
Acceleration = Mathf.Min(Acceleration, MoveStyle.TargetVelocity - CurrentSpeed);
|
|
|
|
|
GroundVelocity += FixedHeading * Acceleration;
|
|
|
|
|
|
2020-08-10 18:42:06 +02:00
|
|
|
|
CharacterController.Move((GravitationalVelocity + GroundVelocity) * Time.deltaTime);
|
2020-08-02 02:52:38 +02:00
|
|
|
|
if (CharacterController.isGrounded) {
|
2020-08-02 03:45:54 +02:00
|
|
|
|
GroundedTime = Time.time;
|
2020-08-08 01:57:12 +02:00
|
|
|
|
LatestGroundedY = transform.position.y;
|
|
|
|
|
}
|
|
|
|
|
if (GravitationalVelocity.y > 0.1 && Mathf.Abs(CharacterController.velocity.y) < 0.1) {
|
2020-08-10 01:27:48 +02:00
|
|
|
|
// Hit a roof while jumping, reset falling velocity downwards (same as "static gravity" when grounded)
|
|
|
|
|
GravitationalVelocity.y = GroundCastLength;
|
2020-08-02 02:52:38 +02:00
|
|
|
|
}
|
2020-08-10 18:42:06 +02:00
|
|
|
|
|
2020-08-05 23:15:28 +02:00
|
|
|
|
if (ShowMoveVector) {
|
2020-08-10 01:27:48 +02:00
|
|
|
|
Debug.DrawLine(transform.position + CharacterController.center, transform.position + CharacterController.center + GravitationalVelocity, Color.yellow, 1.0f);
|
|
|
|
|
Debug.DrawLine(transform.position + CharacterController.center, transform.position + CharacterController.center + GroundVelocity, Color.green, 1.0f);
|
2020-08-05 23:15:28 +02:00
|
|
|
|
}
|
2020-08-06 19:58:10 +02:00
|
|
|
|
|
|
|
|
|
float TargetBobbiness = Grounded ? GroundVelocity.magnitude / MoveStyle.TargetVelocity : 0;
|
2020-08-14 16:47:14 +02:00
|
|
|
|
|
2020-08-14 19:48:27 +02:00
|
|
|
|
CurrentItem.Animator.SetLayerWeight(1, Mathf.Lerp(CurrentItem.Animator.GetLayerWeight(1), TargetBobbiness, 10f * Time.deltaTime));
|
2020-08-02 02:52:38 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|