Character Controller: Odd quirk when backing up

I’ve started with the PlatformCharacterController that Unity provided that combines the NormalCharacterMotor and a CharacterContoller to perform movements.

My goal is for A and D (Horizontal axis) to simply rotate the character along the Y axis via the Normal Character Motor and W and S (Vertical axis) to always move forward and backward relative to the character and not the camera as it was initially intended for. I need these to happen via the character motor rather than directly accessing the transform of the player.

Here is what I have thus far:

void Update () {

		Vector3 directionVector = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
		
		if (directionVector.magnitude>1) 
			directionVector = directionVector.normalized;
		
		directionVector = directionVector.normalized * Mathf.Pow(directionVector.magnitude, 2);		
		directionVector = transform.rotation * directionVector;
		
		Quaternion toCharacterSpace = Quaternion.FromToRotation(transform.forward * -1, transform.up);
		directionVector = toCharacterSpace * directionVector;
		directionVector = Quaternion.Inverse(transform.rotation) * directionVector;
		
		if (walkMultiplier!=1) {
			if ( (Input.GetKey("left shift") || Input.GetKey("right shift") || Input.GetButton("Sneak")) != defaultIsWalk ) {
				directionVector *= walkMultiplier;
			}
		}
		
		motor.desiredMovementDirection = directionVector;
	}

It’s almost working except when backing up. The player always backs up at a particular angle every time despite what direction the player is facing initially.

I must be close, but I can’t seem to tell the controller that when the Axis(“Vertical”) < 0, face the same direction and just move -(transform.forward)!

Could anyone help me please?

EDIT:

I’ve added what I thought made sense:

if(Input.GetAxis("Vertical") < 0) {
    directionVector =  new Vector3(-transform.forward.x,transform.position.y, -transform.forward.z);
}

Which I thought would say the directional vector is the opposite of what currently is forward…but it still does the same thing!? I really hope someone can shed some light on this.

EDIT:

So, I believe the issue is this: Normally, in a 3rd person controller, the characters direction is based of a camera (like you’d see in a typical MMO, or something of the like) however, my game features fixed cameras and thus movement needs to be totally independent of the cameras. Does anything know an algorithm for determining direction based wholly on the character itself?

Well, this caught my eye…

directionVector= transform.rotation * directionVector;

because transform.rotation is a Quaternion, directionVector is a Vector3, and you can’t multiply the two together and get anything useful. Put a Debug.Log at every step and see if you’re getting numbers that make any sense.

Also, when you first set directionVector, you are setting it to be a direction on the xy plane, which would be vertical aim; what you probably want is a direction on the xz plane.

I aren’t very good at scripting and all. If I were you, I would look up a tutorial, and then see how THEY do the controller, then fixed what you did wrong.
P.S. Are the colliders glitches? Because that can happen.

Did you try just “directionVector = -transform.forward”? Because that’s what is usually used to go backwards.

This: directionVector = new Vector3(-transform.forward.x,transform.position.y, -transform.forward.z);

Will set the directionVector to -transform.forward except the Y attribute is absolutely arbitrary so it won’t actually be usable (for this application).