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 /
This question was closed Feb 09, 2015 at 06:58 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by kfchristian15 · Feb 09, 2015 at 04:01 PM · c#rotationmovementscriptingbasicsgetkeyup

GetKeyUp not registering

I have a movement script and I want my character to turn around and move the other way rather than moonwalk backwards (forward animation playing but backward movement). This script works, but sometimes the GetKeyUp won't register and the character will be stuck facing the wrong way and you have to play with it to get them to face the right way again. Is there a simpler or better way to do this (maybe I should use an else instead of if)? Any input is welcome, and thanks in advance.

 if (Input.GetKey (KeyCode.RightArrow)) {

             animation.Play ("Allosaurus_Walk");
             rigidbody.AddForce(Vector3.right *speedH);
         }
         if (Input.GetKeyUp (KeyCode.RightArrow))
         {
             animation.Stop ("Allosaurus_Walk");
             animation.Play ("Allosaurus_Idle");
         }
         if (Input.GetKeyUp (KeyCode.LeftArrow))
         {
             animation.Stop ("Allosaurus_Walk");
             animation.Play ("Allosaurus_Idle");
             if (Input.GetKeyUp (KeyCode.LeftArrow))
             {
                 transform.Rotate (0,180,0);
             }
         }
         if (Input.GetKey (KeyCode.LeftArrow))
         {
             animation.Play ("Allosaurus_Walk");
             rigidbody.AddForce(Vector3.left *speedH);
             if (Input.GetKeyDown (KeyCode.LeftArrow))
             {
                 transform.Rotate (0,180,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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by Eric5h5 · Feb 09, 2015 at 06:04 PM

If the code is in FixedUpdate, it must be moved to Update. KeyUp and Down events are only true for the one frame that they're activated, and FixedUpdate may well not run that frame.

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 kfchristian15 · Feb 09, 2015 at 06:28 PM 0
Share

That fixed it, thanks!

avatar image
0

Answer by hexagonius · Feb 09, 2015 at 05:18 PM

An easier approach could be:

  • create a vector3.zero

    • depending on left or right set x to 1 or -1

    • depending on x stop if 0 and start if not

    set the rotation. Check out Quaternion.Euler. This way you dont turn around but face the correct direction

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 kfchristian15 · Feb 09, 2015 at 06:08 PM 0
Share

That sounds really simple, but it's new for me. What's Quaternion.Euler (I looked it up and it just says it rotates something around the z axis, so how is it different from transform.rotate)? Could you give me an example of this please? Thanks for your suggestion, it sounds promising and a lot more simple than what I had.

avatar image hexagonius · Feb 10, 2015 at 06:02 AM 0
Share

While Rotate is actually rotating your character every time it is called, Quaternion.Euler creates a look direction.

 transform.rotation = Quaternion.Euler(0, -180, 0);

Will make your character look left, and it does not matter how often you call it. Provided that your Character's default orientation was looking right (its rotation values all 0)

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Flip over an object (smooth transition) 3 Answers

Objects not always Spawning At Correct Location 2 Answers

Problems after rotating character. 0 Answers

How would I go About making a flying player move forward, but also Around another object? 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