• 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 /
avatar image
0
Question by ThatFlor · Aug 26, 2019 at 07:43 PM · movementplayerflip

Flipping character sprite to look the way he's moving,How to make my character flip when walking?

 public class PlayerMovement : MonoBehaviour
  {
      public LayerMask m_LayerMask;
      public Rigidbody2D rgb;
      public BoxCollider2D groundChecker;
  
      public float speed;
      public float acceleration;
      public float jumpHeight;
      private float currentSpeed;
      private float targetSpeed;
      private Vector2 amountToMove;
  
      
  
      // Start is called before the first frame update
      void Start()
      {
          
      }
  
      // Update is called once per frame
      void Update()
      {
          targetSpeed = Input.GetAxisRaw("Horizontal") * speed;    
          currentSpeed = IncrementTowards(currentSpeed, targetSpeed, acceleration);
          if (TengoPiso())
          { 
              if (Input.GetKeyDown(KeyCode.W))
               {
                  rgb.AddForce(new Vector2(0, jumpHeight), ForceMode2D.Impulse);
              }
          }
          
  
          amountToMove.x = currentSpeed;
          Move(amountToMove * Time.deltaTime);
      }
  
      private float IncrementTowards (float n, float target, float a)
          //n = currentSpeed
          //targer = targetSpeed
          //a = acceleration
      {
            if(n == target) // si currentSpeed = targetSpeed, devuelve a currentSpeed
          {
              return n;
          }
          else
          {
              float dir = Mathf.Sign(target - n); //n incrementa o baja para acercarse al target, en que direccion debe moverse, sea positiva o negativa.
  
              if(dir == 1)
              {
                  this.transform.localScale = new Vector3(1, 1, 1) * 3.6f;
              }
              else
              {
                  this.transform.localScale = new Vector3(-1, 1, 1) * 3.6f;
              }
              n += a * Time.deltaTime * dir;
              return (dir == Mathf.Sign(target - n)) ? n : target;
              // Condition,  devuelve si la direccion es la misma, o sea la currentSpeed no ha pasado el target
              //? adentro del if, 
              //y n: es el else, de otra manera, si ya paso, devolver al target.
          }
      }
  
      public void Move(Vector2 moveAmount)
      {
          transform.Translate(moveAmount);
      }

I did a flip in the class IncrementTowards, after getting the direction as Mathf.Sign, tho this flip doesnt work as intended, it looks the way it should when its walking but after I stop it flips again and starts flipping the opposite way after I move again. Note that the player has a weapon and firepoint on him, which mean this should flip to the other side with the character sprite.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SirPaddow · Aug 26, 2019 at 08:16 PM

It sounds weird that you want your character to face left or right depending on wether it needs to accelerate or decelerate. Are you sure what you want is not as simple as:

 if (currentSpeed < 0) {
     // face left
 }
 else if (currentSpeed > 0) {
     // face right
 }

Btw, I think it's a bad idea to rescale the gameobject to flip the sprite. You will get very strange behaviours with colliders and other components. You probably have a SpriteRenderer on your gameobject, and with it you can flip the sprite using its "flipX" property. So, somehting like:

 spriteRenderer.flipX = currentSpeed < 0;
Comment
Add comment · Show 3 · 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 ThatFlor · Aug 26, 2019 at 08:37 PM 0
Share

You're right, the flip works like this, tho using the Sprite Renderer, flips the sprite itself and not the sons of it, but I need the character to flip with another sprite (gun), which is a son of the character, and an empty object, used as a placeholder for a firepoint (called firepoint), also inside the character in Unity. Is there a way to do it with the sprite renderer?

avatar image SirPaddow ThatFlor · Aug 26, 2019 at 09:02 PM 0
Share

You can either have multiple public attributes in your script, like:

 public SpriteRenderer characterSpriteRenderer;
 public SpriteRenderer weaponSpriteRenderer;
 
 // in Update()
 
 characterSpriteRenderer.flipX = currentSpeed <0;
 weaponSpriteRenderer.flipX = currentSpeed <0;

Or you can access it using GetComponentsInChildren:

 foreach (SpriteRenderer renderer in GetComponentsInChildren<SpriteRenderer>()) {
    renderer.flipX = currentSpeed < 0;
 }

In the latest, you should definitely cache the renderers, and not call this function at every frame.

avatar image ThatFlor SirPaddow · Aug 26, 2019 at 09:11 PM 0
Share

It worked, thanks for the help

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

182 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

Related Questions

How to move player after animation 2 Answers

Character getting stuck on curbs.. 0 Answers

Need help on making player move towards gameobject, if clicked 1 Answer

Vertical mouse input inverted when player is turned around 0 Answers

Sprite flip movement script not working! HELP! 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