Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Kinda Confused

Discussion in '2D' started by DukkKK, Jul 5, 2021.

  1. DukkKK

    DukkKK

    Joined:
    Feb 5, 2021
    Posts:
    15
    Ok so basically when I start up the game everythings normal but if I move or jump or do anything my charater shrinks to like 1 or -1 I forgot which one. Also the ground boxes are a bit weird but I'm pretty sure thats just from my awful design
    Heres my code.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class playermovement : MonoBehaviour {
    6.     public float movementSpeed;
    7.     public Rigidbody2D rb;
    8.  
    9.     public Animator anim;
    10.  
    11.     public float jumpForce = 20f;
    12.     public Transform Player;
    13.     public LayerMask GroundLayers;
    14.  
    15.     float mx;
    16.  
    17.     private void Update() {
    18.         mx = Input.GetAxisRaw("Horizontal");
    19.  
    20.         if (Input.GetButtonDown("Jump") && IsGrounded()) {
    21.             Jump();
    22.         }
    23.  
    24.         if (Mathf.Abs(mx) > 0.05f) {
    25.             anim.SetBool("isrunning", true);
    26.         } else {
    27.             anim.SetBool("isrunning", false);
    28.         }
    29.  
    30.         if (mx > 0f) {
    31.             transform.localScale = new Vector3(1f, 1f, 1f);
    32.         } else if (mx < 0f) {
    33.             transform.localScale = new Vector3(-1f, 1f, 1f);
    34.         }
    35.  
    36.         anim.SetBool("IsGrounded", IsGrounded());
    37.     }
    38.  
    39.     private void FixedUpdate() {
    40.         Vector2 movement = new Vector2(mx * movementSpeed, rb.velocity.y);
    41.  
    42.         rb.velocity = movement;
    43.     }
    44.  
    45.     void Jump() {
    46.         Vector2 movement = new Vector2(rb.velocity.x, jumpForce);
    47.  
    48.         rb.velocity = movement;
    49.     }
    50.     public bool IsGrounded() {
    51.         Collider2D GroundCheck = Physics2D.OverlapCircle(Player.position, 2f, GroundLayers);
    52.  
    53.         if (GroundCheck != null) {
    54.             return true;
    55.         }
    56.  
    57.         return false;          
    58.     }
    59. }
    60.  
     
  2. LethalInjection

    LethalInjection

    Joined:
    Jan 25, 2015
    Posts:
    36
    What is confusing ? what is your expected outcome ?
    lines 30 - 34 are settings the scale which is why it is shrinking.
     
  3. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    What @LethalInjection said, and if your goal is to flip a character with a scale of more than 1,1,1, then you need to set the scale to what it actually is, or do it dynamically:
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         if (mx > 0f)
    4.         {
    5.             transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
    6.         }
    7.         else if (mx < 0f)
    8.         {
    9.             transform.localScale = new Vector3(-Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
    10.         }
    11.     }
     
  4. DukkKK

    DukkKK

    Joined:
    Feb 5, 2021
    Posts:
    15
    Thank ya'll so much