Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
This question was closed Dec 28, 2017 at 03:41 AM by esandberg2000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by esandberg2000 · Dec 27, 2017 at 10:30 PM · charactercontrollernewbie2d animationcharacter movementflip

How to flip 2D character to face direction of movement

 using System.Collections;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
     public bool dead = false;
     public float speed;
     private Rigidbody2D rb2d;
     public GameObject[] health;
     public GameObject Hazard;
     public GameObject HealthPickup;
     private bool facingRight;
     private void Start()
     {
         facingRight = true;
         rb2d = GetComponent<Rigidbody2D>();
     }
     void FixedUpdate()
     {
         if (Input.GetKey("w") || Input.GetKey("a") || Input.GetKey("s") || Input.GetKey("d"))
         {//find controller inputs
             float moveHorizontal = Input.GetAxisRaw("Horizontal");
             float moveVertical = Input.GetAxisRaw("Vertical");
 
             transform.Translate(moveHorizontal * speed * Time.deltaTime, moveVertical * speed * Time.deltaTime, 0);
 
         }
         else
         {
             float moveHorizontal = Input.GetAxisRaw("JoystickHorizontal");
             float moveVertical = Input.GetAxisRaw("JoystickVertical");
 
             transform.Translate(moveHorizontal * speed * Time.deltaTime, moveVertical * speed * Time.deltaTime, 0);
             //wowie here im trying
         }
 
     }
     private void Flip(float moveHorizontal)
     {
         if (moveHorizontal > 0 && !facingRight || moveHorizontal < 0 && facingRight)
         {
             facingRight = !facingRight;
             Vector3 theScale = transform.localScale;
             theScale.x *= -1;
             transform.localScale = theScale;
         }
     }
 
 
 
     public void OnTriggerEnter(Collider other)
     {
         if (other.CompareTag("Hazard"))
         {
             SendMessage("TakeDamage", 1);
             Destroy(other.gameObject);
         }
 
         if (other.CompareTag("HealthPickup"))
         {
             SendMessage("Heal");
             Destroy(other.gameObject);
         }
     }
 }
 
 I'm sorry if I'm making a stupid mistake somewhere, I am very new to this. Any help would be appreciated. I'm trying to make my 2D character flip by making the x Scale -1 when the character switches direction. 
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

  • Sort: 
avatar image
0

Answer by Larry-Dietz · Dec 28, 2017 at 02:48 AM

Flip looks like it should work for you. You just need to call it. I would put in a call at lines 24 and 32.

If you add these calls and still have problems, let me know.

Hope this helps, -Larry

Comment
Add comment · Show 2 · 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 esandberg2000 · Dec 28, 2017 at 03:41 AM 0
Share

@ Larry-Dietz Thank you that fixed it!!

avatar image Larry-Dietz esandberg2000 · Dec 28, 2017 at 05:04 AM 0
Share

Glad I was able to help! -Larry

Follow this Question

Answers Answers and Comments

85 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

Related Questions

Fliping character 180 degrees when moving left 1 Answer

Strange Character Controller Behavior Caused by Simulated Gravity and Ground Check 0 Answers

How to make character controller forward jump a certain distance? 1 Answer

Player Controller "Horizontal" input convert to "left" and "right" 1 Answer

Character Creeping Backwards 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