Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by josewebdev · Aug 16, 2018 at 02:09 AM · charactercontrollerplatformercharacter movement

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);
         //
     }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image josewebdev · Aug 17, 2018 at 12:29 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

103 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges