Question by
blueberry_sushi · Jun 13, 2018 at 08:42 PM ·
rotationunity 2dmouse control
Rotating a sprite with mouse movement
I'm attempting to get a sprite to rotate based on the position of the mouse cursor on screen. I want to be able to control the speed of the rotation, so I'm attempting to use Lerp, but my sprite isn't responding how I'd like it to. It seems be hitting dead zones where it doesn't rotate to the mouse position, as well as instances where it is tracking the cursor, but it becomes very jittery. Any help would be greatly appreciated!
public float rotSpeed = 4f;
void Update () {
Vector3 mPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
float cursorAngle = Vector3.SignedAngle(transform.right, mPosition, transform.forward);
Quaternion pAngle = Quaternion.Euler(0, 0, cursorAngle);
transform.rotation = Quaternion.Lerp(transform.rotation, pAngle, Time.deltaTime * rotSpeed);
}
Comment
Your answer
Follow this Question
Related Questions
Unity 2D jumping only up 0 Answers
Cannonball not rotating with cannon 3 Answers
How to stop the Rotation at eulerAngle.z(-40) 0 Answers
Camera zoom out relative to player's speed 0 Answers
Add force to rotate perpendicular to an object on Unity 2D 0 Answers