- Home /
2D Aiming in Physics Based Racer
I'm creating a physics based racer where you can use a gun. Currently using unity's input system to control everything, cinemachine settings in picture to track the car and the code listed below on the gun. I believe the issue is as the shotgun is relying on the camera to aim, so as soon as cinemachine adds some offset due to smoothing, it slightly messes up the aiming of the gun. Is there a way to fix this? Thinking about possibly offsetting the aiming position to where the camera is or setting up a secondary camera for aiming somehow.
mousePosition = mainCamera.ScreenToWorldPoint(Mouse.current.position.ReadValue()); Vector2 shotgunDirection = mousePosition - rb.position;
float shotgunAngle = Mathf.Atan2(shotgunDirection.y, shotgunDirection.x) * Mathf.Rad2Deg; rb.rotation = shotgunAngle;