- Home /
Diagonal movement speed difference with normalized vector
Hi, I realize this question pops up a lot but i can't figure out whats wrong here. I'm making my own movement controller script to make movement relative to an orbiting camera on a third person setup. I've fit most of it together from bits and pieces of scripts that i've looked up and it works pretty ok.
I'm using a normalized vector for direction of movement along the x-z plane, multiplied by speed, and adding vertical speed afterwards:
Vector3 movement = moveDirection * speed + new Vector3(0, verticalSpeed, 0);
controller.Move(movement * Time.deltaTime);
I made a little debug speedometer to make sure, which confirmed that indeed diagonal speed is sqrt(2)*regular speed. Debugging I also confirmed that moveDirection is normalized. Am I misinterpreting how CharacterController.Move works?
After coming to a dead end there I tried making my own character controler and started on a basic movement controller using Transform.Translate. I had no problems there, speed remained constant in every direction. I tried adding a character controller and using Move with the same input, and it still woked.
I really don't understand whats going on then. In one case speed varies, in the other it does not and both have virtually the same input. There must be something Im missing here.
Here are the scripts:
The one that works
public class BasicMovement : MonoBehaviour {
public float speed = 6f;
public bool useCharController = false;
void Update () {
CharacterController controller = GetComponent<CharacterController>();
Vector3 translation = new Vector3(Input.GetAxisRaw("Horizontal"),
0, Input.GetAxisRaw("Vertical"));
translation.Normalize();
translation = translation * speed * Time.deltaTime;
if (useCharController){
controller.Move(translation);
}else{
transform.Translate(translation);
}
}
}
The one that doesn't
public class MovementController : MonoBehaviour {
public float speed = 6.0F;
public float gravity = 20.0F;
public float jumpSpeed = 8.0F;
public int maxJumps = 2;
public float jumpCooldown = 0.5f;
public Transform cameraTransform;
public Transform playerModelTransform;
public float modelTurnSpeed = 2.0f;
private Vector3 moveDirection = Vector3.zero;
private float verticalSpeed = 0f;
private int jumpCount = 0;
private float lastJumpTime = -1f;
void Update () {
CharacterController controller = GetComponent<CharacterController>();
bool canJump = jumpCount < maxJumps;
// Forward vector relative to the camera along the x-z plane
Vector3 forward = cameraTransform.TransformDirection(Vector3.forward);
forward.y = 0;
forward = forward.normalized;
// Right vector relative to the camera
// Always orthogonal to the forward vector
Vector3 right = new Vector3(forward.z, 0, -forward.x);
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
bool isMoving = Mathf.Abs(h) > 0.02f || Mathf.Abs(v) > 0.02f;
// x-z plane movement
if (controller.isGrounded) {
moveDirection = h*right + v*forward;
moveDirection = transform.TransformDirection(moveDirection)
.normalized;
// turn the character model in the direction of movement
if (isMoving) {
Vector3 lookForward = Quaternion.LookRotation(forward).eulerAngles;
lookForward.x = lookForward.z = 0;
playerModelTransform.rotation = Quaternion
.Slerp(playerModelTransform.rotation,
Quaternion.Euler(lookForward), modelTurnSpeed * Time.deltaTime);
}
}
// y axis movement
if (canJump && Input.GetButton("Jump")) {
if (lastJumpTime + jumpCooldown < Time.time) {
lastJumpTime = Time.time;
verticalSpeed = jumpSpeed;
jumpCount++;
}
}
if (controller.isGrounded && !Input.GetButton("Jump")){
jumpCount = 0;
verticalSpeed = 0;
} else {
verticalSpeed -= gravity * Time.deltaTime;
}
Vector3 movement = moveDirection * speed + new Vector3(0, verticalSpeed, 0);
controller.Move(movement * Time.deltaTime);
}
}
The debug speedometer
public class DebugSpeedometer : MonoBehaviour {
public float timeStep = 1f;
public float currentSpeed = 0f;
private float lastMeasurement = -1f;
private Vector3 lastMeasurementPosition;
void Start () {
lastMeasurementPosition = transform.position;
}
void Update () {
if (lastMeasurement + timeStep < Time.time) {
lastMeasurement = Time.time;
currentSpeed = Vector3.Distance(transform.position, lastMeasurementPosition) / timeStep;
lastMeasurementPosition = transform.position;
}
}
}
Your answer
Follow this Question
Related Questions
Increased strafe speed\ 1 Answer
Increasing the speed of an object when the scale is decreased and vice versa 0 Answers
Pushing rigidbodies around 1 Answer
rotation speed cap in 2D 0 Answers
Rotation used as movement 4 Answers