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 /
avatar image
1
Question by MedianHansen · Mar 14, 2010 at 12:20 PM · erroreditorbuildkey

Why is GetKeyDown / Up behaving erratically, only responding occasionally in build?

Hi, i'm very new to unity, and as such, i was gonna make a basic demo, just to increase my skills, it should make the player capable of picking up certain items, and activating others, etc. etc.

Now, the key i used for picking up items was the 'E' key, like in many FPS. Being new, i just edited the "FPSWalker" script, which is a build-in, at least in my version of unity.

When testing and all that, it executed just fine, everything worked. I then tried to build it, and run it, and now the 'E' key seemed to only pick up the items once in a while. I then added the line: MoveDirection.y=jumpSpeed as a consequence for pressing the 'E' key, and still it only jumped sometimes, no saying when. The strange thing is still that in the unity editor, it all works fine, but as soon as i build it, the 'E' key only reacts sometimes, while any other key in the game (Which was already written in the build-in-script, not by me), works perfectly, and no lag is present, so it can't be any of those options, my script is as following:

var speed = 6.0; var jumpSpeed = 8.0; var gravity = 20.0; var gem; var altar; var gui; var Offer1=0;

private var moveDirection = Vector3.zero; private var grounded : boolean = false;

function Start () { gem=GameObject.Find("Obj1"); altar=GameObject.Find("Altar"); gui=GameObject.Find("GUI Text");

}

function FixedUpdate() { if (grounded) { // We are grounded, so recalculate movedirection directly from axes moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed;

     if (Input.GetButton ("Jump")) {
         moveDirection.y = jumpSpeed;
     }
 }

 // Apply gravity
 moveDirection.y -= gravity * Time.deltaTime;

 if (Input.GetKeyDown(KeyCode.E))
 {
     moveDirection.y = jumpSpeed;
     if (gem)
     {
         if (Vector3.Distance(transform.position,gem.transform.position)<2.5)
         if (gem.GetComponent("MouseOver").MO==1)
         {
             Offer1=1.0;
             Destroy(gem);
         }
     }

     if (altar)
     if (Vector3.Distance(transform.position,altar.transform.position)<3.2)
     if (altar.GetComponent("MouseOver").MO==1)
     if (Offer1==1)
     {
         gui.GetComponent("ShowLife").LifeUp(10);
         Offer1=0;
     }
 }

 // Move the controller
 var controller : CharacterController = GetComponent(CharacterController);
 var flags = controller.Move(moveDirection * Time.deltaTime);
 grounded = (flags & CollisionFlags.CollidedBelow) != 0;

}

@script RequireComponent(CharacterController)

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

1 Reply

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

Answer by duck · Mar 14, 2010 at 12:32 PM

It's because you're reading it in FixedUpdate. You should probably move the section of code relating to jumping to the Update function.

This is because GetKeyDown is true only for that frame where the key is pressed, so it's inherently linked to the frame rate (and therefore the framerate-based "Update()" function) rather than the physics-engine-based "FixedUpdate()" function. As a result, KeyDown or KeyUp input readings can be missed if you use it in FixedUpdate. The degree or frequency to which they're missed will be related to the framerate, so that is probably why you're seeing a difference between the performance in the editor compared with the built version.

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 MedianHansen · Mar 14, 2010 at 01:20 PM 0
Share

thanks a lot, i was wondering what the difference between those two functions were. I tried moving it to a Update() function, and it worked perfectly, thanks for the help once again ^^

avatar image Lipis · Mar 14, 2010 at 02:34 PM 0
Share

@$$anonymous$$edianHansen Don't forget to accept the answer if it was helpful.. (the little check-mark, next to the answer..) Also if you haven't already did it.. you have to read this: http://answers.unity3d.com/faq

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

No one has followed this question yet.

Related Questions

How to exclude "Selection & Handles" and other editor-specific code in builds? 2 Answers

Unity game crashes when build but not in editor 0 Answers

DLL build issues with Unity 3 1 Answer

GUI Question 0 Answers

Distribute terrain in zones 3 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