Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 $$anonymous$$ · Jul 29, 2019 at 01:46 PM · movementmovement scriptaccelerationmagnitudenormalize

Problems with diagonal movement.

Hey, i know this is a common question, but the answers i have found, can't be aplied to my problem because of an anoying extra layer.

So the problem in question is that my player moves faster while moving diagonally, due to the velocity vector getting doubled.

The usual fix for this would be to either multiply it by .7 when moving diagonally, or normalize the value. But this isn't enough for my problem, because i have smoothed out my movement with an acceleration and decceleration. If i normalize the value, it will only work when at full speed, and changing speeds will not make it accelerate / deccelerate to the new speed. I can't seem to find a way to do this, here is the code i have been using so far:

         Vector3 targetDirection = Vector3.zero;
 
         targetDirection.x = Input.GetAxisRaw("Horizontal");
         targetDirection.z = Input.GetAxisRaw("Vertical");
 
         if (targetDirection.x != 0f)
         {
             velocity.x = Mathf.MoveTowards(velocity.x, speedCap * targetDirection.x, accel * Time.deltaTime);
         }
         else
         {
             velocity.x = Mathf.MoveTowards(velocity.x, 0f, deccel * Time.deltaTime);
         }
 
         if (targetDirection.z != 0f)
         {
             velocity.z = Mathf.MoveTowards(velocity.z, speedCap * targetDirection.z, accel * Time.deltaTime);
         }
         else
         {
             velocity.z = Mathf.MoveTowards(velocity.z, 0f, deccel * Time.deltaTime);
         }
 
         transform.position += velocity * 25 * Time.deltaTime;

So, i think i need to somehow clamp my velocity, to a magnitude, that is it's current velocity? It's pretty hard for me to understand it so if someone could help out it would be awesome.

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 $$anonymous$$ · Jul 29, 2019 at 03:31 PM

Damn i feel so stupid, i just fixed it myself and the solution was litteraly on my nose.

If anyones facing the same problem, then first of all, i pity you for being as dumb as me sorry. Here it is:

Instead of normalizing the velocity, normalize the damn input :/

Heres the working code

          Vector3 targetDirection = Vector3.zero;
  
          targetDirection.x = Input.GetAxisRaw("Horizontal");
          targetDirection.z = Input.GetAxisRaw("Vertical");
 
          targetDirection.Normalize(); //This is the line.
  
          if (targetDirection.x != 0f)
          {
              velocity.x = Mathf.MoveTowards(velocity.x, speedCap * targetDirection.x, accel * Time.deltaTime);
          }
          else
          {
              velocity.x = Mathf.MoveTowards(velocity.x, 0f, deccel * Time.deltaTime);
          }
  
          if (targetDirection.z != 0f)
          {
              velocity.z = Mathf.MoveTowards(velocity.z, speedCap * targetDirection.z, accel * Time.deltaTime);
          }
          else
          {
              velocity.z = Mathf.MoveTowards(velocity.z, 0f, deccel * Time.deltaTime);
          }
  
          transform.position += velocity * 25 * Time.deltaTime;
 

One line that's all.

Comment
Add comment · 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

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

164 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

Related Questions

Does anyone know what i am doing wrong? 0 Answers

Moving tram along rail. 0 Answers

Velocity is not matching the Vector3 it is assigned 1 Answer

I have a movement script, but how can i make it so i can move half the speed while in the air. 0 Answers

Character Transform shifting after colliding with objects 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