How to get total power of controller stick?

Godot Version

v4.2.1.stable.mono.official [b09f793f5]

Question

How to get the combined power of left joy-stick?

I’m making an Animal Crossing Style movement system for my character and have keyboard and controller support working.

I would like to change the speed relative to the power of the left stick (keyboard should be max power)

Heres the relevant code:

// Get the input direction
var inputDir = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down");
var direction = (Transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y)).Normalized();
var sum = direction * SPEED;
// Finally, Move.
MoveAndCollide(sum);

I know you can get the strength of one using Input.GetActionStrength("Whatever") But I need to get it total. I could just get them all and add them together but that feels… wrong

Oh, while we’re at it does anybody have code to rotate “this” in whatever direction the stick is going

Input.get_vector() already gives you the strength in both axis.

You could consider remembering the highest absolute value to obtain the input speed. Normalize the vector to create a directional vector and multiply it by the speed, input speed, and delta.

code vith no godot lib

double x = 0.5;
double y = 0.5;

// Calculate the input speed
double inputSpeed = Math.Max(Math.Abs(x), Math.Abs(y));

// Calculate the magnitude of the vector
double c = Math.Sqrt(x * x + y * y);

// Normalize both x and y with respect to the magnitude c
x = x / c * inputSpeed;
y = y / c * inputSpeed;

Just got home, Looking at both your solutions now

So, I got rid of the multiplication, The player either moves at full speed or none, Is there a different function to move it that would allow for the slow movement?

this why you needed inputSpeed. You needed know which axis is highest to determine speed.

Awesome! Thanks

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.