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?