• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by AstronautPug · Aug 22, 2019 at 04:05 PM · 2d2d game2d sprites

Game (2D) not rendering at all

I'm working on a small project in 2D, and made walk cycles for every direction but left, figuring I could just flip the sprite in code. So I made a bit of code to tell it to flip when moving -x, and it worked (judging by the inspector), but it also prevents everything from rendering, leaving only the background color. Here is the code that is causing the problem:

 if (Input.GetAxisRaw("Horizontal") < -0.5f)
         {
             transform.localScale = new Vector2(-0.5f, 0.5f);
         }
         else
         {
             transform.localScale = new Vector2(0.5f, 0.5f);
         }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by AstronautPug · Aug 22, 2019 at 04:51 PM

@I_Am_Err00r Thank you for the help - It now renders everything correctly on startup, but if you turn left, everything vanishes.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image I_Am_Err00r · Aug 22, 2019 at 04:55 PM 0
Share

Please move your answer here to a comment under my answer, it helps with organization and if I provide the right answer, you can give me the points easier :)


Please provide the entire code that handles the movement, it shouldn't do that if it is setup correctly.

avatar image AstronautPug I_Am_Err00r · Aug 22, 2019 at 05:08 PM 0
Share

Here:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : $$anonymous$$onoBehaviour
 {
     public float moveSpeed;
     private bool isFacingLeft = false;
     private void Flip()
     {
         isFacingLeft = !isFacingLeft;
         transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
     }
     private Animator puganim;
     // Start is called before the first frame update
     void Start()
     {
         puganim = GetComponent<Animator>();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
         {
             transform.Translate(new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f));
         }
         if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
         {
             transform.Translate(new Vector2 (0f, (Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime)));
         }
         float movementDirection = Input.GetAxisRaw("Horizontal");
         if (movementDirection > 0 && isFacingLeft == true)
         {
             Flip();
         }
         else if (movementDirection < 0 && isFacingLeft != true)
         {
             Flip();
         }
         puganim.SetFloat("$$anonymous$$oveX", Input.GetAxisRaw("Horizontal"));
         puganim.SetFloat("$$anonymous$$oveY", Input.GetAxisRaw("Vertical"));
     }
 }
 


avatar image I_Am_Err00r AstronautPug · Aug 22, 2019 at 07:32 PM 0
Share

That's weird it's giving you isseues, I don't know why you were getting errors, but I cleaned up your code:

 using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class PlayerController : $$anonymous$$onoBehaviour
  {
      public float moveSpeed;
      private bool isFacingLeft = false;
      private bool isFacingUp = false;
      private Animator puganim;
      private float horizontal$$anonymous$$ovement;
      private float vertical$$anonymous$$ovement;
      
      void Start()
      {
          puganim = GetComponent<Animator>();
      }
      
      void Update()
      {
          horizontal$$anonymous$$ovement = Input.GetAxisRaw("Horizontal");
         vertical$$anonymous$$ovement = Input.GetAxisRaw("Vertical");
      }
  
 
      void FixedUpdate()
      {
          if (horizontal$$anonymous$$ovement > 0.5f || horizontal$$anonymous$$ovement < -0.5f)
          {
              transform.Translate(new Vector2(horizontal$$anonymous$$ovement * moveSpeed * Time.deltaTime, 0f));
              CheckHorizontalDirection();
          }
          if vertical$$anonymous$$ovement > 0.5f || vertical$$anonymous$$ovement < -0.5f)
          {
              transform.Translate(new Vector2 (0f, vertical$$anonymous$$ovement * moveSpeed * Time.deltaTime)));
              CheckVerticalDirection();
          }
          puganim.SetFloat("$$anonymous$$oveX", horizontal$$anonymous$$ovement);
          puganim.SetFloat("$$anonymous$$oveY", vertical$$anonymous$$ovement);
      }
      
      private void CheckHorizontalDirection()
      {
      if (horizontal$$anonymous$$ovement > .5f && isFacingLeft == true)
          {
              FlipX();
          }
          else if (horizontal$$anonymous$$ovement < .5f && isFacingLeft != true)
          {
              FlipX();
          }
      }
      
      private void CheckVerticalDirection()
      {
      if (vertical$$anonymous$$ovement > .5f && isFacingUp != true)
          {
              FlipY();
          }
          else if (vertical$$anonymous$$ovement < .5f && isFacingUp == true)
          {
              FlipY();
          }
      }
      
           private void FlipX()
      {
          isFacingLeft = !isFacingLeft;
          transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
      }
      
      private void FlipY()
      {
           isFacingUp = !isFacingUp;
          transform.localScale = new Vector2(transform.localScale.x, transform.localScale.y * -1);
      }
  }   

Try that, let me know what happens, if you get an error, please take a screenshot of the transform and the object selected so I can see what is going on; also, do you have any other script that manipulate the scale or rotation of the player object?

Show more comments
avatar image
1

Answer by I_Am_Err00r · Aug 22, 2019 at 04:15 PM

Add a bool variable isFacingLeft;

Change that block of code you provided to this:

         float movementDirection = Input.GetAxisRaw("Horizontal");
         if(movementDirection > 0 && isFacingLeft == true)
         {
             Flip()
         }
         else if(movementDirection < 0 && isFacingLeft != true)
         {
             Flip();
         }

And then add this block of code to that script you have there:

 private void Flip()
     {
         isFacingLeft = !isFacingLeft;
         transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
     }

That flips the moment you change directions; if you want you can change the if statements that look like this:

 if(movementDirection > 0 && isFacingLeft == true)

To this (like you have in your code):

  if(movementDirection > .5f && isFacingLeft == true)

That should flip character on movement changes, let me know if you have issues; I don't have Unity with me and wrote this answer on this forum, so there might be errors.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

293 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

unity 2D limb solvers in wrong points 0 Answers

Factorio-like sprite directions 0 Answers

Se distorsionan los bordes de los sprites 0 Answers

HELP!! Vertical Lines in 2D (I've tried Anti Aliasing AND Pixel Snap) 0 Answers

Help 2D Custom HillClimbRacingGame 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges