So I’m following burgzergarcade’s hack and slash rpg tutorial, and for moving the character we used this code:
(These are the variables)
public CharacterController controller; //This is assigned to the controller elsewhere in the script
private Vector3 moveDirection = Vector3.zero;
private CollisionFlags collisionFlags;
Here is the code which gets it to move
if (controller.isGrounded) {
moveDirection = new Vector3 (0, 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection).normalized;
moveDirection *= moveSpeed * Time.deltaTime;
}
collisionFlags = controller.Move(moveDirection);
}
I didn’t really understand the brief description given in the tutorial, and I looked up all the functions (collisionFlags and TransformDirection)in the unity script Reference but I still don’t really understand how this works. (I understand IsGrounded and Normalized, just not the rest) The code works, but I just don’t understand why it works.
Prior to the tutorial I just used this to go forward, and the opposite to move backwards:
if (Input.GetAxis("Vertical") > 0) {
controller.Move(transform.forward * moveSpeed * Time.deltatime);
}
The results seem to be the same, So would someone be able to explain how this (new) code gets my character to move, and the difference between it and my old code?