#Im having a logical error

1 messages · Page 1 of 1 (latest)

lyric rune
#

(3D)So when my player jumps it goes 10 m up into the air.When a player moves , a new force is added,which reduces the jump because of a simple line of code, this being
⁨```cs
//Ground Check
IsGrounded = Physics.Raycast(Character.transform.position, Vector3.down, PlayerHeight * 0.5f + 0.2f, GroundLayer);

//Rigidbody
Vector2 Direction = MovementV2.ReadValue<Vector2>();

//Drag
if (IsGrounded)
{
Rigidbody.linearDamping = GroundDrag;
}
else
{
Rigidbody.linearDamping = 0;
}
//Force

float YVel = Rigidbody.linearVelocity.y;

Vector3 Z = Direction.x * Character.transform.right;
Vector3 X = Direction.y * Character.transform.forward;

Vector3 ForceDirection = (Z + X) * MovementSpeed;
ForceDirection.y = YVel;

if (IsGrounded == true)
{
Rigidbody.AddForce(ForceDirection.normalized * 10f, ForceMode.Force); //Here is the problem
if (CanJump == false)
{
CanJump = true;
}
if (CurrentJumps > 0)
{
CurrentJumps = 0;
}
}
else
{
Rigidbody.AddForce(ForceDirection.normalized * 10f * AirForce, ForceMode.Force); //Maybe its here too, AirForce is the slowness the player will get in the air so they aren't faster jumping than walking
}```⁩

How could I make movement without affecting the jumping?(this being adding the up direction * Jumping power as a force)
Video:
Firsts jumps are me without moving, the others are while I move forward

#

Do not worry about the weird falling when the jump ends ill fix that after I get a solution to this problem

karmic elm
#

the player isn't pitching up/down, correct?

#

oh you aren't looking up/down in that video so that shouldn't be a factor

#

ForceDirection.y = YVel
what's this for

#

seems like that's the cause

#

also the * MovementSpeed isn't doing anything

#

you should have that where you have the * 10fs instead

remote kayak
#

Ok so since I last saw this code it seems like you switched from directly setting your velocity to this "ForceDirection" variable to actually using AddForce

#

I had given him this ⁨ForceDirection.y = YVel⁩ code earlier when ForceDirection was being directly fed in as ⁨rb.linearVelocity = ForceDirection⁩. This was to preserve the existing Rigidbody y velocity.

Now that ForceDirection is actually being used as a force, e.g. with AddForce, that part is no longer necessary

#

When using AddForce, the Rigidbody's existing velocity will already be preserved

lyric rune
lyric rune
remote kayak
#

think about it

lyric rune
#

Tho how would I limit velocity ?

remote kayak
#

Either through drag (built-in or a custom one using forces) or by explicitly clamping the velocity in your code

lyric rune
#

I do it like this ⁨cs Vector3 LimitedVelocity = new Vector3(Limiting stuff) and then I add it

#

Wait a moment im going to show you how I do it right now

#

⁨```cs
private void SpeedControl()
{
Vector3 FlatVelocity = new Vector3(Rigidbody.linearVelocity.x, 0f, Rigidbody.linearVelocity.z);

 //Limit velocity if needed
 if (FlatVelocity.magnitude > MovementSpeed)
 {
     Debug.Log("Yes");
     float XLimit = Mathf.Clamp(Rigidbody.linearVelocity.x,-MovementSpeed,MovementSpeed);
     float ZLimit = Mathf.Clamp(Rigidbody.linearVelocity.z,-MovementSpeed,MovementSpeed);

     Vector3 LimitedVelocity = Rigidbody.linearVelocity;
     LimitedVelocity.x = XLimit;
     LimitedVelocity.z = ZLimit;

     Rigidbody.linearVelocity = LimitedVelocity;
 }

}```⁩

#

im back

#

this is how I do it

#

tho if I do it like this I also have to set the y velocity

#

Wait I think its fixed right now