Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 garstikaitis · Sep 18, 2016 at 06:16 PM · quaternionlocalrotationlocaleulerangles

GetKeyDown works only once

Hi, I want to make my character rotate in the direction which he is facing(the game is top view), and I encountered this problem:

Whenever I start the game, I start facing up, and I can press the arrow keys once. When I try to click again, my character moves, but does not rotate.

Right now I just care about rotating character in horizontal axis.

 void Start () {
     rb2d = gameObject.GetComponent<Rigidbody2D> ();
     facingUp = true;
     facingRight = true;
     facingLeft = true;
     facingDown = true;
 }
 
 // Update is called once per frame
 void Update () {
     float horizontal = Input.GetAxis ("Horizontal");
     float vertical = Input.GetAxis ("Vertical");
     HandleMovement (horizontal, vertical);

     Flip (horizontal, vertical);
 }

 private void HandleMovement(float horizontal, float vertical) {
     rb2d.velocity = new Vector2 (horizontal * speed, vertical * speed);
 }
    // this method resets the coordinates of the player
       void Reset() {
     if (!target)
         target = GameObject.FindWithTag("Player");

 }

 if (horizontal < 0 && facingRight || horizontal > 0 && !facingRight && Input.GetKeyDown("right")) {
         Reset ();
         facingRight = !facingRight;
         Vector3 rotationRight = transform.localEulerAngles;
         rotationRight.z = 90;
         transform.localRotation = Quaternion.Euler (0, 0, rotationRight.z);
         Debug.Log (Input.anyKeyDown);
     }

     // left button

     else if (horizontal < 0 && facingLeft || horizontal > 0 && !facingLeft && Input.GetKeyDown("left")) {
         Reset ();
         facingLeft = !facingLeft;
         Vector3 rotationLeft = transform.localEulerAngles;
         rotationLeft.z = -90;
         transform.localRotation = Quaternion.Euler (0, 0, rotationLeft.z);
         Debug.Log ("you pressed left");
     }
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

· Add your reply
  • Sort: 
avatar image
0

Answer by SergioSandiaz · Sep 19, 2016 at 06:00 AM

Well... that´s normal, I can´t understand your problem but

 Input.GetKeyDown

is designed for work only once, the you need to release it and click again, you can use

 Input.GetKey

that one returns true while the user press the key, Hope that helps you

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 Bunny83 · Sep 18, 2016 at 11:44 PM

Well, you have those two variables:

 facingRight
 facingLeft 

which you check in your if statement and toggle inside the statement. So once you execute the if the state of the variable will flip and you can't enter the if again. That's literally what you code does. I'm not sure what those variables are good for. At the moment their only use is to prevent the if statement from being executed multiple times.

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 NoseKills · Sep 20, 2016 at 06:43 AM 0
Share

In general, why have 2 booleans to represent a state that can have 2 values. You are facing right or left. You probably shouldn't be able to face "right and left" or "not right and not left". If you need up and down too, make an enum or something

 public enum Facing {
     Up,
     Down,
     Left,
     Right
 }

Sure you can make it work with booleans but for example debugging gets a lot easier if your variables can't end up in invalid states

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

6 People are following this question.

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

Related Questions

Complicated Rotation Issue 1 Answer

Unity Simulate Local Rotation 0 Answers

Code to rotate around a local axis until local Y is 0? 0 Answers

Child versus Parent rotations 3 Answers

is there any way to get a sane number on an objects current y rotation? 2 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