Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 LeftyTwoGuns · Aug 24, 2021 at 01:26 AM · rigidbody2dphysics2dvector2movetowardsmoveposition

Am I using MoveTowards and MovePosition wrong?

 Vector2 inputDirection =  input.normalized * 5f;
 Vector2 moveDirection = rb.position + inputDirection;
 Vector2 move = Vector2.MoveTowards(rb.position, moveDirection, 10f * Time.deltaTime);
 rb.MovePosition(move);

My intention is to move a game object a specific amount of Unity units at a specific speed, in the direction of the 8-way input movement on a controller or keyboard.

With this code, I thought I was taking the movement input and adding 5 Unity units onto the direction and then moving the game object's rigidbody2D to that point with a speed of 10 (or whatever I want).

But, adding a distance onto the input direction doesn't do anything. Only changing the speed in MoveTowards changes the distance that the game object moves. What am I misunderstanding about how these two functions work, or interact with each other?

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
1
Best Answer

Answer by xxmariofer · Aug 24, 2021 at 07:19 AM

I would need a little bit of context, is that code in the update? but yes, you are missunderstanding how movetowards. let me explain:

the second parameter from MoveTowards is not a direction, is a position, what movetowards is doing is moving from 1 position (rb.position) in the direction towards the second position (moveDirection) by x amount (10 * Time.deltaTime).

what you should be doing if I understood you, is this:

 Vector2 targetPosition;
 void UpdateTheTargetPosition()
 {
   Vector2 inputDirection =  input.normalized * 5f;
   targetPosition = rb.position + inputDirection;
 }

 void FixedUpdate()
 {
   Vector2 move = Vector2.MoveTowards(rb.position, targetPosition, speed);//change speed for whatever float value, the higher the faster the "step" towards targetPosition
   rb.MovePosition(move);
 }
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 Bunny83 · Aug 24, 2021 at 01:00 PM 0
Share

Note that he actually passes a position to MoveTowards, however he has named the variable "moveDirection" which of course is completely misleading.


However you are right about seperating the setting of the target position from the actual movement. If you set / update the target position every frame based on the current position, we would just continue to move into the input direction.

avatar image LeftyTwoGuns · Aug 26, 2021 at 02:36 AM 0
Share

This appears to have fixed it. The entirety of this script was all being executed in FixedUpdate and I separated them into different functions as advised here and its working as intended now. Thanks for your help

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

131 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

Related Questions

How do I Control An Objects Movement Only On the X Axis and Have a RigidBody Control Movement on the Y axis? 1 Answer

Rb2D.MovePosition not working - weird error... 1 Answer

Rigidbody2d.MovePosition on Update vs FixedUpdate 1 Answer

Put an offset distance to gameobject that follows player 2 Answers

rigidbody2D.MovePosition() Can't Found 2 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