Hello! I recently coded in the ability for my character's sprite to flip whenever I change the direction of my movement. However, when I start moving in Play Mode, the Transform settings say that my Z coordinate for scale drops to 0, when it's normally 4.3429, both in edit mode and in Play Mode when I don't press any keys. Is there any way to fix this? Here is my code that I have for the character so far. Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { public float speed = 5f; public float jumpSpeed = 5f; private float movement = 0f; private Rigidbody2D rigidBody; public Transform groundCheckPoint; public float groundCheckRadius; public LayerMask groundLayer; private bool isTouchingGround; // Use this for initialization void Start() { rigidBody = GetComponent<Rigidbody2D>(); } // Update is called once per frame void Update() { isTouchingGround = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, groundLayer); movement = Input.GetAxis("Horizontal"); if (movement > 0f) { rigidBody.velocity = new Vector2(movement * speed, rigidBody.velocity.y); transform.localScale = new Vector2(59.20651f, 59.20651f); } else if (movement < 0f) { rigidBody.velocity = new Vector2(movement * speed, rigidBody.velocity.y); transform.localScale = new Vector2(-59.20651f, 59.20651f); } else { rigidBody.velocity = new Vector2(0, rigidBody.velocity.y); } if (Input.GetButtonDown("Jump") && isTouchingGround) { rigidBody.velocity = new Vector2(rigidBody.velocity.x, jumpSpeed); } } }
For testing, I removed these 2 lines of code: transform.localScale = new Vector2(59.20651f, 59.20651f);, and transform.localScale = new Vector2(-59.20651f, 59.20651f);. The game ran fine in Play Mode (I can see my character in the Game's Camera view), so is there an edit I have to make with these 2 lines of code?
As you are assigning a Vector2 to transform.localScale, which is a Vector3, the 3rd component "z" gets zeroed out ;-P You might want to store the localScale in "Start()" (in a Vector3) so you could use something like this: transform.localScale = new Vector3(-storedScale.x, storedScale.y, storedScale.z);
How would that look in this script in terms of positioning and typing? I'm not sure how to put it in properly, and I don't want to ruin this code. Would you be willing to show me where and how to put it in?