• 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 IsaiahKelly · Apr 13, 2018 at 02:16 AM · movementphysicstranslatekinematicworld coordinates

Using Rigidbody.MovePosition With Local Position and Lerp

I simply want to change this function to use Rigidbody.MovePosition instead of local transform position, but so far I can't figure out how to convert the local positions to something that works with that method. I've read the existing discussions on the topic, but none of them seem to apply to my particular situation. I know this must be pretty simple to do, right?

         // target here is a local offset position
         private IEnumerator MoveToPosition (Vector3 target)
         {
             float t = 0;
             Vector3 start = transform.localPosition;
 
             while (t <= 1)
             {
                 t += Time.fixedDeltaTime / m_Speed;
                 transform.localPosition = Vector3.Lerp (start, target, t);
                 yield return null;
             }
         }
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

2 Replies

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

Answer by IsaiahKelly · Apr 16, 2018 at 12:10 AM

Okay, I was really overthinking this. I just needed to get (world) transform.position instead of transform.localPosition and feed that into the Rigidbody.MovePosition. Just make sure target value is also in world coordinates.

         private IEnumerator MoveToPosition (Vector3 target)
         {
             float t = 0;
             Vector3 start = transform.position;
 
             while (t <= 1)
             {
                 t += Time.fixedDeltaTime / m_Speed;
                 Rigidbody.MovePosition (Vector3.Lerp (start, target, t));
 
                 yield return null;
             }
         }


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
avatar image
0

Answer by waikit97 · Apr 13, 2018 at 03:09 AM

 private float m_Speed = 10;
 public GameObject targetObj; //the target object
 public Rigidbody m_rb; //your moving object's RigidBody
 
     private IEnumerator MoveToPosition (GameObject target)
     {
         float t = 0;
         while (t <= 1)
         {
             t += Time.fixedDeltaTime / m_Speed;
             Vector3 start = transform.localPosition; //assign inside the while loop for updating your moving object's local position, since it moves
             m_rb.MovePosition(start + target.transform.localPosition * Time.deltaTime);
             yield return null;
         }
     }
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 IsaiahKelly · Apr 16, 2018 at 12:13 AM 0
Share

Thanks, but this doesn't seem to give me the desired effect and isn't using Vector3.Lerp It also didn't seem to work correctly when I tried it in my own code.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

161 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

Related Questions

Is mixing movement types a bad thing to do? (Force and Translate) 1 Answer

Rigid body child Player movement 0 Answers

Simple Movement Game: Physics vs Manual Collision Detection? 2 Answers

Kinematics: Trying to get an object to land at a known point 1 Answer

Infinite Moving/Rotating Question 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges