• 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 DaedalusEnix · Sep 03, 2016 at 06:14 AM · getaxis

imput.GetAxes ("Horizontal") moving/flip side together, help!

Hello guys, so i stucked with my stick boy too long, i surrender trying to figure out alone, here is the problem:

when i compil, my stick boy only moves to the right side, when i press left, only the image switches the side but my stick boy still keep moving to the right. when i remove the the condition ''if'' transform.eulerangles (0,180) (or transform.localscale (-1,1)), he backs to normal meaning that when a press left he move to the left and right when i press right, but i want to flip his side and move together! please help me! look at my script below

using UnityEngine; using System.Collections;

public class Player : MonoBehaviour {

 public   GameObject player;
 public     float      speed;
 public float MinHeight;


 // Use this for initialization
 void Start () {
     


 }

 // Update is called once per frame
 void Update ()
 {

     float translation = Input.GetAxis ("Vertical") * speed;
     player.transform.Translate (0, translation, 0);
     if (player.transform.position.y < MinHeight) {
         player.transform.position = new Vector2 (transform.position.x, MinHeight);
     }
     float diresq = Input.GetAxis ("Horizontal") * speed;
     player.transform.Translate (diresq, 0, 0);

     if (Input.GetAxis ("Horizontal") < (0)) {
         player.transform.eulerAngles = new Vector2 (0,180);

     }
         if (Input.GetAxis ("Horizontal") > (0)) {
         player.transform.eulerAngles = new Vector2 (0,0);


     }
 }

}

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by b1gry4n · Sep 03, 2016 at 06:29 AM

You only need to get the input axis one time and as long as you store it you do not need to keep checking for input.

     void Update()
     {
         float vertical = Input.GetAxis("Vertical");
         float horizontal = Input.GetAxis("Horizontal");
 
         player.transform.Translate(horizontal * speed, vertical * speed, 0);
 
         if (player.transform.position.y < MinHeight)
         {
 //not sure if youre trying to match the players transform to the transform this script is on... but i changed this
                 player.transform.position = new Vector2(player.transform.position.x, MinHeight);
         }
 
         if (horizontal < 0)
         {
             player.transform.eulerAngles = new Vector2(0, 180);
         }else
         if (horizontal > 0)
         {
             player.transform.eulerAngles = new Vector2(0, 0);
         }
     }

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 DaedalusEnix · Sep 03, 2016 at 03:33 PM 0
Share

Hello thanks a lot for trying to help! i compil your script and still have the same problem, the stick boy that i creates keeps moving to the right no matter if is pressed left or right, only the images is rotationed to the left or to the right.

avatar image
0

Answer by DaedalusEnix · Sep 03, 2016 at 07:47 PM

I finally solved the problem but i even no understand what i did, and sounds ugly

public class Player : MonoBehaviour {

 public   GameObject player;
 public     float      speed;
 public float MinHeight;


 // Use this for initialization
 void Start () {



 }

 // Update is called once per frame
 void Update ()
 {

     float vertical = Input.GetAxis("Vertical");
     float horizontal = Input.GetAxis("Horizontal");

     player.transform.Translate (horizontal * speed, vertical * speed, 0);

     if (player.transform.position.y < MinHeight)
     {
         //not sure if youre trying to match the players transform to the transform this script is on... but i changed this
         player.transform.position = new Vector2(player.transform.position.x, MinHeight);
     }

     if (horizontal < 0)
     {
         player.transform.Translate (horizontal * speed*-2, vertical * speed ); 
         player.transform.eulerAngles = new Vector2(0, 180);
     }else
         if (horizontal > 0)
         {

             player.transform.eulerAngles = new Vector2(0, 0);
         
 }

as you can see i put a new statment >>>> player.transform.Translate (horizontal* speed*-2, vertical * speed, 0); i'm not satisfied with this solution cause is non sense in my opinion, when i remove all the condition about rotationing and let the getaxis play alone, i see no problem with the movimentation, press left he goes to the left, press right he goes to the right. that's it, still awaiting a Real explanation, thank you.

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 villevli · Sep 03, 2016 at 09:20 PM 0
Share
 void Update()
      {
          float vertical = Input.GetAxis("Vertical");
          float horizontal = Input.GetAxis("Horizontal");
  
          player.transform.Translate($$anonymous$$athf.Abs(horizontal) * speed, vertical * speed, 0);
  
          if (player.transform.position.y < $$anonymous$$inHeight)
          {
                  player.transform.position = new Vector2(player.transform.position.x, $$anonymous$$inHeight);
          }
  
          if (horizontal < 0)
          {
              player.transform.eulerAngles = new Vector2(0, 180);
          }else
          if (horizontal > 0)
          {
              player.transform.eulerAngles = new Vector2(0, 0);
          }
      }
avatar image
0

Answer by villevli · Sep 03, 2016 at 06:21 PM

transform.Translate moves the object relative to its local axes so the rotation of the object affects the direction of the movement. You can use transform.Translate(x, y, z, Space.World) to move in world space. Or in your script you can try to make the horizontal value absolute with Mathf.Abs(). (Only for the translation since you later check if the value is negative)

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
avatar image
0

Answer by ArturoSR · Sep 03, 2016 at 09:13 PM

Hello there.

OK, this is the answer to your problem, it happens to me the first time using Unity, so, the problem is here, you don't have to rotate the player controller, you need to create this hierarchy first:

  1. Player (Controller Component) even you can control the Animator directly from this.

  2. Main Character, must be a child from above (The Controller has to have a Transform field on it) and none Controller < This is important.

So, where ever you move your Controller (Axis Horizontal), it has to move left or right, but when is about to turn around (0 to 180), you need to rotate the child (the Character model), this way you can achieve your goal, also the better way to achieve it it's using the Character Controller component attached to the root not to the 3d model.

The error happens because you turn around the main root, I know for sure this going to works, because I use it a lot, cheers.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

How to Rotate my spaceship on the Y axis or turn it around but not right around like half using the horizontal 0 Answers

How can I simulate the smoothness of the Input.GetAxis? 0 Answers

{Newbie Here} How to make my gameObject move using Get.Axis("Horizontal")? 3 Answers

getaxis not working. 0 Answers

Help needed in moving object 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges