- Home /
[Solved] Rigidbody2d smooth movement
Hello. I am new to Unity. I have a question about the movement of Rigidbody2D. I add a sprite with Rigidbody2D to the 2d scene.
Player movement script:
private Vector2 movement;
private Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
movement = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
}
private void FixedUpdate()
{
rb.AddForce(movement * 4000 * Time.fixedDeltaTime);
}
Script on camera:
private GameObject player;
void Start()
{
player = GameObject.FindWithTag("Player");
}
void Update()
{
Vector3 moveTo = new Vector3(player.transform.position.x, player.transform.position.y, -10f);
transform.position = Vector3.Lerp(transform.position, moveTo, 10.0f * Time.deltaTime);
}
When I start the game, the player moves with shaking, not smoothly. How to avoid this? Thanks.
Answer by mickeru · Mar 04, 2020 at 10:17 PM
Enabling interpolation for Rigidbody2D and increasing the values of Linear Drag and Angular Drag solved the problem. In addition, you must include your sprite in the Player object as a child and rotate it if necessary (for example, rotate to a crosshair).
Your answer
Follow this Question
Related Questions
2D Game, Character Slides off Slopes, Need to be Able to Walk on Them 1 Answer
Rigid Body Movement (Mobile Issue) 3 Answers
Standing on player to follow his movement,Follow movement when standing on moving platform/player 1 Answer
Rigidbody2D x velocity not moving unless placed in FixedUpdate 1 Answer