- Home /
Problems with the movement of custom character controller
Hello friends, first of all I appreciate the help you can give me, I'm doing a character controller, based on the rotation of the camera, I have 2 problems, the first is that when I walk diagonally the character walks faster, and the second is that when I walk diagonally the character can not jump, I attach my code, the function that I use is executed in update, thank you very much
void Move_Pc()
{
if (transform.position.y < -50.0f) transform.position = new Vector3(0, 2, 0);
//
float moveAxis_mag = 0.0f;
Vector3 moveAxis = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
moveAxis_mag = Mathf.Round(moveAxis.normalized.magnitude);
Quaternion screenMovementSpace = Quaternion.Euler(0f, Camera.main.GetComponent<Transform>().eulerAngles.y, 0f);
Vector3 screenMovementForward = screenMovementSpace * Vector3.forward;
Vector3 screenMovementRight = screenMovementSpace * Vector3.right;
movingDirection = moveAxis.x * screenMovementRight + moveAxis.z * screenMovementForward;
//
_animator.SetBool("Grounded", mCharacterController.isGrounded);
if (mCharacterController.isGrounded)
{
cuenta_saltos = 0;
movingForce.Set(0f, 0f, 0f);
movingForce = transform.TransformDirection(movingDirection);
speed = speed_max;
}
if (Input.GetButtonDown("Jump"))
{
if (cuenta_saltos < 2)
{
if (cuenta_saltos == 0)
{
movingForce.y += jumpSpeed;
speed = speed_max * 2.0f;
_animator.Play("JUMP");
} else
{
speed = speed_max * 2.5f;
movingForce.y = jumpSpeed * 1.2f;
_animator.Play("JUMP_DOUBLE");
}
cuenta_saltos++;
}
}
DoGolpes();
movingForce.x = movingDirection.x * speed * moveAxis_mag;
movingForce.z = movingDirection.z * speed * moveAxis_mag;
movingForce.y -= gravity * Time.deltaTime;
if (movingDirection != Vector3.zero)
{
transform.rotation = Quaternion.LookRotation(movingDirection);
_animator.SetFloat("Walking", moveAxis_mag);
} else
{
_animator.SetFloat("Walking", 0.0f);
}
mCharacterController.Move(movingForce * Time.deltaTime);
//
}
Answer by Ermiq · Aug 16, 2018 at 04:54 AM
Hm... The code looks correct and it should work. Although it's not optimal at all. For example, why do you need to round magnitude of normalized vector? Vector3.normalized
returns a vector with a magnitude equal to 1.0 and then you round this magnitude for some reason. ??... Also, you declare a lot of local variables which would be made global, because the engine calls Update() very often and every time it does it, your program has to find a place for this variables to hold them, initialaize them, provide links and all, and then set their memory places free on the end of Update and then again place them somewhere in next Update(), and then again, and again, and again... And garbage collector cleans memory, when it could rest a bit if you had made these variables global. Also, do not initialize variable with a zero/null value if you're going to assign a real value to it right in the next string. Example:
movingForce.Set (0f, 0f, 0f);
movingForce = transform.TransformDirection(movingDirection);
Why do you do that? You are going to rewrite value with a new one, so why do you rewrite value twice?
Ok. Now what about moving faster on diagonal. Imagine a square. It's left bottom angle is a zero. Character stands still. On left upper angle you have a forward vector with a length equal to edge length and it's a moving forward vector, character goes forward. The same for the right bottom angle - going to the right. And what is going on when character goes on diagonal? Yes, he goes on vector (zero -> forward) + (zero -> right)
and the length of this vector is a square root from the sum of two squared zero -> forward lengths. Not sure if spell this correctly in English.... However, that's why character goes faster on diagonal. You just need to adjust the speed in this case. Why your character can't jump? Don't know, sorry. Are you sure the controller is grounded when you trying to jump?
man thank you for your answer, for the matter of the jumps it turns out that my keyboard did not accept all those keys oppressed at the time As for the diagonal speed you gave me a great idea of how I should do it, now I'm normalizing only the axes vector and it's working beautiful and I'm going to take into account the global variables to optimize the code Once again, thank you so much hablas español yo si jeje
Your answer
Follow this Question
Related Questions
CharecterController.Move() ignores parents movement 0 Answers
Top-down player rotation using rigidbody 0 Answers
Make a Platform push a character controller? 2 Answers
2D character controller getting stuck on walls 5 Answers
The character controller stop moving when the Speed Start increase Endless runner 0 Answers