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 byyswiller · Jul 14, 2016 at 07:40 AM · c#movementvelocitymathf.lerpnormalize

Smooth 2D topdown movement using rigidbody.velocity. Problems with Vector2.Normalize, Mathf.Lerp and diagonal movement.

Hello!

I've been working on a 2D topdown shooter with randomly generating maps, pickups, waves of enemies etc. Recently I started fine-tuning the player movement, but ran into some problems, since I pretty much suck at maths and physics and end up getting confused with vectors. By the way, I'm using C#. Any help would be appreciated:

Originally I was using this piece of code:

 void FixedUpdate()
 {
     GetComponent<Rigidbody2D>().velocity = new Vector2(Mathf.Lerp(0, Input.GetAxis("Horizontal") * speed, 0.8f), Mathf.Lerp(0, Input.GetAxis("Vertical") * speed, 0.8f));
 }

It felt very responsive and the player turned and stopped smoothly. However there was one problem, which I came to find was asked here several times before. Diagonal movement (i.e. pressing W and D together) is noticeably faster than only horizontal or vertical movement.

Apparently double input causes the vector be longer and multiplied by speed it becomes faster. After reading through a fair amount of questions and forum posts, I came to the conclusion that I should normalize the vector. Here's what I tried:

 void FixedUpdate()
 {
     Vector2 direction = new Vector2 (0,0);
     direction.x = Input.GetAxis ("Horizontal");
     direction.y = Input.GetAxis ("Vertical");
     direction.Normalize ();
     GetComponent<Rigidbody2D> ().velocity = direction * speed;
 }

This does indeed seem to reduce the diagonal movement speed to about the same as vertical/horizontal.

However, now the movement becomes way less smooth than my previous attempt. Starting to move, stopping, and changing to the opposite direction mid-movement all happen in a very unnatural, snappy manner. A quick press of the button results in a big "leap" for the character instead of a small nudge. I assume this happens because the second piece of code has no Math.Lerp?

My attempts to combine the two have been unsuccessful. The end result usually behaves similar to either of the two. Also, simply normalizing my first attempt resulted in the speed variable not affecting movement speed at all. Instead it got also normalized into 1, right?:

 void FixedUpdate()
 {
     GetComponent<Rigidbody2D>().velocity = new Vector2 (Mathf.Lerp(0, Input.GetAxis ("Horizontal") * speed, 0.8f), Mathf.Lerp(0, Input.GetAxis ("Vertical") * speed, 0.8f)).normalized;
 }

So in a nutshell, my questions are:

How do I move a 2D character with a rigidbody using both Vector2.Normalize() and Mathf.Lerp, eliminating double speed for diagonal movement and making acceleration slightly smoothed?

or

Am I even in the right track? Is there an easier / more optimal way to do all this than what I've been trying?

Thanks in advance, and apologies for a (partly) already answered question.

Comment
Add comment · Show 3
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 AngelDeads · Aug 05, 2016 at 12:49 AM 0
Share

Hello, sorry for bothering, i have the same problem as you do and i didn't find the solution. I've read your reddit post too but without any help. Did you find the solution on this problem? Or did you used another method?

avatar image AngelDeads AngelDeads · Aug 05, 2016 at 01:13 AM 1
Share

@AngelDeads Okay, so, i found the solution:

     GetComponent<Rigidbody2D>().velocity = Vector3.Clamp$$anonymous$$agnitude(movementDirection,1) * $$anonymous$$ovementSpeed;

You have to set the clamp magnitude to 1 and than multiply the whole resulted vector 3 with the speed. I made the mistake where I was multiplying this two inside the Clamp$$anonymous$$agnitude. I really hope you understood the solution if you still needed it.

avatar image NoseKills AngelDeads · Aug 05, 2016 at 06:09 AM 0
Share

You posted a comment as an answer and then an answer as a comment to that answer...

I'll convert

0 Replies

· Add your reply
  • Sort: 

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

194 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

Related Questions

Unity Space Shooter Tutorial Player Will Not Move 1 Answer

Controller script active on both characters. 1 Answer

How do I create a 2D movement script that will cause prefabs to drift around the screen? 0 Answers

[Android] Character movement script help 1 Answer

Failed to create agent because it is not close enough to the NavMesh 4 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