How do I stop the player from shaking in Unity2D?
Behaviour I have: http://i.imgur.com/1zTHbPz.gif
Behaviour I want: http://i.imgur.com/BKuURHx.gif
As you can see in the GIF, the heart (player) shakes when the player wants to move, for example, down but there's a collider in front of it. If my words weren't clear enough, please check the images. The player use "rigidbody2D" and a BoxCollider2D to keep itself from moving through walls, which it does but with the shaky effect.
Here's the code I am using to move the character:
function Update () {
this.gameObject.transform.position += Vector3.up * speed*Input.GetAxisRaw("Vertical");
this.gameObject.transform.position += Vector3.right * speed*Input.GetAxisRaw("Horizontal");
}
I have no idea how to fix it, please help me.
Thanks.
Edit: Just checked, it does use rigidbody2D. oops.
Answer by FlutterCZ · Feb 23, 2016 at 02:29 PM
Solved by using a workaround: Using Linecast to detect collision and then allowing movement in specific directions only.
Answer by hexagonius · Feb 22, 2016 at 10:21 PM
you said it yourself, the player does not have a rigidbody. that's why that happens. give it a rigidbody ends move with addforce, or by changing velocity in FixedUpdate
Thank you for the response. And also, I would like to apologise for my statement that it doesn't use Rigidbody2D, it does.
I've tried using AddForce() and FixedUpdate(), but the movement is icey (doesn't have constant velocity, speeds up when holding the button longer). As usuals, I will upload a picture/GIF explaining my problem.
http://i.imgur.com/VyVUipx.gif
Bonus points: Why is there a space between the Player's Collider2D and the walls, if the size of the Collider2D is exactly the size of the sprite? To fix this, I've made the Collider a bit smaller, but still, why is this happening?