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 /
  • Help Room /
avatar image
0
Question by WillB · Jan 06, 2013 at 01:02 AM · rigidbodycharactercontrollermoveposition

How to: rigidbody.MovePosition to player's position

Scripting noob here. I have a game populated with large amounts of enemies, so I decided to switch them from CharacterControllers to kinematic rigidbodies to optimize, since I've read on here that there's much less overhead when doing so.

What I'm trying to do is very simple. I need to have my AI enemies move towards the "target" GameObject, which is assigned in the editor. According to the script reference, this should work:

 rigidbody.MovePosition (target.position);

However, it makes my enemy teleport to my player object, not actually travel there like how characterController.Move does.

How can I achieve the same effect as characterController.Move() for a rigidbody?

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

Answer by Lovrenc · Jan 06, 2013 at 01:25 AM

 public float movementSpeed= 5f; //for instance
 
     void Update() {
     
         Vector3 direction = (target.transform.position - transform.position).normalized;
           rigidbody.MovePosition(transform.position + direction * movementSpeed * Time.deltaTime);
     
     }


EDIT: fixed a bug.

Comment
Add comment · Show 7 · 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 WillB · Jan 06, 2013 at 02:20 AM 0
Share

Thanks for the reply. This does not work, unfortunately.

It just zeroes out the position of the GameObject, rgardless of where it is placed in the editor.

I tried a very similar line that worked really well with CharacterController, but for some reason it doesn't do anything with a rigidbody.

avatar image Lovrenc · Jan 06, 2013 at 02:49 AM 0
Share

Fixed it. This is essentially translate it seems.

avatar image WillB · Jan 06, 2013 at 06:21 AM 0
Share

Ah that works wonderfully. The GameObject is jittery though, but I've seen that issue mentioned in multiple other threads; I'll search for a fix.

Thanks for all of your help!

avatar image TheSaviour WillB · Jul 16, 2018 at 02:59 PM 0
Share

Hello. I know this is an old thread but have you found a solution for the jittery-ness? I used the same code and encountered the exact same problem.

avatar image ResourceV · Feb 02, 2017 at 04:25 AM 2
Share

shouldn't you be using FixedUpdate () ins$$anonymous$$d?

avatar image Understainding · Jul 23, 2018 at 07:06 PM 0
Share

Remember that RigidBody.is$$anonymous$$inematic should be set to true to allow for smooth transition as explained in the Rigidbody.$$anonymous$$ovePosition() docummentation.

Show more comments
avatar image
1

Answer by zzeeshann · May 12, 2016 at 02:37 PM

rigidbody is depreciated use GetComponent()

Vector3 direction = (target.transform.position - transform.position).normalized; GetComponent().MovePosition(transform.position + direction movementSpeed Time.deltaTime);

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

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

15 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

Related Questions

Control speed of a rigidbody that move to a fixed distance 2 Answers

rigidbody.MovePosition to rigidbody.velocity 0 Answers

Moving RigidBody Smoothly 0 Answers

Moving a cube with RigidBody. MovePostition, it randomly stops moving 0 Answers

Gravity issue on MouvementController using RigidBody when going down a slope 0 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