Skip to content

Commit

Permalink
Medidor de velocidade e mais movimentação
Browse files Browse the repository at this point in the history
A movimentação ainda esta rapida, mas agora temos um medidor de velocidade para provar isso :)
  • Loading branch information
Andre-Messias committed Sep 7, 2024
1 parent 712b6ba commit 7dfa629
Show file tree
Hide file tree
Showing 77 changed files with 13,650 additions and 27 deletions.
356 changes: 350 additions & 6 deletions Assets/Scenes/TestScene.unity

Large diffs are not rendered by default.

93 changes: 72 additions & 21 deletions Assets/Scripts/Player/PlayerMovement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public class PlayerMovement : MonoBehaviour
[Range(0, 1)]
[SerializeField] private float gravityPercentWhileJumping = 0.75f;
[SerializeField] private float jumpForce = 15;
private bool _isJumping = false;
private bool _canJump = false;
[SerializeField] private bool _isJumping = false;
[SerializeField] private bool _canJump = false;

// Checks
private bool _isGrounded = false;
[SerializeField] private bool _isGrounded = false;
[SerializeField] private LayerMask groundLayer;
[SerializeField] private float changeIsGroundedStateDelay = 0.1f;
private Coroutine _cancellingGrounded;
Expand Down Expand Up @@ -69,15 +69,15 @@ private void Start()
{
if(rb == null)
Generics.ReallyTryGetComponent(gameObject, out rb);
_stopDefaultDuration = staticDesacellerationTime.GetDuration();
_groundMoveDefaultDuration = groundMoveTime.GetDuration();

if (firstPerson == null)
Generics.ReallyTryGetComponent(gameObject, out firstPerson);
fovChangeEffect.SetStartValue(firstPerson.baseFOV);
_fovChangeEffectDefaultDuration = fovChangeEffect.GetDuration();
}

public Vector2 A = new();

private void Update()
{
GetInputs();
Expand Down Expand Up @@ -138,7 +138,6 @@ void GroundMoveManagement()
if (!_isStatic)
{
staticDesacellerationTime.Reset();
staticDesacellerationTime.SetDuration(_stopDefaultDuration);
GroundMove();
}
else if(!dashState)
Expand All @@ -147,36 +146,58 @@ void GroundMoveManagement()
StaticStop();
}
}
else
{
staticDesacellerationTime.Reset();
groundMoveTime.Reset();
}
}


Vector3 _startGroundMoveVelocity;
float _groundMoveDefaultDuration;
void GroundMove()
{
groundMoveTime.Update(Time.deltaTime);
if(groundMoveTime.GetValue() == 0)
StartGroundMove();

Vector3 maxVelocity = (_planeOrientationForward * _keyboard.y + _planeOrientationRight * _keyboard.x) * groundMoveSpeed;
Vector3 moveVelocity = Vector3.Lerp(Vector3.zero, maxVelocity, groundMoveTime.GetValue());
groundMoveTime.Update(Time.deltaTime);
Vector3 moveVelocity = Vector3.Lerp(_startGroundMoveVelocity, maxVelocity, groundMoveTime.GetValue());
rb.velocity = moveVelocity + new Vector3(0, rb.velocity.y, 0);
}

void StartGroundMove()
{
_startGroundMoveVelocity = rb.velocity;
_startGroundMoveVelocity.y = 0;
groundMoveTime.SetDuration(_groundMoveDefaultDuration + GetGroundMoveExtraTime(rb.velocity.magnitude));
}

float GetGroundMoveExtraTime(float x) => (4 / (1 + Mathf.Pow((float)Math.E, -0.06f * (x - 70))));

private Vector3 _staticStartVelocity;
private float _stopExtraDuration;
private float _stopDefaultDuration;
void StaticStop()
{
// Stop player logic
if (staticDesacellerationTime.GetValue() == 0)
{
_staticStartVelocity = rb.velocity;

_stopDefaultDuration = staticDesacellerationTime.GetDuration();
_stopExtraDuration = GetStopExtraTime(rb.velocity.magnitude);
staticDesacellerationTime.SetDuration(_stopDefaultDuration + _stopExtraDuration);
}
StartStaticStop();

staticDesacellerationTime.Update(Time.deltaTime);
rb.velocity = Vector3.Lerp(_staticStartVelocity, new Vector3(0, rb.velocity.y, 0), staticDesacellerationTime.GetValue());
rb.velocity = new Vector3(0, rb.velocity.y, 0) + Vector3.Lerp(_staticStartVelocity, Vector3.zero, staticDesacellerationTime.GetValue());
}

