How can I walk in the right direction?

Hello, I am a couple days into Unity (with several years of Lua and Python and a few months of C# under my belt).

I am working on a basic player controller for my first project, and I am using a Constant Force component to achieve the style I want.

If I want to walk up a hill, the force being applied to the character of course needs to be modified to be relative to the normal of the face right below the character. I have already gotten that programmed right here:

// Get the normal of the ground the player is on.
Vector3 groundNormal = groundRay.normal;
Quaternion groundRotation = Quaternion.LookRotation(groundNormal);

// Create a transform that has the purpose of redirectionalizing the walk force to work with the normal of the ground underneath the player.
Transform groundPivot = vectorLibrary.transform.newTransform(Vector3.zero, groundRotation);

// Just for a visual of the ground pivot.
debugCube.transform.rotation = groundPivot.rotation;
debugCube.transform.position = groundRay.point;

Vector3 directionalizedWalkForce = groundPivot.InverseTransformDirection(playerModelWalkForce);

playerForce.relativeForce = directionalizedWalkForce * setWalkSpeed;

The issue comes when the redirectionalized walk force’s positive Z direction is no longer aligned with the direction the character is facing, resulting in the movement system becoming all wonky. Here is a screenshot for that.


Notice how the red line in the debug cube is not facing the direction my camera (the character) is facing. This makes me walk in the wrong direction

I am not ther most adept at dealing with vector related math, so if anybody could help me, that would be great. Thank you for your time reading this!

If I am using any bad practices, please let me know! Also, ‘vectorLibrary’ is just a quick library I set up to perform some small tasks. In the case of ‘newTransform’, I’m just running a quick function that generates a new empty game object and returns the transform with memory related optimizations.

I think you would just take the direction the player is facing and project it onto a plane of the ground normal: Unity - Scripting API: Vector3.ProjectOnPlane

1 Like

Thanks for the reply! I am going to mess around with this method and see if I get any success out of it.

1 Like

Welcome! Make sure this is a focus you want to play with… character controllers are notoriously finicky, to the point where I feel most people starting out should just use an existing one and get on with customizing it.

That way you know you’re starting from success and can build new features on top of a solid basis.

If you insist on rolling your own, you will need to both make the controller AND make your custom features work in it.

Generally these are two very separate things, and as a beginner you are not really be equipped to reason about the problem space.

I’ve been using Unity for 12 years and I always reach for existing controllers, such as this one:

1 Like

@Kurt-Dekker
Thanks for your response! Though I’m a beginner, I have some decent experience with general programming and I’ve also gotten a tiny bit of C# under my belt. I’ve trudged through a few different frameworks (mostly in Roblox though), so I’m not entirely new to the game haha

I mainly find joy in doing problem solving and creating smaller pieces to the machine—in other words, coding everything—so making things like player controllers is the type of thing I’m aiming to do. If my main goal is to create a fully planned game, then I will definitely take your feedback and look for existing assets to base off of and put my main energy into developing the idea.

I think I am doing okay with the character controller for now, and the normal vector problem was the first hiccup I’ve gotten so far. This is how I have my stuff managed. If you have any feedback for that, feel free to reply! I’ll look around for other assets to help me out in organizing things in better ways too.