- Home /
Question by
emirhrtrk · Mar 17, 2020 at 08:10 AM ·
c#animation2d gameplayer movementgun movement
Gun is not rotating while player is in a animation
Hi, currently I am working on a 2D top-down shooter game and I have a gun and that gun has a gun rotation script, which rotates my gun towards the mouse, in it and it works well when the player is in IDLE Animation. But when I press D to turn player left (by animation) and when I still pressing D key, my gun doesn't rotate. How can I solve that? Here is my Gun Rotation script:
public class GunController : MonoBehaviour
{
public Rigidbody2D rb;
public Camera cam;
private SpriteRenderer myspriteRenderer;
Vector2 mousePos;
void Start()
{
}
// Update is called once per frame
void Update()
{
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
}
void FixedUpdate()
{
Vector2 lookDir = mousePos - rb.position;
if ( lookDir.x > 0f)
{
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg;
rb.rotation = angle;
myspriteRenderer.flipX = false;
}
if ( lookDir.x < 0f)
{
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 180f;
rb.rotation = angle;
myspriteRenderer.flipX = true;
}
}
void Awake()
{
myspriteRenderer = GetComponent<SpriteRenderer>();
}
}
Comment
Your answer
Follow this Question
Related Questions
,Sprite disappeared on positive y axis 2d unity 0 Answers
How to flip my player left and right? 1 Answer
How do I check if a player is holding a trick and not just tapping it (but i want both) 0 Answers
Player movement starts after a few seconds after pressing the button 1 Answer
PLayer animation 0 Answers