From 84f1283d97689d992040c81fd000e3099c733668 Mon Sep 17 00:00:00 2001 From: Manolache Razvan Date: Mon, 1 Nov 2021 00:44:55 +0200 Subject: [PATCH] Flipping without changing transform You will flip the sprite with SpriteRenderer component(not with transform). This is better if you dont want the x scale -1. --- CharacterController2D.cs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/CharacterController2D.cs b/CharacterController2D.cs index 925bfe0..a7c193d 100644 --- a/CharacterController2D.cs +++ b/CharacterController2D.cs @@ -16,6 +16,7 @@ public class CharacterController2D : MonoBehaviour private bool m_Grounded; // Whether or not the player is grounded. const float k_CeilingRadius = .2f; // Radius of the overlap circle to determine if the player can stand up private Rigidbody2D m_Rigidbody2D; + private SpriteRenderer m_spriteRenderer; private bool m_FacingRight = true; // For determining which way the player is currently facing. private Vector3 m_Velocity = Vector3.zero; @@ -33,7 +34,7 @@ public class BoolEvent : UnityEvent { } private void Awake() { m_Rigidbody2D = GetComponent(); - + m_spriteRenderer = GetComponent(); if (OnLandEvent == null) OnLandEvent = new UnityEvent(); @@ -135,12 +136,7 @@ public void Move(float move, bool crouch, bool jump) private void Flip() { - // Switch the way the player is labelled as facing. m_FacingRight = !m_FacingRight; - - // Multiply the player's x local scale by -1. - Vector3 theScale = transform.localScale; - theScale.x *= -1; - transform.localScale = theScale; + m_spriteRenderer.flipX= !m_FacingRight; } }