movement refactor

This commit is contained in:
GoaLitiuM
2021-09-13 19:08:22 +03:00
parent 582b1782e4
commit 76cb9c2070

View File

@@ -678,7 +678,7 @@ namespace Game
// TODO: snap to ground here
// jump
bool jumpAction = InputManager.GetAction("Jump");
if (jumped && !jumpAction)
@@ -686,58 +686,59 @@ namespace Game
else if (jumped && Time.GameTime - lastJumped >= autoJumpTime)
jumped = false; // jump timeout
if (onGround && jumpAction && !jumped)
{
Console.Print("jumping");
// reset velocity from gravity
if (-Vector3.Dot(Physics.Gravity.Normalized, velocity) < 0 &&
Vector3.Dot(velocity, traceGround.hitNormal) < -0.1)
{
velocity = Vector3.ProjectOnPlane(velocity, traceGround.hitNormal);
}
velocity += Vector3.Up * jumpVelocity;
onGround = false;
jumped = true;
lastJumped = Time.GameTime;
if (JumpLandSound != null && JumpLandSound.IsLoaded)
{
var audioSource = new AudioSource();
audioSource.Clip = JumpLandSound;
audioSource.Position = rootActor.Position; //new Vector3(-350, 176, 61);//rootActor.Position;
audioSource.Parent = Actor.Parent;
audioSource.Pitch = 1f;
audioSource.Play();
Destroy(audioSource, JumpLandSound.Length);
}
}
// ground friction
if (onGround)
{
float currentSpeed = velocity.Length;
// jump
if (jumpAction && !jumped)
{
Console.Print("jumping");
// reset velocity from gravity
if (-Vector3.Dot(Physics.Gravity.Normalized, velocity) < 0 &&
Vector3.Dot(velocity, traceGround.hitNormal) < -0.1)
{
velocity = Vector3.ProjectOnPlane(velocity, traceGround.hitNormal);
}
float control = currentSpeed < stopspeed ? stopspeed : currentSpeed;
var drop = control * friction * Time.DeltaTime;
velocity += Vector3.Up * jumpVelocity;
onGround = false;
jumped = true;
lastJumped = Time.GameTime;
float newspeed = currentSpeed - drop;
if (newspeed < 0)
newspeed = 0;
if (JumpLandSound != null && JumpLandSound.IsLoaded)
{
var audioSource = new AudioSource();
audioSource.Clip = JumpLandSound;
audioSource.Position = rootActor.Position; //new Vector3(-350, 176, 61);//rootActor.Position;
audioSource.Parent = Actor.Parent;
audioSource.Pitch = 1f;
if (currentSpeed < 0.0001f)
velocity *= 0;
else
velocity *= newspeed / currentSpeed;
}
audioSource.Play();
Destroy(audioSource, JumpLandSound.Length);
}
}
if (onGround) // ground acceleration
{
// ground friction
{
float currentSpeed = velocity.Length;
float control = currentSpeed < stopspeed ? stopspeed : currentSpeed;
var drop = control * friction * Time.DeltaTime;
float newspeed = currentSpeed - drop;
if (newspeed < 0)
newspeed = 0;
if (currentSpeed < 0.0001f)
velocity *= 0;
else
velocity *= newspeed / currentSpeed;
}
// ground acceleration
ApplyAcceleration(ref velocity, wishVelocity.Normalized, wishVelocity.Length, float.MaxValue,
accelerationGround);
}
else // air acceleration
else // air movement
{
var wishspeed = wishVelocity.Length;
if (wishspeed > maxAirSpeed)
@@ -746,10 +747,10 @@ namespace Game
if (strafeAcceleration != 0f)
ApplyAcceleration(ref velocity, wishVelocity.Normalized, wishspeed, maxAirStrafeSpeed,
strafeAcceleration);
}
if (!onGround)
// apply gravity
velocity += Physics.Gravity * Time.DeltaTime;
}
safePosition = rigidBody.Position;
currentVelocity = velocity;