Howdy Unity,
Big semester break coming up due to the Olympics, so I decided to get back to Unity3d-ing. Building a simple FPS, got most of the basic stuff down but I seem to have encountered a problem…when you move diagonally you actually move faster than just horizontally or vertically. This is because you have two Vectors that are equal (lets say they’re equal to one) so on a triangle, the edges would be 1, 1 and root 2 (1.41)…meaning the character moves 1 and a half times as fast when he’s moving diagonally.
This seems to be a pretty broad problem and I’m guess someone else must have enountered it, but it’s actually fairly tough to fix. I want my character to have a sense of ‘weight’, so I have a small amount of time required to accelerate and decellerate…he’s what I had before noticing this problem.
var speed = 6.0;
var acceleration = 1.0;
var decceleration = 0.9;
var gravity = 20.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
private var currentSpeedX = 0.0;
private var currentSpeedZ = 0.0;
function FixedUpdate () {
if (grounded) {
currentSpeedX += Input.GetAxis("Horizontal") * acceleration;
currentSpeedZ += Input.GetAxis("Vertical") * acceleration;
if (Input.GetAxis("Vertical") == 0)
{
currentSpeedZ = currentSpeedZ * decceleration;
}
if (Input.GetAxis("Horizontal") == 0)
{
currentSpeedX = currentSpeedX *decceleration;
}
currentSpeedZ = Mathf.Clamp(currentSpeedZ, -speed, speed);
currentSpeedX = Mathf.Clamp(currentSpeedX, -speed, speed);
moveDirection = new Vector3(currentSpeedX, 0, currentSpeedZ);
moveDirection = transform.TransformDirection(moveDirection);
}
moveDirection.y -= gravity * Time.deltaTime;
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags CollisionFlags.CollidedBelow) != 0;
}
It’s pretty similar to the FPS mover from the 3 part tutorial.
Anyways, I can’t figure out for the life of me how to make it work. I want the character to be able to move equal speeds in all directions, but the problem arises when I’m trying to make decceleration. I tried just using one speed value (rather than X and Z speeds) and just point the vector in the right direction, but it doesn’t quite work…
For example: if you’re moving up-right and maximum speed, then let go of the up key and press down, it should only slow down your upwards movement…however, if you’re only using one speed variable, this isn’t possible.
Anyways, long story short is that this was bugging me, and someone must have had this problem before (although I vaguely remember in GoldenEye 64 moving diagonally was faster than horz or vert…)