So, I’m trying to get my player sprite to flip along the x-axis according to which side the mouse is (i.e. if the mouse cursor is to the right, the player faces right). My code for this is: public Rigidbody2D rb; public SpriteRenderer sr; public GameObject go; private void Start() { rb = GetComponent<Rigidbody2D>(); sr = GetComponent<SpriteRenderer>(); go = GetComponent<GameObject>(); } if(Input.mousePosition.x > go.transform.position.x) { sr.flipX = false; } else if (Input.mousePosition.x < go.transform.position.x) { sr.flipX = true; }
Now, this code conflicts with my movement script, which is: ` void FixedUpdate () {
if (Y > -7.0f)
{
grounded = true;
}
if (X > 0.0f)
{
X -= 0.5f;
}
if (X < 0.0f)
{
X += 0.5f;
}
if (Y > -7.0f)
{
Y -= 0.7f;
}
if (Input.GetKey("a"))
{
X = -7.0f;
}
if (Input.GetKey("d"))
{
X = 7.0f;
}
if (Input.GetKeyDown(KeyCode.Space))
{
if(canJump == true && grounded == true)
{
Y = 11.0f;
Debug.Log("jump");
}
}rb.velocity = new Vector2(x: X, y: Y);
}
private void OnCollisionEnter2D(Collision2D collision)
{
canJump = true;
}
private void OnCollisionExit2D(Collision2D collision)
{
canJump = false;
}
}
`
Note that the movement code works fine on its own, but when I added the sprite flipping code it magically stops working. Could anyone tell me why this happens and/or give me an alternative method to do this?