Describing things in 3D can be hard, but I will do my best.
We have a ground (square) and a ball (sphere) in a 3D world. We are using Unity’s axis alignment, where forward is positive z, right is positive x and up is positive y.
The ball can be controlled by the player, which can give input in the x and z axes (y position being adjusted as needed while traversing the ground). At the start of the game the ball is located at (0,0,0).
Here is how I would like the ball to move depending on the input and the orientation of the ground. The input vector is (x,z). For the sake of simplicity we do not take any external forces into account when moving the ball.
I believe that the simplest way to describe what I want is the following.
First scenario: The ground has no rotation and the input is (1,1). The ball is moved to (1,0,1).
Second scenario: The position of the ball after it has been moved, relative the ground, in this scenario should be the same as in the first scenrio. The ground is rotated 89 degrees around the z axis and the input is (1,1). The ball will be trying to move through the ground, but I want it to travel in a direction somewhat similar to (0.02,0.98,1).
If we would have one screenshot for each scenario, after the ball has been moved, they would look identical if the camera had been rotated along with the ground.
I assume this can be solved by first translating the ball then do some rotation magic, but that probably is not an option since that assumes the ground does not change throughout the movement of the ball. I need to figure out the direction vector based on the orientation of the ground immediately below the ball and the input so that I can do some sort of (ray)casting along it, moving the ball in steps if the ground is uneven.
I have been able to move the ball along the surface of the ground, but the “direction vector along the surface” is only correct when the ground is not rotated or is rotated 90 degrees. Anything inbetween and it is off, so my math is wrong. Anyone?