How to make a 2D object tilt smoothly based on mouse movement?
I'm making a 2D "block breaker" kind of game where the player is a paddle that follows the mouses x-position. I want the paddle to smoothly tilt a bit in the direction it is currently moving. This is my code which gets called in Update():
private void MovePaddle()
{
paddlePosX = (Input.mousePosition.x / Screen.width * screenWidthInUnits);
transform.position = new Vector2(Mathf.Clamp(paddlePosX, 3.08f, 12.92f), transform.position.y);
vel = (transform.position.x - previous) / Time.deltaTime;
previous = transform.position.x;
gameObject.GetComponent<Rigidbody2D>().rotation = Mathf.Clamp(vel * (-1),-30f,30f);
}
The movement works well, and the tilting works semi-well. The problem is that mouse movement isnt as easy to detect as, lets say, arrow keys. The mouse velocity gets updated every frame so it fluctuates really hard and the paddle isnt moving smoothly, but rather constantly moving within the normal rotation and the rotation specified by the mouse. I think I need a system to make rotation accelerate and decelerate instead of updating it every second in order to make it look more smoothly, but i cant get that to work, as I'm still pretty much a beginner. I'd really appreciate it if someone could come up with a solution that is beginner-friendly and doesn't use complex functions. It doesn't have to be elegant, I'd much rather appreciate a less-optimal but more easy to understand solution :)
Answer by $$anonymous$$ · Nov 16, 2020 at 10:17 AM
Check this out this is about shooting but in the first part of the video he teaches how to rotate the weapon.
Will maybe be helpful