0
\$\begingroup\$

Apologies for the bad title -- I have no idea how to explain this problem succinctly.

I have two 3D vectors. One is the player's movement direction, and the other is the direction towards a wall they are climbing. I need to make it so when the player is moving towards or away from the wall, they are actually moving up and down. All while preserving the sideways movement as it would be without climbing.

I've tried to understand cross products and other vector maths, but I cannot figure this out. Thank you for any help you can provide.

Edit: an simple scenario for clarity (y = vertical):

Vector3 directionOfMovement = (1.0, 0.0, 0.0)
Vector3 directionTowardsWall = (1.0, 0.0, 0.0)
Vector3 myRequiredResult = (0.0, 1.0, 0.0)

I need it to work when the directions are at odd angles as well.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$
// If directionTowardsWall has magnitude 1.0, this dot product gives the
// signed length of the component of movement along that direction.
float projection = Vector3.Dot(directionOfMovement, directionTowardsWall);

// That component as a vector is then that length times the direction.
Vector3 movementTowardsWall =  projection * directionTowardsWall;

// The component parallel to the wall is what remains after subtracting 
// the towards/away component.
Vector3 movementAlongWall = directionOfMovement - movementTowardsWall;

// And the movement on the vertical axis is the signed length of the projection.
movementAlongWall.y += projection;
\$\endgroup\$
1
  • \$\begingroup\$ Perfect solution, great explanation in comments. Thanks! \$\endgroup\$ Commented Jun 10, 2023 at 15:53

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.