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 AustinB95 · Nov 20, 2014 at 07:09 AM · javascript

Keycode issues, newb(Sensationalist title removed)

I am having a huge problem with getting my code to work right. I want to move left right up and down, but I keep getting errors with my code. This is my very first game made and I just started coding, but I think I got most of it right. CODE(BELOW)

 #pragma strict
 
 var moveup :KeyCode;
 var movedown :KeyCode;
 var moveleft :KeyCode;
 var moveright :KeyCode;
 var speed : float = 10;
 
 function Update () 
 {
   if (Input.GetKey(moveup))
   {
     rigidbody2D.velocity.y = speed;
   }
   else if (Input.GetKey(movedown))
   {
     rigidbody2D.velocity.y = speed *-1;
   }
   else 
   {
     rigidbody2D.velocity.y =0;
   }
 
   if (Input.GetKey(moveleft))
   {
     rigidbody2D.velocity.x = speed *-1;
   }
   else if (Input.GetKey(moveright)).x = speed;
   {
   else
   {
     rigidbody2D.velocity.x =0;
   }
 };




Comment
Add comment · Show 3
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 meat5000 ♦ · Nov 20, 2014 at 01:50 AM 0
Share

Ah LOL man, you dont need to write out the line numbers manually but well done for the effort. It's good that you were considerate enough to put them in.

Highlight all your code and click the 101010 button! :D

Oh yes, please post the errors.

Try adding keycode directly to Get$$anonymous$$ey.

 Input.Get$$anonymous$$ey($$anonymous$$eyCode.G) //Letter G

Using GetButton will access custom buttons from Input manager,

avatar image Uldeim · Nov 20, 2014 at 07:14 AM 1
Share

Agreed. Tell us exactly what is happening incorrectly. Also, do you ever assign moveup, etc.?

avatar image AustinB95 · Nov 21, 2014 at 02:09 PM 0
Share

Uldiem I do assign the move up and move down keys within the script code once it's implemented into the character. I will try these specific key binds though. I was using the same move script from when I tried recreating Pong lol

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Landern · Nov 20, 2014 at 07:11 AM

Without the ACTUAL error, this becomes difficult. but off the bat, your if statements are malformed, you have a variable being set in an else if condition..., adjust to:

   #pragma strict
   var moveup :KeyCode;
   var movedown :KeyCode;
   var moveleft :KeyCode;
   var moveright :KeyCode;
   var speed : float = 10;
   function Update ()
   {
     if (Input.GetKey(moveup))
     {
       rigidbody2D.velocity.y = speed;
     }
     else if (Input.GetKey(movedown))
     {
       rigidbody2D.velocity.y = speed *-1;
     }
     else
     {
       rigidbody2D.velocity.y =0;
     }

     if (Input.GetKey(moveleft))
     {
       rigidbody2D.velocity.x = speed *-1;
     }
     else if (Input.GetKey(moveright))
     {
       rigidbody2D.velocity.x = speed;
     }
     else
     {
       rigidbody2D.velocity.x =0;
     }
   };



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 AustinB95 · Nov 21, 2014 at 02:29 PM

Thank you so much!

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How long should it take me to make my game 2 Answers

How do I single out one object amongst a group of objects with the same tag? 3 Answers

I don't understand Vector2 + Vector3 3 Answers

Parenting Objects 1 Answer

Why isn't my word generator working? 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