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 /
  • Help Room /
avatar image
0
Question by LinkUpGames · Sep 17, 2017 at 09:08 PM · c#vector3charactercontroller

Normalized Vector3 Movement Really Slow?

Hi there, so I'm attempting to correct the issue with the provided CharacterController.Move script in the API that causes the controller to move at twice the rate of speed when moving diagonally. I've been cracking at it with my Google-Fu for hours to no avail. It seems the solutions that are working for others work for me as well but come with the added pain of being unable to move faster than a crawl. Here's the current code I'm working with.

  CharacterController controller = GetComponent<CharacterController>();
         if (!controller.isGrounded)
         {
             moveDirection.x = Input.GetAxis("Horizontal") * airSpeed;
             moveDirection.z = Input.GetAxis("Vertical") * airSpeed;
         }
         // If grounded, zero out y component and allow jumping
         else
         {
             moveDirection.x = Input.GetAxis("Horizontal");
             moveDirection.z = Input.GetAxis("Vertical");
             Vector3 newMoveDirection = new Vector3(moveDirection.x, 0, moveDirection.z);
             newMoveDirection *= groundSpeed;
             if (newMoveDirection.magnitude > 1.0f)
             {
                 newMoveDirection = newMoveDirection.normalized;
             }
             if (Input.GetButton("Jump"))
             {
                 moveDirection.y = jumpSpeed;
             }
         }
         moveDirection = transform.TransformDirection(moveDirection);
         moveDirection.y -= gravity * Time.deltaTime;
         controller.Move(moveDirection * Time.deltaTime);


It may be a bit of a mess due to my persistent fiddling trying to find a solution. At this stage the movement is normalized and you move at the same speed in every direction. It however is extremely slow and adjusting my groundSpeed variable does absolutely nothing. I feel I need an alternative solution to this issue as for whatever reason this hasn't been working. Any ideas?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by NoseKills · Sep 17, 2017 at 09:55 PM

With a quick glance, your only mistake is that you multiply before normalizing.

 Vector3 newMoveDirection = new Vector3(moveDirection.x, 0, moveDirection.z);
 newMoveDirection *= groundSpeed;

Let's say you steer full right, newMoveDirection is (1,0). If movementSpeed is 100, it becomes (100,0). The magnitude (length of the vector) is now 100 so it gets normalized (maintains direction but length is made 1) so it becomes (1,0) again.

What you (probably) want to do is normalize it if it's magnitude is over 1 and then multiply with groundspeed.
That makes it so the vector is at most 1 unit in length before the multiplication and, limiting the final length of it to groundSpeed

Comment
Add comment · Show 3 · 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 LinkUpGames · Sep 17, 2017 at 11:15 PM 0
Share

That's one of the methods I have already attempted as moving the multiplication to any other location results in the same issue. What I gather you are suggesting is:

 moveDirection.x = Input.GetAxis("Horizontal");
             moveDirection.z = Input.GetAxis("Vertical");
             Vector3 new$$anonymous$$oveDirection = new Vector3(moveDirection.x, 0, moveDirection.z);
             if (new$$anonymous$$oveDirection.magnitude > 1.0f)
             {
                 new$$anonymous$$oveDirection = new$$anonymous$$oveDirection.normalized;
             }
             new$$anonymous$$oveDirection *= groundSpeed;


Which also results in a dead stop in momentum. Putting it inside the if statement before normalization leads to slow movement. After normalization = doubled speed while running diagonally.

avatar image NoseKills LinkUpGames · Sep 17, 2017 at 11:47 PM 0
Share

Now that i read this with a bit more thought, the code you posted doesn't show you using new$$anonymous$$oveDirections value for anything, you just calculate a value for it, which makes me a bit confused if its value is even causing the problem you are having.

Any non-zero vector can be normalized, making its length exactly 1 no matter if it is diagonal or not so if you use normalized vector (multiplied with something or not) for movement, it will definitely be the same length regardless of direction.

If you multiply first and normalize second, the length of the vector will be 1 regardless of if you multiplied by 2 or 2 million, so that's definitely not the way to go and that's why the character is moving slow.

I haven't used CharacterControllers enough to answer the momentum bit :/

avatar image LinkUpGames NoseKills · Sep 18, 2017 at 12:08 AM 0
Share

I think I just have to live with the character moving quickly when moving diagonally as there doesn't seem to be a legitimate solution.

avatar image
0

Answer by ransomink · Sep 18, 2017 at 02:29 AM

Dunno why you'd just live with an error or miscalculation. You create Vector3 newMoveDirection and normalize it but you move the player using controller.Move(moveDirection * Time.deltaTime);

Problem 1: You don't use the Vector3 that you normalized.

Problem 2: You normalize the vector after applying speed (do it at the same time or after).

I'm not sure if the code you have after it will affect the movement because it happens after the normalization, so I placed it at the end. You can see if this works or not.

 float h = Input.GetAxis( "Horizontal" );
 float v = Input.GetAxis( "Vertical" );
 
 if ( controller.IsGrounded )
 {
     if ( Input.GetButton( "Jump" ) )
     {
         moveDirection.Set( h, jumpSpeed, v );
     }
     else
     {
         moveDirection.Set( h, 0, v );
     }
 
     moveDirection.y -= gravity * Time.deltaTime;
     moveDirection    = transform.TransformDirection( moveDirection );
     moveDirection    = moveDirection.normalized * ( groundSpeed * Time.deltaTime );
     controller.Move( moveDirection );
 }

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 LinkUpGames · Sep 20, 2017 at 11:25 PM 0
Share

Not sure why my first reply didn't post but anyways. That works for normalizing the movement and allowing for a normal speed. However, when attempting to jump the character controller doesn't go anywhere. It jumps less than an inch and immediately hits the ground. It may be due to noramlizing the y axis? I'm not sure.

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

401 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 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 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 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

Grappling hook physics script error 0 Answers

Normalized Vector3 doesn't allow for jumping? 0 Answers

fps script not working 1 Answer

Vector3 and transform.rotation? 2 Answers

Moving GameObject a specific distance in the Z direction and back again - regardless of rotation 1 Answer


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