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 /
avatar image
0
Question by andrewwebber25 · Jul 27, 2017 at 07:46 PM · animator controllerdirectionanimationsplayer movementtouch controls

Touch Controls for Player Movement not Working Properly

Hey so this is a bit of a loaded question so theres a good chance that im going to award some points if anyone can figure this out. So I made a 2D sidescroller that up until now i have used the keyboard to control left and right movement with GetAxis ("Horizontal"). The player walks left and right fine and animates properly. Meaning that if I walk left, he actually faces and walks the direction.

Im developing this game for android phones so now I made some touch buttons on the canvas. They work well and when pressed the player walks left and right. My only issue is that when he walks left, instead of walking facing that direction, he looks like he is walking backward/moonwalking. How can I get it to play the mirrored animation like when he uses keyboard commands? Here is my code for the Player:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class Player : MonoBehaviour
 {
 
     public float maxSpeed = 3;
     public float speed = 50f;
     public float jumpPower = 150f;
     public bool grounded;
     private Rigidbody2D rb2d;
     private Animator anim;
 
     public bool moveright;
     public bool moveleft;
 
     void Start()
     {
 
         rb2d = gameObject.GetComponent<Rigidbody2D>();
         anim = gameObject.GetComponent<Animator>();
 
 
     }
 
 
     void Update()
     {
 // This code is used to move the player with keyboard
 
         anim.SetBool("Grounded", grounded);
         anim.SetFloat("Speed", Mathf.Abs(rb2d.velocity.x));
 
         if (Input.GetAxis("Horizontal") < -0.1)
         {
             transform.localScale = new Vector3(-1, 1, 1);
 
         }
         if (Input.GetAxis("Horizontal") > 0.1)
         {
             transform.localScale = new Vector3(1, 1, 1);
 
         }
 
         if (Input.GetButtonDown("Jump") && grounded)
         {
             rb2d.AddForce(Vector2.up * jumpPower);
 
         }
 
 //This code is using the bools from above to make player move by touch icons
 
         if (Input.GetKey(KeyCode.LeftArrow))
         {
             rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
 
         }
         if (Input.GetKey(KeyCode.RightArrow))
         {
             rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
         }
 
 
         if (moveright)
         {
             rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
         }
         if (moveleft)
         {
             rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
         }
 
     }
 
     void FixedUpdate()
     {
 
         Vector3 easeVelocity = rb2d.velocity;
         easeVelocity.y = rb2d.velocity.y;
         easeVelocity.z = 0.0f;
         easeVelocity.x *= 0.75f;
 
         float h = Input.GetAxis("Horizontal");
 
         //Fake friction / Easing the x speed of our player
         if (grounded)
         {
             rb2d.velocity = easeVelocity;
         }
 
 
         //Moving the Player
         rb2d.AddForce((Vector2.right * speed) * h);
 
         //Limiting the Speed of the Player
         if (rb2d.velocity.x > maxSpeed)
         {
             rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
 
         }
 
         if (rb2d.velocity.x < -maxSpeed)
         {
             rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
 
         }
     }
 }

I originally tried replacing the "rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);" code with "transform.localScale = new Vector3(-1, 1, 1);" which made the player face the proper directions with the touch arrows but stopped him from walking. For all I know, which direction the player animates isnt even part of this script, its been so long since Ive done it. Im stumped really hard here guys and I know this is a loaded question with a lot of code but Ill take any suggestions or alternative methods if you can think of any! Thanks

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
1
Best Answer

Answer by FortisVenaliter · Jul 27, 2017 at 08:18 PM

You tried replacing the rigidbody code with the scale code? Try merging them. Use both statements within the if block instead of one or the other.

Comment
Add comment · Show 1 · 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 andrewwebber25 · Jul 27, 2017 at 08:27 PM 0
Share

woah i feel kind of stupid, how didnt i think of that?! Thank you very much for the quick fix!

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.

Follow this Question

Answers Answers and Comments

69 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

Related Questions

Change animations without complete the first one? 1 Answer

Gameobject does not leave TPose in Playmode 0 Answers

Can't seem to get my Transition to work at all 1 Answer

Accessing an FBX's Animated Custom Properties in runtime. 0 Answers

Animator switching between floats 1 Answer


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