Hey, first time poster!
I’m working on a 3D Space Arcade Game and I’ve been having a lot of fun but I’ve run into my first real mental roadblock.
To get the feel I want whilst using the physics system I’ve written the code at the bottom of this post and I would like to point out that I’m new to coding so if you look at this and are horrified I’m truly sorry! XD
My problem is I’m getting a fluctuation of +/- 1 magical unity unit of Vector3.Magnitude which I’m using for speed. Now I did my due diligence and my research into the issue would suggest that using rb.velocity and AddForce isn’t best practice and can lead to issues like this.
I’ve been through the documentation, i’ve found things like Lerp and SmoothDamp but these seem to relate to transform/rb.velocity than addForce. I guess what im asking is whats the best way to fix this?
Should i use transform instead? i’ve read it causes issues when collisions are involved.
Thanks in advance!
void HandleThrottle()
{
// relevant variables - thrust1D, throttlePosition, throttleIncriment
if (thrust1D > 0.1f)
{
throttlePosition = throttlePosition + throttleIncrement;
throttlePosition = Mathf.Clamp(throttlePosition, maxReverseThrottle, maxThrottle);
//UnityEngine.Debug.Log("Throttle Position" + throttlePosition);
}
else if (thrust1D < -0.1f)
{
throttlePosition = throttlePosition - throttleIncrement;
throttlePosition = Mathf.Clamp(throttlePosition, maxReverseThrottle, maxThrottle);
//UnityEngine.Debug.Log("Throttle Position" + throttlePosition);
}
else if (throttleCentre != 0)
{
throttlePosition = 0f;
}
speed = Vector3.Magnitude(rb.velocity);
int speedFloor = (int)Mathf.Floor(Mathf.Abs(speed));
UnityEngine.Debug.Log("Speed:" + speedFloor);
if (throttlePosition >= 0.1f && !boosting && speedFloor <= maxSpeed)
{
rb.AddRelativeForce(Vector3.forward * thrust * Time.deltaTime);
Vector3 clampedVelocity = Vector3.ClampMagnitude(rb.velocity, maxSpeed);
rb.velocity = clampedVelocity;
}
if (throttlePosition <= -0.1f && !boosting && speedFloor <= maxReverseSpeed)
{
rb.AddRelativeForce(Vector3.back * reverseThrust * Time.deltaTime);
Vector3 clampedVelocity = Vector3.ClampMagnitude(rb.velocity, maxReverseSpeed);
rb.velocity = clampedVelocity;
}
if (throttlePosition <= -0.1f && boosting && speedFloor <= maxReverseSpeed)
{
rb.AddRelativeForce(Vector3.back * reverseThrust * boostMultiplier * Time.deltaTime);
Vector3 clampedVelocity = Vector3.ClampMagnitude(rb.velocity, maxReverseSpeed * boostMultiplier);
rb.velocity = clampedVelocity;
}
if (throttlePosition >= 0.1f && boosting)
{
rb.AddRelativeForce(Vector3.forward * thrust * boostMultiplier * Time.deltaTime);
Vector3 clampedVelocity = Vector3.ClampMagnitude(rb.velocity, maxSpeed * boostMultiplier);
rb.velocity = clampedVelocity;
}
if (throttlePosition >= 0.1f && !boosting && speedFloor > maxSpeed)
{
rb.velocity -= rb.velocity.normalized * decelerationRate * Time.fixedDeltaTime;
}
}