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 /
avatar image
0
Question by sharktornado1 · Jan 16, 2020 at 04:47 PM · rigidbodyscripting beginnermovetowardsrigidbody.moveposition

how to use rigidbody.moveposition to go to an exact location

If i have a vector 3 of a point where i want the character to smoothly move to, how do i use rigidbody.moveposition to achieve this? I tried using the movetowards function but it doesnt work as well with rigidbody physics and jumping + collision. Can you also tell me whether i should code rigid body related things in fixed update or update.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by AltaNox · Jan 16, 2020 at 06:16 PM

There are multiple ways to make something move "smoothly" towards a point. If you have time constraints then a Lerp combined with an easing function would work well. If you don't have time constraints you could try using the Vector3.SmoothDamp function. Something like this

 body.MovePosition(Vector3.SmoothDamp(body.position,targetPoint,ref velocity,smoothTime);

And yes, you should be doing this in FixedUpdate

Comment
Add comment · Show 3 · 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 lgarczyn · Jan 17, 2020 at 01:24 AM 0
Share

This will not work with a rigidbody, unless you discard the position and only use the velocity. Even then, it's not supposed to be used for this.

avatar image AltaNox lgarczyn · Jan 17, 2020 at 08:43 PM 0
Share

It's good etiquette to explain with detail before down voting someone. I don't get what you mean by saying unless you discard the position and only use velocity. I have used this code in the past and it works as intended. $$anonymous$$aybe you what you don't get is the intention here. Also why is it not supposed to be used for this ?

avatar image lgarczyn AltaNox · Jan 18, 2020 at 06:23 PM 0
Share

I'm sorry, my comment was inadequate, and the downvote a mistake, based on a misunderstanding of your code. I unfortunately cannot remove it now.


Since smoothdamp returns a position, if you use it you have to set a rigidbody's position. If it's kinematic, that's alright, you can use $$anonymous$$ovePosition as the commentor asked, however he appears to be using a dynamic rigidbody.


If it is dynamic, you shouldn't use $$anonymous$$ovePosition, and only set the velocity. You can use the velocity variable of smoothdamp to input and modify a rigidbody's velocity, which works. But you should not set the position directly.

avatar image
0

Answer by lgarczyn · Jan 17, 2020 at 01:23 AM

The previous answer works for a kinematic rigidbody, here's the solution for a dynamic rigidbody, which will not pass through walls.

   // Get the delta position  
   Vector3 dir = target - rb.position;
   // Get the velocity required to reach the target in the next frame
   dir /= Time.fixedDeltaTime;
   // Clamp that to the max speed
   dir = Vector3.ClampMagnitude(dir, speed);
   // Apply that to the rigidbody
   rb.velocity = dir;


with rb being a script variable containing the your objects rigidbody, and target being the world position of your target.

If it is not smooth enough, you can try:

 Vector3 velocity = rb.velocity;
 Vector3.SmoothDamp(
     rb.position,
     target,
     ref velocity,
     timeToReachTarget,
     maxSpeed,
     Time.fixedDeltaTime);
 rb.velocity = velocity;

Comment
Add comment · Show 3 · 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 sharktornado1 · Jan 17, 2020 at 12:59 PM 0
Share

This works however it is very slow even if i increase the speed a lot

avatar image lgarczyn sharktornado1 · Jan 17, 2020 at 08:22 PM 0
Share

Small mistake on my part, try the updated code

avatar image lgarczyn lgarczyn · Jan 18, 2020 at 06:25 PM 0
Share

Also remember to use FixedUpdate for the code, and you might want to enable interpolation. If everyone works, please mark the question as solved.

avatar image
0

Answer by JasonBennett · Jan 17, 2020 at 08:45 PM

Hi @sharktornado1,

First of all, put it in FixedUpdate. (Source: https://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html) Secondly, it has to be a Kinetic Rigidbody.

If the Rigidbody has isKinematic set to false, it works like transform.position=newPosition and teleports the object to the new position (rather than performing a smooth transition).



So you need to have a Kinetic Rigidbody. Then you simply need to figure out the amount you want to move per frame.

 float moveAmount = .25f;
 Vector3 newPosition = transform.position + (new Vector3(1,0,0) * moveAmount);
 RB.MovePosition(newPosition);



The previous moves a Rigidbody to the right at the speed of moveAmount, which you can change as needed.

Collisions for Kinematic Rigidbody are tricky. They register on Dynamic Rigidbodies, but not on other Kinematic ones (as least in terms of their physics.) Let me know if this works and if you need more information.

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

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

170 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

Related Questions

What's the best move/rotate method to simulate a herd of animals moving around? 2 Answers

transform.forward not always forward? 2 Answers

How do i add velocity in direction depending on WASD keys pressed 1 Answer

How good is to put Move.Towards on Fixed Update? -1 Answers

Is it ok to use transform.Rotate with a rigidbody that will use force also 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