Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Movement Glitch

Discussion in '2D' started by BookwormGames, Oct 11, 2018 at 7:29 PM.

  1. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    6
    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):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     public float speed = 5f;
    8.     public float jumpSpeed = 5f;
    9.     private float movement = 0f;
    10.     private Rigidbody2D rigidBody;
    11.     public Transform groundCheckPoint;
    12.     public float groundCheckRadius;
    13.     public LayerMask groundLayer;
    14.     private bool isTouchingGround;
    15.  
    16.     // Use this for initialization
    17.     void Start()
    18.     {
    19.         rigidBody = GetComponent<Rigidbody2D>();
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         isTouchingGround = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, groundLayer);
    26.         movement = Input.GetAxis("Horizontal");
    27.         if (movement > 0f) {
    28.             rigidBody.velocity = new Vector2(movement * speed, rigidBody.velocity.y);
    29.             transform.localScale = new Vector2(59.20651f, 59.20651f);
    30.         }
    31.         else if (movement < 0f) {
    32.             rigidBody.velocity = new Vector2(movement * speed, rigidBody.velocity.y);
    33.             transform.localScale = new Vector2(-59.20651f, 59.20651f);
    34.         }
    35.         else {
    36.        
    37.             rigidBody.velocity = new Vector2(0, rigidBody.velocity.y);
    38.         }
    39.         if (Input.GetButtonDown("Jump") && isTouchingGround) {
    40.             rigidBody.velocity = new Vector2(rigidBody.velocity.x, jumpSpeed);
    41.         }
    42.     }
    43. }
    44.  
     
  2. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    6
    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?
     
  3. BIG-BUG

    BIG-BUG

    Joined:
    Mar 29, 2009
    Posts:
    455
    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);