- Home /
Whats the best way to move a 2d rigidbody?
My 2d character has a rigidbody attached to it. What is the best way to move it? Should I modify the transform, use AddForce, or set a velocity? Answers appreciated.
Answer by YoungDeveloper · Jan 19, 2014 at 07:19 PM
Changing transform would be the most easiest and best way in my opinion. And instead of gravity you could shoot ray down and fake the falling and jumping effect, at least it would be more optimized solution.
I forgot to mention my character moves in 8 directions like in Legend of Zelda. There is no gravity in my game. Wouldn't modifying the transform cause errors in the collision? Like hitting a wall?
There should be a collider around walls, houses and other objects. On mouse click, cast a ray, and move player only if hit.collider.gameObject.tag == "Terrain". Or are you moving your character with keyboard arrows only ?
You would still need some kind or barriers, which wouldn't not let player go past certain area. But it really depends on how your game will be made.
If you don't $$anonymous$$d player "sliding" against the wall if he hits a collider by you continue walking then simple colliders would be the easiest solution.
But if the game will be "cubed" environment only, like most classic 2 or 2.5D games, then raycast to each direction (4) could be a simple but effective solution.
Answer by Vardan Meliksetyan · Jan 19, 2014 at 07:36 PM
For pool game I use AddForce() function
http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html
I used AddForceAtPosition()
http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForceAtPosition.html
it work normal , it was 3D game , I use this in 2D it work good!
Your answer
Follow this Question
Related Questions
Enemy Movement Help 3 Answers
Top down 2d movement without being affected by rotation 2 Answers
Move camera and player OR move objects? 1 Answer
Help with topdown movement in Unity2d? 1 Answer
How do I detect when my player is moving in unity 2d? 1 Answer