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 Tronline · Dec 28, 2016 at 02:48 PM · c#movementdiagonal

Character Diagonal Movement Issue

I've made a movement script + jumping but it seems like when I move diagonally (W + D/A) or (S + D/A) my character moves twice as fast as normal. I get it's because in the script I've done:

 moveDirection *= speed;

So it basically multiplies the moveDirection by speed, but when I press 2 keys like W and D I think it kinda multiplies both of the keys or the directions so it equals to twice as much as normal. How do I fix this? Or maybe I'm just seeing things and I'm actually moving normally? Thanks in advance :P

Comment
Add comment · Show 1
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 Tronline · Dec 28, 2016 at 02:42 PM 0
Share

It's 3D btw :P

3 Replies

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

Answer by AlwaysSunny · Dec 28, 2016 at 02:47 PM

You should normalize the input vector. Like so...

 float h = Input.GetAxis("Horizontal");
 float v = Input.GetAxis("Vertical");
 Vector3 inputVector = new Vector2( h, 0, v );
 inputVector.Normalize();

Then multiply it by your desired speed and use that to move the character.
This will correct the issue.

Think of it like this: Without normalization, the input vector fits into a square. The corners of a square are further from the center than the middle of the edges are. Normalizing "chops off" the excess, so the final input vector fits into a circle, making the vector a uniform length no matter what direction it's pointing.

Best,

Comment
Add comment · Show 4 · 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 UnityCoach · Dec 28, 2016 at 06:51 PM 3
Share

Using Vector3.Normalize() will result in a Length that is always equal to one. Thus preventing subtle motion when using Joysticks or $$anonymous$$ouse for example. I'd recommend using Clamp$$anonymous$$agnitude ins$$anonymous$$d.

avatar image AlwaysSunny UnityCoach · Dec 28, 2016 at 09:59 PM 0
Share

Excellent point! Saw WASD and forgot about Clamping. :)

avatar image UnityCoach AlwaysSunny · Dec 28, 2016 at 10:15 PM 0
Share

Been there :)

Show more comments
avatar image
0

Answer by Tronline · Dec 28, 2016 at 02:57 PM

@AlwaysSunny Oh ok! I get it now, thanks!

Comment
Add comment · Show 2 · 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 AlwaysSunny · Dec 31, 2016 at 05:50 PM 0
Share

Addressing your comment about clamping here, because UA only lets you reply so many levels deep.

You should use Clamp$$anonymous$$agnitude ins$$anonymous$$d of normalizing. Replace

 moveDirection.Normalize();

with

 moveDirection = Vector3.Clamp$$anonymous$$agnitude(moveDirection, 1);
avatar image Tronline · Dec 31, 2016 at 06:29 PM 0
Share

Thanks so much :D

avatar image
0

Answer by IntergalacticSloth · May 18, 2018 at 05:52 AM

Also discussed here https://answers.unity.com/questions/639075/using-move-the-enemy-moves-faster-at-a-diagonal.html

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

10 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

Related Questions

How do I get diagonal 3d movement? 1 Answer

How to move a game object to a position after selecting it 0 Answers

How can I get the player to face the direction it is going in a Unity 2D Game?,How do I make it so that my character faces in the direction it is facing in a 2D Unity Game 0 Answers

Problems With Walking Code 1 Answer

How to disable diagonal Vector3 movement of non-player object using MoveTowards? 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