Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 superluigi · Jun 13, 2013 at 05:56 AM · javascriptmovementcharactercontroller

My CharacterController does not walk and face in the same direction

When I go left my CharacterController turns left which is good but he walks to the right. Going right works fine and while I know why hes walking backwards I cannot figure out how to fix it. This is a 2D game movement is on the X axis.

     var speed                 : float                     = 4.0;
   //var speedo                 : float                     = -4.0;
     var gravity             : float                     = 20.0;
     private var velocity     : Vector3                     = Vector3.zero;
     var controller             : CharacterController;
     
     function Update () 
     {
         if (controller.isGrounded)
         {
             velocity = GetHorizontalMovement();
             velocity *= speed;
         }
         velocity.y -= gravity * Time.deltaTime;
         controller.Move (velocity * Time.deltaTime);
     
         if(Input.GetAxisRaw("Horizontal") > 0)
             transform.rotation = Quaternion.Euler(0, 0, 0);
         else if(Input.GetAxisRaw("Horizontal") < 0)
             transform.rotation = Quaternion.Euler(0, 180, 0);
     }
     
     function GetHorizontalMovement()
     {
          var direction : Vector3 = Vector3 (Input.GetAxisRaw("Horizontal"), 0, 0);
          return transform.TransformDirection (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

3 Replies

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

Answer by superluigi · Jun 13, 2013 at 08:41 PM

Ok figured it out although I'm not 100% sure how I did it.

 var speed                 : float                     = 4.0;
 var gravity             : float                     = 20.0;
 private var velocity     : Vector3                     = Vector3.zero;
 var controller             : CharacterController;
 
 function Update () 
 {
     if (controller.isGrounded && Input.GetAxisRaw("Horizontal") > 0)
     {
         transform.rotation = Quaternion.Euler(0, 0, 0);
         velocity = GetHorizontalMovement();
         velocity *= speed;
     }
 
     else if(controller.isGrounded && Input.GetAxisRaw("Horizontal") < 0)
     {
         transform.rotation = Quaternion.Euler(0, 180, 0);
            velocity = GetHorizontalMovement();
         velocity *= speed;
     }
     
     else if(controller.isGrounded)
     {
         velocity = GetHorizontalMovementZero();
     
     }
 
 velocity.y -= gravity * Time.deltaTime;
 controller.Move (velocity * Time.deltaTime);
     
 }
 
 function GetHorizontalMovement()
 {
 var direction : Vector3 = Vector3 (1, 0, 0);
 return transform.TransformDirection (direction);
 }
 
 function GetHorizontalMovementZero()
 {
 var direction : Vector3 = Vector3 (0, 0, 0);
 return transform.TransformDirection (direction);
 }
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 jerobinson · Jun 13, 2013 at 07:06 AM

The problem is that your horizontal movement changes between + / - when pressing left / right. At the same time, the character also changes rotation depending on what key you press. (Pressing right; character faces right and moves forward, pressing left; character faces left and moves backwards). So rather than changing the characters velocity between positive or negative, just make it equal to speed when there is input:

 if(Input.GetAxis("Horizontal") > 0f) {
     velocity.z = speed;
 }

 

EXAMPLE SCRIPT:

    var speed           : float             = 4.0;
   //var speedo           : float            = -4.0;
     var gravity          : float            = 20.0;
     private var velocity    : Vector3               = Vector3.zero;
     var controller         : CharacterController;
  
     function Update () 
     {
         if (controller.isGrounded)
         {
            if(Input.GetAxisRaw("Horizontal") > 0) {
                velocity.x = speed;
            }
         }
         velocity.y -= gravity * Time.deltaTime;
         controller.Move (velocity * Time.deltaTime);
  
         if(Input.GetAxisRaw("Horizontal") > 0)
            transform.rotation = Quaternion.Euler(0, 0, 0);
         else if(Input.GetAxisRaw("Horizontal") < 0)
            transform.rotation = Quaternion.Euler(0, 180, 0);
     }
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 superluigi · Jun 13, 2013 at 07:57 AM 0
Share

so what your saying is change my GetHorizontal$$anonymous$$ovement function? sorry I'm still confused also do you really want me to mess with the z axis or is that a typo?

avatar image jerobinson · Jun 13, 2013 at 11:09 AM 0
Share

Sorry, yes. I meant to say velocity.x = speed;. Your GetHorizontal$$anonymous$$ovement function is making the player move backwards whenever he looks left. The most basic way that you could fix this would be to change var direction : Vector3 = Vector3 (Input.GetAxisRaw("Horizontal"), 0, 0); to var direction : Vector3 = Vector3 (1f, 0, 0);

I have added a made the adjustments needed to your code, anded added it to my answer above. ;)

avatar image superluigi · Jun 13, 2013 at 07:59 PM 0
Share

I already tried that but it doesn't work what happens is the player just starts walking in one direction non stop even if I let the key go.

avatar image jerobinson · Jun 14, 2013 at 04:43 AM 0
Share

You would need to set the speed.x to 0 again when the key is released. Oh well, you fixed it anyways. Good Luck ;)

avatar image
0

Answer by Bradders1504 · Jun 13, 2013 at 08:06 AM

Make the Input.GetAxisRaw("Horizontal") a negative value like -(Input.GetAxisRaw("Horizontal")). For the GetHorizontalMovement() function.

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 superluigi · Jun 13, 2013 at 08:00 PM 0
Share

Thanks for the answer but it didn't work. All this did was switch the problem now he walks backwards when facing to the right

avatar image Bradders1504 · Jun 13, 2013 at 08:48 PM 0
Share

I see, was a quick answer, thanks for commenting back

avatar image bodec · Jun 14, 2013 at 05:40 AM 0
Share

GetAxisRaw and GetAxis work on a scale between 1 and -1 adding a negative to the input method would only change wich side is negative and positive.

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

17 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

Related Questions

CharacterController unexpectedly moving up 0 Answers

Keeping Character From Walking Through Walls 1 Answer

Rotate character to the moving direction problems? 2 Answers

Small Project, tutorials/help needed 0 Answers

Character Controller moves forever after colliding with object 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