- Home /
How do i flip my character and its objects and not just the sprite
My code only makes the sprite rotate and not the objects attached to a player, can i get some help to make the whole char rotate without flipping the camera Here is the code public Animator animator; Rigidbody2D rg2d; SpriteRenderer spriteRenderer; public float runspeed = 8f; public float jumpforce = 18f; bool isGrounded; [SerializeField] Transform groundCheck; [SerializeField] Transform groundCheck2; [SerializeField] Transform groundCheck3; void Start() { animator = GetComponent<Animator>(); rg2d = GetComponent<Rigidbody2D>(); spriteRenderer = GetComponent<SpriteRenderer>(); } private void FixedUpdate() { if ((Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"))) || (Physics2D.Linecast(transform.position, groundCheck2.position, 1 << LayerMask.NameToLayer("Ground"))) || (Physics2D.Linecast(transform.position, groundCheck3.position, 1 << LayerMask.NameToLayer("Ground")))) { isGrounded = true; animator.SetBool("Jumping", false); } else { isGrounded = false;animator.SetBool("Jumping", true); } if (Input.GetKey("d")) { rg2d.velocity = new Vector2(runspeed, rg2d.velocity.y); spriteRenderer.flipX = false; } else if (Input.GetKey("a")) { rg2d.velocity = new Vector2(-(runspeed), rg2d.velocity.y); spriteRenderer.flipX = true; } else { rg2d.velocity = new Vector2(0, rg2d.velocity.y); } if (Input.GetKey("space") && isGrounded) { rg2d.velocity = new Vector2(rg2d.velocity.x, jumpforce); } animator.SetFloat("Speed",Mathf.Abs(rg2d.velocity.x)); if (Input.GetKeyDown("q") && isGrounded) { animator.SetBool("Attack", true); } else animator.SetBool("Attack", false);
Answer by kreaturian · May 27, 2019 at 06:14 AM
Instead of using the flipX variable in the SpriteRenderer component, I would recommend just changing the transform of the parent object. So if you want to flip the player, just make the player's Y rotation 0 for not flipped and 180 if you want it flipped.
I agree, but @$$anonymous$$arkoTheGameNord, you also mention that you don’t want your camera to flip, which suggests that it is a child of the player. You can avoid flipping the camera by:
Not having it a child in the first place and just create a follow script that clamps to the player movement you want to follow.
Leave it as a child, but make its parent = null before the flipping logic and then re-parent after flipping.
Leave it as a child, but after flipping, reverse the flip of the camera. If you set the y rotation from 0 to 180 degrees. Then do the opposite with the camera after the flip occurs.
Cheers
Answer by UltraGearGames · May 27, 2019 at 08:22 AM
Set the player's scale to -1 on the X.
I used to do this, but no longer recommend it because if you have animations within unity that use squash and stretch, then doing this will collide with what the animations want to do. So i recommend just flipping the y rotation 180.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Sprite "face" second sprite only on Z axis 2 Answers
I'm trying to make a bike minigame, and I can't make the pedal 1 Answer
Multiple Cars not working 1 Answer
Saving Rotation of Sprite 1 Answer