float GetStopExtraTime(float x) => (4 / (1 + Mathf.Pow((float) Math.E, (float) -0.08 * (x - 60))));
void StartStaticStop()
{
_staticStartVelocity = rb.velocity;
_staticStartVelocity.y = 0;
_stopExtraDuration = GetStopExtraTime(rb.velocity.magnitude);
staticDesacellerationTime.SetDuration(_stopDefaultDuration + _stopExtraDuration);
}

float GetStopExtraTime(float x) => (4 / (1 + Mathf.Pow((float) Math.E, -0.08f * (x - 60))));

#endregion

Expand All @@ -185,7 +206,7 @@ void StaticStop()
void JumpManagement()
{
_isJumping = IsJumping();
_canJump = _isGrounded || CollidersCointainsGround(Physics.OverlapSphere(foot.position, preGroundSphereRadius));
_canJump = !_jumpCooldownState && (_isGrounded || CollidersCointainsGround(Physics.OverlapSphere(foot.position, preGroundSphereRadius)));
}

bool CollidersCointainsGround(Collider[] colliders)
Expand All @@ -198,13 +219,19 @@ bool CollidersCointainsGround(Collider[] colliders)
return false;
}

private float _jumpCooldown = 0.2f;
private bool _jumpCooldownState = false;
void Jump()
{
_jumpCooldownState = true;
rb.velocity = new Vector3(rb.velocity.x, Mathf.Clamp(rb.velocity.y, 0, Mathf.Infinity), rb.velocity.z);
rb.AddForce(body.transform.up * jumpForce, ForceMode.Impulse);
rb.AddForce(_normalVector * 0.5f * jumpForce, ForceMode.Impulse);
Invoke(nameof(JumpCooldown), _jumpCooldown);
}

void JumpCooldown() => _jumpCooldownState = false;

bool IsJumping() => (rb.velocity.y > 0 && Input.GetKey(KeyCode.Space) && !_isGrounded);

void DrawPreGroundSphere()
Expand All @@ -228,7 +255,10 @@ void Gravity()
private void AirMove()
{
if (_isGrounded) return;


Vector2 airVelocity = new Vector2(rb.velocity.x, rb.velocity.z);
airMoveSpeed = GetAirMoveSpeed(Mathf.Clamp(airVelocity.magnitude, 0, 50));

// Foward
rb.AddForce(_planeOrientationForward * _keyboard.y * airMoveSpeed);
// Sideways
Expand All @@ -243,14 +273,16 @@ Vector3 RemoveYFromVector(Vector3 vector)
vector *= magnitude;
return vector;
}

float GetAirMoveSpeed(float x) => (17 / (1 + Mathf.Pow((float)Math.E, 0.05f * (x - 30))));
#endregion

#region Dash

public void Dash()
{
Vector3 foward = orientation.transform.forward;
rb.velocity = new Vector3(foward.x, foward.y * 1.5f, foward.z) * rb.velocity.magnitude;
rb.velocity = new Vector3(foward.x, foward.y * 1.3f, foward.z) * (new Vector2(rb.velocity.x, rb.velocity.z).magnitude / 0.7f);
rb.AddForce(foward * dashForce, ForceMode.Impulse);
canDash = false;
dashState = true;
Expand Down Expand Up @@ -286,7 +318,7 @@ void RotateCameraEffect()
void FOVCameraEffect()
{
float newTarget;
if (dashState)
if (dashState || !_isGrounded)
{
newTarget = fovChangeEffectDashTarget;
fovChangeEffect.SetDuration(_fovChangeEffectDefaultDuration);
Expand Down Expand Up @@ -327,13 +359,32 @@ private void OnCollisionStay(Collision other)
_normalVector = normal;
if (_cancellingGrounded != null)
StopCoroutine(_cancellingGrounded);
break;
}
}

// Invoke ground/wall cancel, since we can't check normals with CollisionExit
_cancellingGrounded = StartCoroutine(ChangeIsGroundedState(changeIsGroundedStateDelay, false));
}

private void OnCollision(Collision other)
{
// Make sure we are only checking for walkable layers
int layer = other.gameObject.layer;
if (!IsGroundLayer(layer)) return;

// get if ground is working as a floor
for (int i = 0; i < other.contactCount; i++)
{
Vector3 normal = other.contacts[i].normal;
if (IsFloor(normal))
{
StartGroundMove();
break;
}
}
}

