vector3 lerp based on 2 different floats/axes?

I’m trying to move an object around my player in a 2.5d sidescroller game based on analog stick input. The thing is, my player rotates around in the world (with the camera fixed to the side) so it’s local horizontal direction (transform.right) is not always the global x direction same for it’s local up direction (transform.up). I had this working just fine back when the player’s movement direction was fixed on the X axis by setting an offset of the players position and adding the analog on the same axis like so:


Vector3 endPoint = new Vector3(transform.position.x + inputs.x * floraDistanceMultiplier, transform.position.y + inputs.y * floraDistanceMultiplier, transform.position.z);

but now that the player is “off axis” so to speak, this of course doesn’t work.

I am able to get it working one axis at a time:


Vector3 h = player.transform.position + player.transform.right * floraDistanceMultiplier * inputs.x;
Vector3 v = player.transform.position + player.transform.up * floraDistanceMultiplier * inputs.y;

But couldn’t figure out how to get both vectors influencing the final position. Using localPosition or applying the x and y of the final vector manually didn’t work I’m assuming because x and y are always global rather than defining the transform.right as a long some kind of local X axis.

I had the thought to lerp the two - which almost has successful results, in that the end point properly move around the player from left to up only though, not in a full circle around the player, and the diagonal is severely closer than the full extent at up and left.


Vector3 endPoint = Vector3.Lerp(h, v, inputs.x + inputs.y);

is there a better way to do this overall? Or is there some way to lerp between 2 vectors using 2 floats? (the analog stick x and y) or using a vector? Thanks!

Let’s break down your original working function;

// This;
Vector3 endPoint = new Vector3(transform.position.x + inputs.x * floraDistanceMultiplier, transform.position.y + inputs.y * floraDistanceMultiplier, transform.position.z);

// Can be expanded and simplified to this;
Vector3 endPoint = transform.position + (Vector3.right * inputs.x + Vector3.up * inputs.y) * floraDistanceMultiplier;

As you’ve already noticed, Vector3.up is in world-space and its player-relative equivalent is player.transform.up .

As such, we can now replace the world-space calls with their local-space equivalents to get the same effect relative to the player;

Vector3 endPoint = player.transform.position + (player.transform.right * inputs.x + player.transform.up * inputs.y) * floraDistanceMultiplier;