I am completely lost with this, I thought it would be simple but it just doesn’t seem to be working.
Let me preface this that I have tried using ProjectOnPlane, which just breaks as soon as I start walking on a 90 degree incline.
I’m trying to achieve what is pictured below
However I cannot understand how to rotate my movement vector.
I tried taking the rotation of my character (this.transform.rotation) and multiplying it by my movement Vector, however this doesn’t seem to work as the resulting vector points in seemingly random directions.
How can I rotate my Movement Direction Vector depending on either the ground I’m standing on or the rotation of my character?
Let me go through what I’m doing here because that is not working at all.
// Getting Inputs from the Stick
Vector3 stickDirection = new Vector3(InputManager.horizontal, 0, InputManager.vertical);
// Getting the Rotation of the camera
Vector3 CameraDirection = camera.forward;
CameraDirection.y = 0.0f;
Quaternion referentialShift = Quaternion.LookRotation(CameraDirection);
// Multiplying Stick Vector with Camera Quaternion and transforming into world space
moveDirection = transform.TransformDirection(referentialShift * stickDirection);
This creates the red vector here, which is going straight through the wall
The yellow one is after using ProjectOnPlane with the normal of the ground.
I’m not really sure how the camera is relevant. Could you elaborate? My code will move your character “forward” regardless of their orientation, so if they’re walking up a wall, pressing W will walk them up instead of into the wall. Is that not the issue we’re trying to solve?
Here’s an example of my code on objects of various rotations, all moving forward and backward in respect to their orientation, when you press W or S.
No that is exactly the problem, but the rotation of the camera is needed because I need the character to move depending on the orientation of the camera.
Either way, even while not multiplying my Stick Vector with the Quaternion of the camera, my Movevement Vector (in red) still doesn’t adjust depending on the rotation of my character.
When you wrote “Don’t multiply it by the rotation”, you meant change this:
Is the transform you’re using in that line of code the character’s transform? Is your character’s default orientation (rotation: 0, 0, 0) looking down the Z axis?
Use the transform of whatever has the more accurate representation of the orientation of the character. Moving via velocity shouldn’t matter.
If all else fails, fall back to an empty GameObject with my code and try to figure out what other factors could be giving you incorrect values because like I posted in my gif, the code definitely moves forward and back relative to orientation.
I’ve been working with your suggestions and you were completely right.
The first problem was that I wasn’t using a specific rotation to use TransformDirection, so I changed it to use the rotation of my collider. However my collider was also rotating around itself, which meant that moving in any direction would make my character spin on himself.
So I made an empty gameobject, gave it only a Y rotation and a W rotation, then used its rotation with TransformDirection and everything worked.