Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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$$ · Nov 16, 2014 at 09:41 PM · rigidbody2dfixedupdatethreadingmoveposition

MovePosition with Multiple Scripts

I have a GameObject. It has the following two scripts attached:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(Rigidbody2D))]
 public class MovePosition1 : MonoBehaviour
 {
     void FixedUpdate()
     {
         float newX = transform.position.x + 1;
         float newY = transform.position.y;
         Vector2 newPosition = new Vector2(newX, newY);
         rigidbody2D.MovePosition(newPosition);
     }
 }

==========================================================

 using UnityEngine;
 using System.Collections;

 [RequireComponent(typeof(Rigidbody2D))]
 public class MovePosition2 : MonoBehaviour
 {
     void FixedUpdate()
     {
         float newX = transform.position.x;
         float newY = transform.position.y + 1;
         Vector2 newPosition = new Vector2(newX, newY);
         rigidbody2D.MovePosition(newPosition);
     }
 }

But only one of the Rigidbody2D.MovePosition() calls are taking place. Why is this? How can I fix this (without combining the scripts)?

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

Answer by AdamScura · Nov 16, 2014 at 10:17 PM

Short answer is you can't.

This is from the documentation on MovePosition:

It is important to understand that the actual position change will only occur during the next physics update therefore calling this method repeatedly without waiting for the next physics update will result in the last call being used. For this reason, it is recommended that it is called during the FixedUpdate callback.

As the documentation suggests, you should have a single behaviour that calls MovePosition.

For example you could create a behaviour called Mover with a Move method that adds up the requested movement vectors every frame. Then on FixedUpdate Mover would call rigidbody2d.MovePosition once with the combined movement vector.

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 AdamScura · Nov 16, 2014 at 10:28 PM 0
Share

Here is an example:

 using UnityEngine;
 
 [RequireComponent(typeof(Rigidbody2D))]
 public class $$anonymous$$over : $$anonymous$$onoBehaviour
 {
     private Vector2 _movement;
 
     public void $$anonymous$$ove(Vector2 movement)
     {
         _movement += movement;
     }
 
     void FixedUpdate()
     {
         rigidbody2D.$$anonymous$$ovePosition((Vector2)transform.position + _movement );
         _movement = Vector2.zero;
     }
 }
 

You should set this to execute last in your script execution order.

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

26 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

Related Questions

Rigidbody2d.MovePosition on Update vs FixedUpdate 1 Answer

Issue with direction and rigibody2D 1 Answer

rigidbody2D velocity zero? 1 Answer

Moving rigidbodies with MovePosition/MoveRotation outside of FixedUpdate 3 Answers

Am I using MoveTowards and MovePosition wrong? 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