• 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
1
Question by SepM · Aug 06, 2015 at 06:20 PM · movementrigidbodymovepositioncanceldirections

rigidbody.MovePosition in More than One Direction

I want to make smooth movements for my characters that have rigidbodies. I tried rigidbody.AddForce, but it's not the type of movement I want. It's drags too much, if you know what I mean. So I decided to check out rigidbody.MovePosition. It's exactly what I'm looking for, but I can only do one direction or the other doesn't work. Here's what I have:

 //Side-to-Side Movement
     rigidbody.MovePosition(transform.position + Camera.main.transform.right*mh*Time.deltaTime*7);
 //Forward-Backward Movement
     rigidbody.MovePosition(transform.position + Camera.main.transform.forward*mv*Time.deltaTime*7);

mh and mv are horizontal and vertical variables that correspond with controller inputs (left & right is mh, up & down is mv). The problem is that horizontal code (which comes first) is being cancelled out by the vertical (last). If I were to switch the order, horizontal would cancel out vertical. How would I make it so that both directions work?

Thanks in advance.

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

Answer by zach-r-d · Aug 06, 2015 at 07:45 PM

Try adding the forward and right components at the same time and doing a single MovePosition. Also a quick tip: when using rigidbodies, using Rigidbody.position rather than Transform.position will give better results since transform positions are only updated at the end of the physics step.

 // Forward, backward, and sideways movement
 Vector3 hMove = Camera.main.transform.right*mh*Time.deltaTime*7;
 Vector3 vMove = Camera.main.transform.forward*mv*Time.deltaTime*7;
 rigidbody.MovePosition(rigidbody.position + hMove + vMove);


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 tbriz · May 19, 2020 at 02:56 AM 0
Share

This was perfect for adding a custom jump i needed my character to perform using rigidbody.$$anonymous$$ovePosition - was able to add vertical and horizontal movement at the same time after adjusting this a bit for 2d (using Vector2)... You're awesome!

avatar image
0

Answer by Garross · Aug 06, 2015 at 06:54 PM

haven't used move position my self , however I do see that you are using "transform.forward" which is a big no no for 2D as it moves u along the z axis(its for 3D) , use "transform.up" instead

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

What is the best way to move the player with physics without using MovePosition? 0 Answers

My Character Moves through walls 3 Answers

Calling Rigidbody.MovePosition, no movement at all 4 Answers

Rigidbody moving and jerks 1 Answer

Rigidbody.MovePosition not behaving as expected (possible bug?) 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