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 /
  • Help Room /
avatar image
0
Question by Emme73 · Mar 25, 2019 at 04:52 PM · 2dcontrols

2D spaceship controls, ship flies without key input :I

Hi, beeing a noob, I wonder why my 3d model spaceship already flies to the right without a button beeing pressed :D Also, how can I make it so that it only flies when I press a button, and stops when I release it ? Up/down works, left/right not, although its the same command...Thanks !

Public class Player_Control : MonoBehaviour {

 void Start()
 {
     
 }

 public float moveSpeed = 10f;
 public float turnSpeed = 5f;
 public float tiltAngle = 60f;

 
 

 void Update()

 {




 if (Input.GetKey(KeyCode.DownArrow))
    transform.Translate(Vector3.down * moveSpeed * Time.deltaTime);

     if (Input.GetKey(KeyCode.UpArrow))
    transform.Translate(Vector3.up * moveSpeed * Time.deltaTime);

     Quaternion target2 = Quaternion.Euler(0, 0, 0);
     if (Input.GetKey(KeyCode.RightArrow))
         transform.rotation = target2;
     transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);

     Quaternion target = Quaternion.Euler(0, 180, 0);
     if (Input.GetKey(KeyCode.LeftArrow))
         transform.rotation = target;
     transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
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
Best Answer

Answer by Alex_May · Mar 25, 2019 at 09:59 PM

You need to put some of your code in a scope, or the compiler won't know which instructions to execute after your ifs. Use the curly braces for this. Here's your code with braces added that should work:

 if (Input.GetKey(KeyCode.DownArrow))
 {
     transform.Translate(Vector3.down * moveSpeed * Time.deltaTime);
 }
 
 if (Input.GetKey(KeyCode.UpArrow))
 {        
     transform.Translate(Vector3.up * moveSpeed * Time.deltaTime);
 }
 
 Quaternion target2 = Quaternion.Euler(0, 0, 0);
 if (Input.GetKey(KeyCode.RightArrow))
 {
     // you want to both set the rotation and to move the ship when
     // the button is down, so you need to group these two statements together.
     transform.rotation = target2;
     transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
 }
 
 Quaternion target = Quaternion.Euler(0, 180, 0);
 if (Input.GetKey(KeyCode.LeftArrow))
 {
     transform.rotation = target;
     // changed Vector3.forward to Vector3.back
     transform.Translate(Vector3.back * moveSpeed * Time.deltaTime);
 }
 




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 Alex_May · Mar 25, 2019 at 10:01 PM 0
Share

If you omit the curly braces, the compiler interprets this as an instruction to execute only the first command after the if statement if the condition is true. So your code was executing the rotation commands if the relevant keys were pressed, but was going ahead and executing the translation commands regardless of the outcome of the if statement.

avatar image
0

Answer by Emme73 · Mar 27, 2019 at 05:08 PM

Hey, thanks ! Thats what I did, and now it works !!!

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 Alex_May · Mar 27, 2019 at 10:00 PM 0
Share

Welcome - please mark my answer as correct is possible!

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

267 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 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 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 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

How to : Tap and hold to go down. Double tap and hold to go down faster? 0 Answers

Swapping player's controls in game (2D) 1 Answer

Changing (swapping) users movement controls IN GAME on trigger (2D) 0 Answers

Good Solution to Physics Based Gameplay 1 Answer

2D Movement Controls Like a Dying Fly 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