Ive been a few hours researching since i pretty much suck at maths and after some work i got it to work my issue is that i can’t get to cap the maximum velocity correctly so it just ends up speeding more each time
using Godot;
using System;
public partial class Character : RigidBody3D
{
public float acceleration = 5f;
public float friction = 0.85f;
public float maxVelocity = 40f;
public StateMachine stateMachine = new StateMachine();
public virtual void Initialize()
{
stateMachine.Initialize(this);
}
public void Move(Vector3 direction,double delta)
{
var wishDir = direction.Normalized() * maxVelocity;
wishDir = wishDir.Clamp(-maxVelocity,maxVelocity);
if (wishDir != Vector3.Zero)
{
var force = wishDir * acceleration;
ApplyCentralForce(force);
} else
{
var force = -LinearVelocity * friction;
ApplyCentralForce(force);
}
}
}
Allright could you explain further still because im blowing my mind trynna fix this out i tried a lot of things capping the force in many different ways wich it seems to obviusly not work and about the velocity i’ll try doing what you say
I clamped the velocity in the said function ans still nothing same issue idk if i did some calculation incorrectly
using Godot;
using System;
public partial class Character : RigidBody3D
{
public float acceleration = 5f;
public float friction = 0.85f;
public float maxVelocity = 40f;
public StateMachine stateMachine = new StateMachine();
public virtual void Initialize()
{
stateMachine.Initialize(this);
}
public void Move(Vector3 direction,double delta)
{
var wishDir = direction.Normalized() * maxVelocity;
if (wishDir != Vector3.Zero)
{
var force = wishDir * acceleration;
GD.Print(force.Length());
ApplyCentralForce(force);
} else
{
var force = -LinearVelocity * friction;
ApplyCentralForce(force);
}
}
public override void _IntegrateForces(PhysicsDirectBodyState3D state)
{
state.LinearVelocity.Clamp(-maxVelocity,maxVelocity);
}
}
Yeah i noticed still i used another approach and works my only issue is that maxvel number is very small but i don’t mind it using
using Godot;
using System;
public partial class Character : RigidBody3D
{
public float acceleration = 5f;
public float friction = 0.85f;
public float maxVelocity = 5f;
public StateMachine stateMachine = new StateMachine();
public virtual void Initialize()
{
stateMachine.Initialize(this);
}
public void Move(Vector3 direction,double delta)
{
var wishDir = direction.Normalized() \* maxVelocity;
if (wishDir != Vector3.Zero)
{
Vector3 horizontalVel = new Vector3(LinearVelocity.X, 0, LinearVelocity.Z);
Vector3 velocityChange = wishDir - horizontalVel;
var force = velocityChange \* acceleration \* Mass;
ApplyCentralForce(force);
GD.Print(LinearVelocity.Length());
} else
{
var force = -LinearVelocity \* friction \* Mass;
ApplyCentralForce(force);
}
}
public override void \_IntegrateForces(PhysicsDirectBodyState3D state)
{
}