bool IsGroundLayer(int layer) => groundLayer == (groundLayer | (1 << layer));

bool IsFloor(Vector3 v)
Expand Down
36 changes: 36 additions & 0 deletions Assets/Test_Ranking.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mantega;
using TMPro;

public class Test_Ranking : MonoBehaviour
{
[Header("Movement Statistics")]
[SerializeField] private PlayerMovement playerMovement;
[SerializeField] private Rigidbody playerRb;
public TMP_Text text_playerSpeed;
public float speedRecord;
void Start()
{
if(playerMovement == null)
Generics.ReallyTryGetComponent(gameObject, out playerMovement);
playerRb = playerMovement.GetComponent<Rigidbody>();
}

// Update is called once per frame
void Update()
{
float playerSpeed = playerRb.velocity.magnitude;
if(playerSpeed > speedRecord)
speedRecord = playerSpeed;
SetMovementStatistics(playerSpeed);
}

public void SetMovementStatistics(float currentSpeed)
{
if (text_playerSpeed == null)
return;
text_playerSpeed.text = $"Velocity: {currentSpeed}\nRecord: {speedRecord}";
}
}
11 changes: 11 additions & 0 deletions Assets/Test_Ranking.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/TextMesh Pro.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/TextMesh Pro/Documentation.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/TextMesh Pro/Fonts.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions Assets/TextMesh Pro/Fonts/LiberationSans - OFL.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Digitized data copyright (c) 2010 Google Corporation
with Reserved Font Arimo, Tinos and Cousine.
Copyright (c) 2012 Red Hat, Inc.
with Reserved Font Name Liberation.

This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at: http://scripts.sil.org/OFL

-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------

PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide development of collaborative font projects, to support the font creation efforts of academic and linguistic communities, and to provide a free and open framework in which fonts may be shared and improved in partnership with others.

The OFL allows the licensed fonts to be used, studied, modified and redistributed freely as long as they are not sold by themselves. The fonts, including any derivative works, can be bundled, embedded, redistributed and/or sold with any software provided that any reserved names are not used by derivative works. The fonts and derivatives, however, cannot be released under any other type of license. The requirement for fonts to remain under this license does not apply to any document created using the fonts or their derivatives.

DEFINITIONS
"Font Software" refers to the set of files released by the Copyright Holder(s) under this license and clearly marked as such. This may include source files, build scripts and documentation.

"Reserved Font Name" refers to any names specified as such after the copyright statement(s).

"Original Version" refers to the collection of Font Software components as distributed by the Copyright Holder(s).

"Modified Version" refers to any derivative made by adding to, deleting, or substituting -- in part or in whole -- any of the components of the Original Version, by changing formats or by porting the Font Software to a new environment.

"Author" refers to any designer, engineer, programmer, technical writer or other person who contributed to the Font Software.

PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining a copy of the Font Software, to use, study, copy, merge, embed, modify, redistribute, and sell modified and unmodified copies of the Font Software, subject to the following conditions:

1) Neither the Font Software nor any of its individual components, in Original or Modified Versions, may be sold by itself.

2) Original or Modified Versions of the Font Software may be bundled, redistributed and/or sold with any software, provided that each copy contains the above copyright notice and this license. These can be included either as stand-alone text files, human-readable headers or in the appropriate machine-readable metadata fields within text or binary files as long as those fields can be easily viewed by the user.

3) No Modified Version of the Font Software may use the Reserved Font Name(s) unless explicit written permission is granted by the corresponding Copyright Holder. This restriction only applies to the primary font name as presented to the users.

4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font Software shall not be used to promote, endorse or advertise any Modified Version, except to acknowledge the contribution(s) of the Copyright Holder(s) and the Author(s) or with their explicit written permission.

5) The Font Software, modified or unmodified, in part or in whole, must be distributed entirely under this license, and must not be distributed under any other license. The requirement for fonts to remain under this license does not apply to any document created using the Font Software.

TERMINATION
This license becomes null and void if any of the above conditions are not met.

DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE FONT SOFTWARE.
8 changes: 8 additions & 0 deletions Assets/TextMesh Pro/Fonts/LiberationSans - OFL.txt.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Assets/TextMesh Pro/Fonts/LiberationSans.ttf
Binary file not shown.
19 changes: 19 additions & 0 deletions Assets/TextMesh Pro/Fonts/LiberationSans.ttf.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/TextMesh Pro/Resources.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Assets/TextMesh Pro/Resources/Fonts & Materials.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7dfa629

Please sign in to comment.