- Home /
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.
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);
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!
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
Your answer
Follow this Question
Related Questions
Calling Rigidbody.MovePosition, no movement at all 4 Answers
Rigidbody moving and jerks 1 Answer
My Character Moves through walls 3 Answers
What is the best way to move the player with physics without using MovePosition? 0 Answers
Rigidbody movement 1 Answer