Search Unity

Movement Glitch

Discussion in '2D' started by BookwormGames, Oct 11, 2018.

  1. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    39
    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:
    39
    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:
    457
    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);
     
  4. BookwormGames

    BookwormGames

    Joined:
    Aug 30, 2018
    Posts:
    39
    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?