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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved How do i fix the code i broke? Lines 30 to 38.

Discussion in 'Editor & General Support' started by unity_7ADBFCA753D9332FD8E6, Dec 5, 2022.

  1. unity_7ADBFCA753D9332FD8E6

    unity_7ADBFCA753D9332FD8E6

    Joined:
    Dec 5, 2022
    Posts:
    3
    It was working fine and seemingly stopped out of nowhere. I've spent too much time trying to figure out what I did so I'm hoping someone here can help. Lines 30 to 38 is the code that doesnt seem to be doing what I'm wanting. The print statement works, so I know the condition for the if statement is still working fine. But my character is forced to look to the right no matter what. Any ideas?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.     [SerializeField] private float speed;
    9.     [SerializeField] private float jumpHeight;
    10.     private BoxCollider2D playerBox;
    11.     private Rigidbody2D body;
    12.     private Animator anim;
    13.     [SerializeField] private LayerMask groundLayer;
    14.     [SerializeField] private LayerMask wallLayer;
    15.     [SerializeField] private LayerMask goalLayer;
    16.  
    17.     private void Awake()
    18.     {
    19.         playerBox = GetComponent<BoxCollider2D>();
    20.         body = GetComponent<Rigidbody2D>();
    21.         anim = GetComponent<Animator>();
    22.     }
    23.  
    24.     private void Update()
    25.     {
    26.         if (onGoal()){
    27.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    28.             SceneManager.UnloadSceneAsync(SceneManager.GetActiveScene());}
    29.        
    30.         float horizInput = Input.GetAxis("Horizontal");
    31.         body.velocity = new Vector2(horizInput * speed, body.velocity.y);
    32.         print(horizInput);
    33.         if (horizInput > 0.1f)
    34.             print("im looking right");
    35.             transform.localScale = new Vector3(1, 1, 1);
    36.         if (horizInput < -0.1f)
    37.             print("im looking left");
    38.             transform.localScale = new Vector3(-1, 1, 1);
    39.  
    40.         if (Input.GetKey(KeyCode.Space) && isGrounded())
    41.             Jump();
    42.  
    43.         anim.SetBool("run", horizInput != 0);
    44.         anim.SetBool("grounded", isGrounded());
    45.     }
    46.  
    47.     private void Jump()
    48.     {
    49.         body.velocity = new Vector2(body.velocity.x, jumpHeight);
    50.         anim.SetTrigger("jump");
    51.     }
    52.  
    53.     private bool isGrounded()
    54.     {
    55.         RaycastHit2D raycastHit = Physics2D.BoxCast(playerBox.bounds.center, playerBox.bounds.size, 0, Vector2.down, 0.02f, groundLayer);
    56.         return raycastHit.collider != null;
    57.     }
    58.  
    59.     private bool onWall()
    60.     {
    61.         RaycastHit2D raycastHit = Physics2D.BoxCast(playerBox.bounds.center, playerBox.bounds.size, 0, new Vector2(transform.localScale.x, 0), 0.02f, wallLayer);
    62.         return raycastHit.collider != null;
    63.     }
    64.  
    65.     private bool onGoal()
    66.     {
    67.         RaycastHit2D raycastHit = Physics2D.BoxCast(playerBox.bounds.center, playerBox.bounds.size, 0, Vector2.down, 0.02f, goalLayer);
    68.         return raycastHit.collider != null;
    69.     }
    70. }
    71.  
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
    Get in the habit of using curly braces in ALL your if statements.

    Because this:
    Code (CSharp):
    1. if (horizInput > 0.1f)
    2.     print("im looking right");
    3.     transform.localScale = new Vector3(1, 1, 1);
    Is actually this:
    Code (CSharp):
    1. if (horizInput > 0.1f)
    2. {
    3.     print("im looking right");
    4. }
    5.  
    6. transform.localScale = new Vector3(1, 1, 1);
    So both your
    transform.localScale
    assignments are always running.
     
  3. unity_7ADBFCA753D9332FD8E6

    unity_7ADBFCA753D9332FD8E6

    Joined:
    Dec 5, 2022
    Posts:
    3
    oh okay. So an if statement without curly braces executes only the immediately following statement?

    Also I changed it to the following. Its still not editing the Scale. It does print "Im looking left" if im running to the left of the screen though. It might be worth noting that the print statements are for debugging
    Code (CSharp):
    1.         if (horizInput > 0.1f)
    2.             {
    3.                 print("im looking right");
    4.                 transform.localScale = new Vector3(1, 1, 1);
    5.             }
    6.         if (horizInput < -0.1f)
    7.             {
    8.                 print("im looking left");
    9.                 transform.localScale = new Vector3(-1, 1, 1);
    10.             }
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
    Yes, and I find them not great for code readability, personally.

    First make sure you've saved the updated script, and that Unity has reloaded.

    Otherwise make sure you're operating on the correct axis so that the world x-axis is left/right.

    Also FYI, if you're working with sprites, the SpriteRenderer component has properties for flipping which way the sprite is facing: https://docs.unity3d.com/ScriptReference/SpriteRenderer-flipX.html
     
  5. unity_7ADBFCA753D9332FD8E6

    unity_7ADBFCA753D9332FD8E6

    Joined:
    Dec 5, 2022
    Posts:
    3
    I am! I bet that will be more consistent than transform.localScale.
    I'll give that a try and thank you!

    worked like a charm! Thanks again

    solution - instantiate the SpriteRenderer component in the awake function

    and then

    Code (CSharp):
    1.         if(Input.GetKeyDown(KeyCode.D))
    2.         {
    3.             // flip the sprite
    4.             playerSprite.flipX = false;
    5.         }
    6.         if(Input.GetKeyDown(KeyCode.A))
    7.         {
    8.             // flip the sprite
    9.             playerSprite.flipX = true;
    10.         }
     
    Last edited: Dec 5, 2022